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