Home | History | Annotate | Download | only in unittest
      1 package org.unicode.cldr.unittest;
      2 
      3 import java.util.Collections;
      4 import java.util.Comparator;
      5 import java.util.HashSet;
      6 import java.util.Map.Entry;
      7 import java.util.Set;
      8 import java.util.TreeSet;
      9 
     10 import org.unicode.cldr.util.CLDRConfig;
     11 import org.unicode.cldr.util.CLDRFile;
     12 import org.unicode.cldr.util.DtdData;
     13 import org.unicode.cldr.util.DtdData.AttributeValueComparator;
     14 import org.unicode.cldr.util.DtdType;
     15 import org.unicode.cldr.util.Timer;
     16 import org.unicode.cldr.util.XPathParts;
     17 
     18 import com.ibm.icu.dev.util.CollectionUtilities;
     19 import com.ibm.icu.util.Output;
     20 
     21 public class TestPerf extends TestFmwkPlus {
     22     public static void main(String[] args) {
     23         new TestPerf().run(args);
     24     }
     25 
     26     static final int ITERATIONS = 20;
     27     static final Set<String> testPaths;
     28     static final int elementSize;
     29     static final Set<String> elements = new HashSet<String>();
     30     static final Set<String> attributes = new HashSet<String>();
     31     static final Set<String> attributeValues = new HashSet<String>();
     32     static final String[] sortedArray;
     33 
     34     static {
     35         Set<String> testPaths_ = new HashSet<String>();
     36         CollectionUtilities.addAll(CLDRConfig.getInstance().getEnglish()
     37             .iterator(), testPaths_);
     38         testPaths = Collections.unmodifiableSet(testPaths_);
     39         Set<String> sorted = new TreeSet<String>(
     40             CLDRFile.getComparator(DtdType.ldml));
     41         sorted.addAll(testPaths);
     42         sortedArray = sorted.toArray(new String[sorted.size()]);
     43 
     44         // warmup
     45         int size = 0;
     46         for (String p : testPaths) {
     47             XPathParts xpp = XPathParts.getFrozenInstance(p);
     48             size += xpp.size();
     49             for (int i = 0; i < xpp.size(); ++i) {
     50                 elements.add(xpp.getElement(i));
     51                 for (Entry<String, String> attributeAndValue : xpp
     52                     .getAttributes(i).entrySet()) {
     53                     String attribute = attributeAndValue.getKey();
     54                     String value = attributeAndValue.getValue();
     55                     if (attributes.add(attribute)) {
     56                         // System.out.println("Adding " + attribute + ", " + p);
     57                     }
     58                     attributeValues.add(value);
     59                 }
     60             }
     61         }
     62         elementSize = size;
     63     }
     64 
     65     public void TestA() {
     66         logln("Path count: " + testPaths.size());
     67         logln("Elements: " + elements.size());
     68         logln("Attributes: " + attributes.size() + "\t" + attributes);
     69         logln("AttributeValues: " + attributeValues.size());
     70     }
     71 
     72     @Override
     73     protected void init() throws Exception {
     74         super.init();
     75     }
     76 
     77     public void TestXPathParts() {
     78         Timer t = new Timer();
     79         t.start();
     80         int size = 0;
     81         for (String p : testPaths) {
     82             for (int i = 0; i < ITERATIONS; ++i) {
     83                 XPathParts xpp = new XPathParts().set(p);
     84                 size += xpp.size();
     85             }
     86         }
     87         long duration = t.stop();
     88         assertRelation("", true, duration / ITERATIONS / 1000000.0, LEQ, 50.0); // 47231000
     89     }
     90 
     91     public void TestMutableXPathParts() {
     92         Timer t = new Timer();
     93         t.start();
     94         int size = 0;
     95         XPathParts xpp = new XPathParts();
     96         for (String p : testPaths) {
     97             for (int i = 0; i < ITERATIONS; ++i) {
     98                 xpp.set(p);
     99                 size += xpp.size();
    100             }
    101         }
    102         long duration = t.stop();
    103         assertRelation("", true, duration / ITERATIONS / 1000000.0, LEQ, 50.0); // 47231000
    104         assertEquals("", elementSize, size / ITERATIONS);
    105     }
    106 
    107     public void TestFastFrozenXPathParts() {
    108         Timer t = new Timer();
    109         t.start();
    110         int size = 0;
    111         for (String p : testPaths) {
    112             for (int i = 0; i < ITERATIONS; ++i) {
    113                 XPathParts xpp = XPathParts.getFrozenInstance(p);
    114                 size += xpp.size();
    115             }
    116         }
    117         long duration = t.stop();
    118         assertRelation("", true, duration / ITERATIONS / 1000000.0, LEQ, 50.0);
    119         assertEquals("", elementSize, size / ITERATIONS);
    120     }
    121 
    122     public void TestFastXPathParts() {
    123         Timer t = new Timer();
    124         t.start();
    125         int size = 0;
    126         for (String p : testPaths) {
    127             for (int i = 0; i < ITERATIONS; ++i) {
    128                 XPathParts xpp = XPathParts.getInstance(p);
    129                 size += xpp.size();
    130             }
    131         }
    132         long duration = t.stop();
    133         assertRelation("", true, duration / ITERATIONS / 1000000.0, LEQ, 50.0);
    134         assertEquals("", elementSize, size / ITERATIONS);
    135     }
    136 
    137     public void TestXPathPartsWithComparators() {
    138         XPathParts normal = new XPathParts();
    139         DtdData dtdData = DtdData.getInstance(DtdType.ldml);
    140 
    141         XPathParts newParts = new XPathParts(dtdData.getAttributeComparator(),
    142             null);
    143         for (String path : sortedArray) {
    144             String newPath = newParts.set(path).toString();
    145             assertEquals("path", path, newPath);
    146         }
    147     }
    148 
    149     public void TestPathComparison() {
    150         DtdData dtdData = DtdData.getInstance(DtdType.ldml);
    151         AttributeValueComparator avc = new AttributeValueComparator() {
    152             @Override
    153             public int compare(String element, String attribute, String value1,
    154                 String value2) {
    155                 Comparator<String> comp = CLDRFile.getAttributeValueComparator(
    156                     element, attribute);
    157                 return comp.compare(value1, value2);
    158             }
    159         };
    160         Comparator<String> comp = dtdData.getDtdComparator(avc);
    161 
    162         int iterations = 50;
    163         Output<Integer> failures = new Output<Integer>();
    164 
    165         // warmup
    166         checkCost(sortedArray, CLDRFile.getComparator(DtdType.ldml), 1,
    167             failures);
    168         assertRelation("CLDRFile.ldmlComparator-check", true, failures.value,
    169             LEQ, 0);
    170         double seconds = checkCost(sortedArray,
    171             CLDRFile.getComparator(DtdType.ldml), iterations, failures);
    172         assertRelation("CLDRFile.ldmlComparator", true, seconds, LEQ, 0.1);
    173         // logln(title + "\tTime:\t" + timer.toString(iterations));
    174 
    175         // warmup
    176         checkCost(sortedArray, comp, 1, failures);
    177         assertRelation("DtdComparator-check", true, failures.value, LEQ, 0);
    178         double newSeconds = checkCost(sortedArray, comp, iterations, failures);
    179         assertRelation("DtdComparator", true, newSeconds, LEQ, seconds * .5); // new
    180         // code
    181         // needs
    182         // to
    183         // be
    184         // twice
    185         // as
    186         // fast
    187     }
    188 
    189     private double checkCost(String[] sortedArray, Comparator<String> comp,
    190         int iterations, Output<Integer> failures2) {
    191         Timer timer = new Timer();
    192         int failures = 0;
    193         for (int i = 0; i < iterations; ++i) {
    194             String lastPath = null;
    195             for (String currentPath : sortedArray) {
    196                 if (lastPath != null) {
    197                     if (comp.compare(lastPath, currentPath) > 0) {
    198                         failures++;
    199                     }
    200                 }
    201                 lastPath = currentPath;
    202             }
    203         }
    204         timer.stop();
    205         failures2.value = failures;
    206         return timer.getSeconds() / iterations;
    207     }
    208 
    209     public void TestUnused() {
    210 
    211     }
    212 }
    213