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