]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/perf/decimalformatperf.pl
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / perf / decimalformatperf.pl
1 #!/usr/local/bin/perl\r
2 # *******************************************************************************\r
3 # * Copyright (C) 2002-2007 International Business Machines Corporation and     *\r
4 # * others. All Rights Reserved.                                                *\r
5 # *******************************************************************************\r
6 \r
7 use strict;\r
8 \r
9 # Assume we are running within the icu4j root directory\r
10 use lib 'src/com/ibm/icu/dev/test/perf';\r
11 use Dataset;\r
12 \r
13 #---------------------------------------------------------------------\r
14 # Test class\r
15 my $TESTCLASS = 'com.ibm.icu.dev.test.perf.DecimalFormatPerformanceTest';\r
16 \r
17 # Methods to be tested.  Each pair represents a test method and\r
18 # a baseline method which is used for comparison.\r
19 my @METHODS  = (\r
20                  ['TestJDKConstruction',     'TestICUConstruction'],\r
21                  ['TestJDKParse',            'TestICUParse'],\r
22                  ['TestJDKFormat',           'TestICUFormat']\r
23                );\r
24 # Patterns which define the set of characters used for testing.\r
25 my @OPTIONS = (\r
26 #                 locale    pattern      date string\r
27                 [ "en_US",  "#,###.##",  "1,234.56"],\r
28                 [ "de_DE",  "#,###.##",  "1.234,56"],\r
29               );\r
30 \r
31 my $THREADS;        # number of threads (input from command-line args)\r
32 my $CALIBRATE = 2;  # duration in seconds for initial calibration\r
33 my $DURATION  = 10; # duration in seconds for each pass\r
34 my $NUMPASSES = 4;  # number of passes.  If > 1 then the first pass\r
35                     # is discarded as a JIT warm-up pass.\r
36 \r
37 my $TABLEATTR = 'BORDER="1" CELLPADDING="4" CELLSPACING="0"';\r
38 \r
39 my $PLUS_MINUS = "±";\r
40 \r
41 if ($NUMPASSES < 3) {\r
42     die "Need at least 3 passes.  One is discarded (JIT warmup) and need two to have 1 degree of freedom (t distribution).";\r
43 }\r
44 \r
45 my $OUT; # see out()\r
46 \r
47 # run all tests with the specified number of threads from command-line input\r
48 # (if there is no arguments, use $THREADS = 1)\r
49 foreach my $arg ($#ARGV >= 0 ? @ARGV : "1") {\r
50   $THREADS = $arg;\r
51   main();\r
52 }\r
53 \r
54 \r
55 #---------------------------------------------------------------------\r
56 # ...\r
57 sub main {\r
58     my $date = localtime;\r
59     my $threads = ($THREADS > 1) ? "($THREADS threads)" : "";\r
60     my $title = "ICU4J Performance Test $threads $date";\r
61 \r
62     my $html = $date;\r
63     $html =~ s/://g; # ':' illegal\r
64     $html =~ s/\s*\d+$//; # delete year\r
65     $html =~ s/^\w+\s*//; # delete dow\r
66     $html = "perf $html.html";\r
67 \r
68     open(HTML,">$html") or die "Can't write to $html: $!";\r
69 \r
70     print HTML <<EOF;\r
71 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"\r
72    "http://www.w3.org/TR/html4/strict.dtd">\r
73 <HTML>\r
74    <HEAD>\r
75       <TITLE>$title</TITLE>\r
76    </HEAD>\r
77    <BODY>\r
78 EOF\r
79     print HTML "<H1>$title</H1>\n";\r
80 \r
81     print HTML "<H2>$TESTCLASS</H2>\n";\r
82 \r
83     my $raw = "";\r
84 \r
85     for my $methodPair (@METHODS) {\r
86 \r
87         my $testMethod = $methodPair->[0];\r
88         my $baselineMethod = $methodPair->[1];\r
89 \r
90         print HTML "<P><TABLE $TABLEATTR><TR><TD>\n";\r
91         print HTML "<P><B>$testMethod vs. $baselineMethod</B></P>\n";\r
92         \r
93         print HTML "<P><TABLE $TABLEATTR BGCOLOR=\"#CCFFFF\">\n";\r
94         print HTML "<TR><TD>Options</TD><TD>$testMethod</TD>";\r
95         print HTML "<TD>$baselineMethod</TD><TD>Ratio</TD></TR>\n";\r
96         $OUT = '';\r
97 \r
98         for my $pat (@OPTIONS) {\r
99             print HTML "<TR><TD>@$pat[0], \"@$pat[1]\", \"@$pat[2]\"</TD>\n";\r
100 \r
101             out("<P><TABLE $TABLEATTR WIDTH=\"100%\">");\r
102 \r
103             # measure the test method\r
104             out("<TR><TD>");\r
105             print "\n$testMethod [@$pat]\n";\r
106             my $t = measure2($testMethod, $pat, -$DURATION);\r
107             out("</TD></TR>");\r
108             print HTML "<TD>", formatSeconds(4, $t->getMean(), $t->getError);\r
109             print HTML "/event</TD>\n";\r
110 \r
111             # measure baseline method\r
112             out("<TR><TD>");\r
113             print "\n$baselineMethod [@$pat]\n";\r
114             my $b = measure2($baselineMethod, $pat, -$DURATION);\r
115             out("</TD></TR>");\r
116             print HTML "<TD>", formatSeconds(4, $b->getMean(), $t->getError);\r
117             print HTML "/event</TD>\n";\r
118 \r
119             out("</TABLE></P>");\r
120 \r
121             # output ratio\r
122             my $r = $t->divide($b);\r
123             my $mean = $r->getMean() - 1;\r
124             my $color = $mean < 0 ? "RED" : "BLACK";\r
125             print HTML "<TD><B><FONT COLOR=\"$color\">", formatPercent(3, $mean, $r->getError);\r
126             print HTML "</FONT></B></TD></TR>\n";\r
127         }\r
128 \r
129         print HTML "</TABLE></P>\n";\r
130 \r
131         print HTML "<P>Raw data:</P>\n";\r
132         print HTML $OUT;\r
133         print HTML "</TABLE></P>\n";\r
134     }\r
135 \r
136     print HTML <<EOF;\r
137    </BODY>\r
138 </HTML>\r
139 EOF\r
140     close(HTML) or die "Can't close $html: $!";\r
141 }\r
142 \r
143 #---------------------------------------------------------------------\r
144 # Append text to the global variable $OUT\r
145 sub out {\r
146     $OUT .= join('', @_);\r
147 }\r
148 \r
149 #---------------------------------------------------------------------\r
150 # Append text to the global variable $OUT\r
151 sub outln {\r
152     $OUT .= join('', @_) . "\n";\r
153 }\r
154 \r
155 #---------------------------------------------------------------------\r
156 # Measure a given test method with a give test pattern using the\r
157 # global run parameters.\r
158 #\r
159 # @param the method to run\r
160 # @param the pattern defining characters to test\r
161 # @param if >0 then the number of iterations per pass.  If <0 then\r
162 #        (negative of) the number of seconds per pass.\r
163 #\r
164 # @return a Dataset object, scaled by iterations per pass and\r
165 #         events per iteration, to give time per event\r
166 #\r
167 sub measure2 {\r
168     my @data = measure1(@_);\r
169     my $iterPerPass = shift(@data);\r
170     my $eventPerIter = shift(@data);\r
171 \r
172     shift(@data) if (@data > 1); # discard first run\r
173 \r
174     my $ds = Dataset->new(@data);\r
175     $ds->setScale(1.0e-3 / ($iterPerPass * $eventPerIter));\r
176     $ds;\r
177 }\r
178 \r
179 #---------------------------------------------------------------------\r
180 # Measure a given test method with a give test pattern using the\r
181 # global run parameters.\r
182 #\r
183 # @param the method to run\r
184 # @param the pattern defining characters to test\r
185 # @param if >0 then the number of iterations per pass.  If <0 then\r
186 #        (negative of) the number of seconds per pass.\r
187 #\r
188 # @return array of:\r
189 #         [0] iterations per pass\r
190 #         [1] events per iteration\r
191 #         [2..] ms reported for each pass, in order\r
192 #\r
193 sub measure1 {\r
194     my $method = shift;\r
195     my $pat = shift;\r
196     my $iterCount = shift; # actually might be -seconds/pass\r
197 \r
198     out("<P>Measuring $method for input file @$pat[0] for encoding @$pat[2] , ");\r
199     if ($iterCount > 0) {\r
200         out("$iterCount iterations/pass, $NUMPASSES passes</P>\n");\r
201     } else {\r
202         out(-$iterCount, " seconds/pass, $NUMPASSES passes</P>\n");\r
203     }\r
204 \r
205     # is $iterCount actually -seconds/pass?\r
206     if ($iterCount < 0) {\r
207 \r
208         # calibrate: estimate ms/iteration\r
209         print "Calibrating...";\r
210         my @t = callJava($method, $pat, -$CALIBRATE, 1);\r
211         print "done.\n";\r
212 \r
213         my @data = split(/\s+/, $t[0]->[2]);\r
214         $data[0] *= 1.0e+3;\r
215 \r
216         my $timePerIter = 1.0e-3 * $data[0] / $data[1];\r
217         \r
218         # determine iterations/pass\r
219         $iterCount = int(-$iterCount / $timePerIter + 0.5);\r
220         \r
221         out("<P>Calibration pass ($CALIBRATE sec): ");\r
222         out("$data[0] ms, ");\r
223         out("$data[1] iterations = ");\r
224         out(formatSeconds(4, $timePerIter), "/iteration<BR>\n");\r
225     }\r
226     \r
227     # run passes\r
228     print "Measuring $iterCount iterations x $NUMPASSES passes...";\r
229     my @t = callJava($method, $pat, $iterCount, $NUMPASSES);\r
230     print "done.\n";\r
231     my @ms = ();\r
232     my @b; # scratch\r
233     for my $a (@t) {\r
234         # $a->[0]: method name, corresponds to $method\r
235         # $a->[1]: 'begin' data, == $iterCount\r
236         # $a->[2]: 'end' data, of the form <ms> <loops> <eventsPerIter>\r
237         # $a->[3...]: gc messages from JVM during pass\r
238         @b = split(/\s+/, $a->[2]);\r
239         push(@ms, $b[0] * 1.0e+3);\r
240     }\r
241     my $eventsPerIter = $b[2];\r
242 \r
243     out("Iterations per pass: $iterCount<BR>\n");\r
244     out("Events per iteration: $eventsPerIter<BR>\n");\r
245 \r
246     my @ms_str = @ms;\r
247     $ms_str[0] .= " (discarded)" if (@ms_str > 1);\r
248     out("Raw times (ms/pass): ", join(", ", @ms_str), "<BR>\n");\r
249 \r
250     ($iterCount, $eventsPerIter, @ms);\r
251 }\r
252 \r
253 #---------------------------------------------------------------------\r
254 # Invoke java to run $TESTCLASS, passing it the given parameters.\r
255 #\r
256 # @param the method to run\r
257 # @param the number of iterations, or if negative, the duration\r
258 #        in seconds.  If more than on pass is desired, pass in\r
259 #        a string, e.g., "100 100 100".\r
260 # @param the pattern defining characters to test\r
261 #\r
262 # @return an array of results.  Each result is an array REF\r
263 #         describing one pass.  The array REF contains:\r
264 #         ->[0]: The method name as reported\r
265 #         ->[1]: The params on the '= <meth> begin ...' line\r
266 #         ->[2]: The params on the '= <meth> end ...' line\r
267 #         ->[3..]: GC messages from the JVM, if any\r
268 #\r
269 sub callJava {\r
270     my $method = shift;\r
271     my $pat = shift;\r
272     my $n = shift;\r
273     my $passes = shift;\r
274     \r
275     my $n = ($n < 0) ? "-t ".(-$n) : "-i ".$n;\r
276     \r
277     my $cmd = "java -classpath classes $TESTCLASS $method $n -p $passes -L @$pat[0] \"@$pat[1]\" \"@$pat[2]\" -r $THREADS";\r
278     print "[$cmd]\n"; # for debugging\r
279     open(PIPE, "$cmd|") or die "Can't run \"$cmd\"";\r
280     my @out;\r
281     while (<PIPE>) {\r
282         push(@out, $_);\r
283     }\r
284     close(PIPE) or die "Java failed: \"$cmd\"";\r
285 \r
286     @out = grep(!/^\#/, @out);  # filter out comments\r
287 \r
288     #print "[", join("\n", @out), "]\n";\r
289 \r
290     my @results;\r
291     my $method = '';\r
292     my $data = [];\r
293     foreach (@out) {\r
294         next unless (/\S/);\r
295 \r
296         if (/^=\s*(\w+)\s*(\w+)\s*(.*)/) {\r
297             my ($m, $state, $d) = ($1, $2, $3);\r
298             #print "$_ => [[$m $state $data]]\n";\r
299             if ($state eq 'begin') {\r
300                 die "$method was begun but not finished" if ($method);\r
301                 $method = $m;\r
302                 push(@$data, $d);\r
303                 push(@$data, ''); # placeholder for end data\r
304             } elsif ($state eq 'end') {\r
305                 if ($m ne $method) {\r
306                     die "$method end does not match: $_";\r
307                 }\r
308                 $data->[1] = $d; # insert end data at [1]\r
309                 #print "#$method:", join(";",@$data), "\n";\r
310                 unshift(@$data, $method); # add method to start\r
311 \r
312                 push(@results, $data);\r
313                 $method = '';\r
314                 $data = [];\r
315             } else {\r
316                 die "Can't parse: $_";\r
317             }\r
318         }\r
319 \r
320         elsif (/^\[/) {\r
321             if ($method) {\r
322                 push(@$data, $_);\r
323             } else {\r
324                 # ignore extraneous GC notices\r
325             }\r
326         }\r
327 \r
328         else {\r
329             die "Can't parse: $_";\r
330         }\r
331     }\r
332 \r
333     die "$method was begun but not finished" if ($method);\r
334 \r
335     @results;\r
336 }\r
337 \r
338 #|#---------------------------------------------------------------------\r
339 #|# Format a confidence interval, as given by a Dataset.  Output is as\r
340 #|# as follows:\r
341 #|#   241.23 - 241.98 => 241.5 +/- 0.3\r
342 #|#   241.2 - 243.8 => 242 +/- 1\r
343 #|#   211.0 - 241.0 => 226 +/- 15 or? 230 +/- 20\r
344 #|#   220.3 - 234.3 => 227 +/- 7\r
345 #|#   220.3 - 300.3 => 260 +/- 40\r
346 #|#   220.3 - 1000 => 610 +/- 390 or? 600 +/- 400\r
347 #|#   0.022 - 0.024 => 0.023 +/- 0.001\r
348 #|#   0.022 - 0.032 => 0.027 +/- 0.005\r
349 #|#   0.022 - 1.000 => 0.5 +/- 0.5\r
350 #|# In other words, take one significant digit of the error value and\r
351 #|# display the mean to the same precision.\r
352 #|sub formatDataset {\r
353 #|    my $ds = shift;\r
354 #|    my $lower = $ds->getMean() - $ds->getError();\r
355 #|    my $upper = $ds->getMean() + $ds->getError();\r
356 #|    my $scale = 0;\r
357 #|    # Find how many initial digits are the same\r
358 #|    while ($lower < 1 ||\r
359 #|           int($lower) == int($upper)) {\r
360 #|        $lower *= 10;\r
361 #|        $upper *= 10;\r
362 #|        $scale++;\r
363 #|    }\r
364 #|    while ($lower >= 10 &&\r
365 #|           int($lower) == int($upper)) {\r
366 #|        $lower /= 10;\r
367 #|        $upper /= 10;\r
368 #|        $scale--;\r
369 #|    }\r
370 #|}\r
371 \r
372 #---------------------------------------------------------------------\r
373 # Format a number, optionally with a +/- delta, to n significant\r
374 # digits.\r
375 #\r
376 # @param significant digit, a value >= 1\r
377 # @param multiplier\r
378 # @param time in seconds to be formatted\r
379 # @optional delta in seconds\r
380 #\r
381 # @return string of the form "23" or "23 +/- 10".\r
382 #\r
383 sub formatNumber {\r
384     my $sigdig = shift;\r
385     my $mult = shift;\r
386     my $a = shift;\r
387     my $delta = shift; # may be undef\r
388     \r
389     my $result = formatSigDig($sigdig, $a*$mult);\r
390     if (defined($delta)) {\r
391         my $d = formatSigDig($sigdig, $delta*$mult);\r
392         # restrict PRECISION of delta to that of main number\r
393         if ($result =~ /\.(\d+)/) {\r
394             # TODO make this work for values with all significant\r
395             # digits to the left of the decimal, e.g., 1234000.\r
396 \r
397             # TODO the other thing wrong with this is that it\r
398             # isn't rounding the $delta properly.  Have to put\r
399             # this logic into formatSigDig().\r
400             my $x = length($1);\r
401             $d =~ s/\.(\d{$x})\d+/.$1/;\r
402         }\r
403         $result .= " $PLUS_MINUS " . $d;\r
404     }\r
405     $result;\r
406 }\r
407 \r
408 #---------------------------------------------------------------------\r
409 # Format a time, optionally with a +/- delta, to n significant\r
410 # digits.\r
411 #\r
412 # @param significant digit, a value >= 1\r
413 # @param time in seconds to be formatted\r
414 # @optional delta in seconds\r
415 #\r
416 # @return string of the form "23 ms" or "23 +/- 10 ms".\r
417 #\r
418 sub formatSeconds {\r
419     my $sigdig = shift;\r
420     my $a = shift;\r
421     my $delta = shift; # may be undef\r
422 \r
423     my @MULT = (1   , 1e3,  1e6,  1e9);\r
424     my @SUFF = ('s' , 'ms', 'us', 'ns');\r
425 \r
426     # Determine our scale\r
427     my $i = 0;\r
428     ++$i while ($a*$MULT[$i] < 1 && $i < @MULT);\r
429     \r
430     formatNumber($sigdig, $MULT[$i], $a, $delta) . ' ' . $SUFF[$i];\r
431 }\r
432 \r
433 #---------------------------------------------------------------------\r
434 # Format a percentage, optionally with a +/- delta, to n significant\r
435 # digits.\r
436 #\r
437 # @param significant digit, a value >= 1\r
438 # @param value to be formatted, as a fraction, e.g. 0.5 for 50%\r
439 # @optional delta, as a fraction\r
440 #\r
441 # @return string of the form "23 %" or "23 +/- 10 %".\r
442 #\r
443 sub formatPercent {\r
444     my $sigdig = shift;\r
445     my $a = shift;\r
446     my $delta = shift; # may be undef\r
447     \r
448     formatNumber($sigdig, 100, $a, $delta) . ' %';\r
449 }\r
450 \r
451 #---------------------------------------------------------------------\r
452 # Format a number to n significant digits without using exponential\r
453 # notation.\r
454 #\r
455 # @param significant digit, a value >= 1\r
456 # @param number to be formatted\r
457 #\r
458 # @return string of the form "1234" "12.34" or "0.001234".  If\r
459 #         number was negative, prefixed by '-'.\r
460 #\r
461 sub formatSigDig {\r
462     my $n = shift() - 1;\r
463     my $a = shift;\r
464 \r
465     local $_ = sprintf("%.${n}e", $a);\r
466     my $sign = (s/^-//) ? '-' : '';\r
467 \r
468     my $a_e;\r
469     my $result;\r
470     if (/^(\d)\.(\d+)e([-+]\d+)$/) {\r
471         my ($d, $dn, $e) = ($1, $2, $3);\r
472         $a_e = $e;\r
473         $d .= $dn;\r
474         $e++;\r
475         $d .= '0' while ($e > length($d));\r
476         while ($e < 1) {\r
477             $e++;\r
478             $d = '0' . $d;\r
479         }\r
480         if ($e == length($d)) {\r
481             $result = $sign . $d;\r
482         } else {\r
483             $result = $sign . substr($d, 0, $e) . '.' . substr($d, $e);\r
484         }\r
485     } else {\r
486         die "Can't parse $_";\r
487     }\r
488     $result;\r
489 }\r
490 \r
491 #eof\r