2 # *******************************************************************************
3 # * Copyright (C) 2002-2007 International Business Machines Corporation and *
4 # * others. All Rights Reserved. *
5 # *******************************************************************************
9 # Assume we are running within the icu4j root directory
10 use lib 'src/com/ibm/icu/dev/test/perf';
13 #---------------------------------------------------------------------
15 my $TESTCLASS = 'com.ibm.icu.dev.test.perf.DateFormatPerformanceTest';
17 # Methods to be tested. Each pair represents a test method and
18 # a baseline method which is used for comparison.
20 ['TestJDKConstruction', 'TestICUConstruction'],
21 ['TestJDKParse', 'TestICUParse'],
22 ['TestJDKFormat', 'TestICUFormat']
24 # Patterns which define the set of characters used for testing.
26 # locale pattern date string
27 [ "en_US", "dddd MMM yyyy", "15 Jan 2007"],
28 [ "sw_KE", "dddd MMM yyyy", "15 Jan 2007"],
29 [ "en_US", "HH:mm", "13:13"],
30 [ "en_US", "HH:mm zzzz", "13:13 Pacific Standard Time"],
31 [ "en_US", "HH:mm z", "13:13 PST"],
32 [ "en_US", "HH:mm Z", "13:13 -0800"],
35 my $THREADS; # number of threads (input from command-line args)
36 my $CALIBRATE = 2; # duration in seconds for initial calibration
37 my $DURATION = 10; # duration in seconds for each pass
38 my $NUMPASSES = 4; # number of passes. If > 1 then the first pass
39 # is discarded as a JIT warm-up pass.
41 my $TABLEATTR = 'BORDER="1" CELLPADDING="4" CELLSPACING="0"';
43 my $PLUS_MINUS = "±";
46 die "Need at least 3 passes. One is discarded (JIT warmup) and need two to have 1 degree of freedom (t distribution).";
51 # run all tests with the specified number of threads from command-line input
52 # (if there is no arguments, use $THREADS = 1)
53 foreach my $arg ($#ARGV >= 0 ? @ARGV : "1") {
59 #---------------------------------------------------------------------
63 my $threads = ($THREADS > 1) ? "($THREADS threads)" : "";
64 my $title = "ICU4J Performance Test $threads $date";
67 $html =~ s/://g; # ':' illegal
68 $html =~ s/\s*\d+$//; # delete year
69 $html =~ s/^\w+\s*//; # delete dow
70 $html = "perf $html.html";
72 open(HTML,">$html") or die "Can't write to $html: $!";
75 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
76 "http://www.w3.org/TR/html4/strict.dtd">
83 print HTML "<H1>$title</H1>\n";
85 print HTML "<H2>$TESTCLASS</H2>\n";
89 for my $methodPair (@METHODS) {
91 my $testMethod = $methodPair->[0];
92 my $baselineMethod = $methodPair->[1];
94 print HTML "<P><TABLE $TABLEATTR><TR><TD>\n";
95 print HTML "<P><B>$testMethod vs. $baselineMethod</B></P>\n";
97 print HTML "<P><TABLE $TABLEATTR BGCOLOR=\"#CCFFFF\">\n";
98 print HTML "<TR><TD>Options</TD><TD>$testMethod</TD>";
99 print HTML "<TD>$baselineMethod</TD><TD>Ratio</TD></TR>\n";
103 for my $pat (@OPTIONS) {
104 print HTML "<TR><TD>@$pat[0], \"@$pat[1]\", \"@$pat[2]\"</TD>\n";
106 out("<P><TABLE $TABLEATTR WIDTH=\"100%\">");
108 # measure the test method
110 print "\n$testMethod [@$pat]\n";
111 my $t = measure2($testMethod, $pat, -$DURATION);
113 print HTML "<TD>", formatSeconds(4, $t->getMean(), $t->getError);
114 print HTML "/event</TD>\n";
116 # measure baseline method
118 print "\n$baselineMethod [@$pat]\n";
119 my $b = measure2($baselineMethod, $pat, -$DURATION);
121 print HTML "<TD>", formatSeconds(4, $b->getMean(), $t->getError);
122 print HTML "/event</TD>\n";
127 my $r = $t->divide($b);
128 my $mean = $r->getMean() - 1;
129 my $color = $mean < 0 ? "RED" : "BLACK";
130 print HTML "<TD><B><FONT COLOR=\"$color\">", formatPercent(3, $mean, $r->getError);
131 print HTML "</FONT></B></TD></TR>\n";
134 print HTML "</TABLE></P>\n";
136 print HTML "<P>Raw data:</P>\n";
138 print HTML "</TABLE></P>\n";
145 close(HTML) or die "Can't close $html: $!";
148 #---------------------------------------------------------------------
149 # Append text to the global variable $OUT
151 $OUT .= join('', @_);
154 #---------------------------------------------------------------------
155 # Append text to the global variable $OUT
157 $OUT .= join('', @_) . "\n";
160 #---------------------------------------------------------------------
161 # Measure a given test method with a give test pattern using the
162 # global run parameters.
164 # @param the method to run
165 # @param the pattern defining characters to test
166 # @param if >0 then the number of iterations per pass. If <0 then
167 # (negative of) the number of seconds per pass.
169 # @return a Dataset object, scaled by iterations per pass and
170 # events per iteration, to give time per event
173 my @data = measure1(@_);
174 my $iterPerPass = shift(@data);
175 my $eventPerIter = shift(@data);
177 shift(@data) if (@data > 1); # discard first run
179 my $ds = Dataset->new(@data);
180 $ds->setScale(1.0e-3 / ($iterPerPass * $eventPerIter));
184 #---------------------------------------------------------------------
185 # Measure a given test method with a give test pattern using the
186 # global run parameters.
188 # @param the method to run
189 # @param the pattern defining characters to test
190 # @param if >0 then the number of iterations per pass. If <0 then
191 # (negative of) the number of seconds per pass.
194 # [0] iterations per pass
195 # [1] events per iteration
196 # [2..] ms reported for each pass, in order
201 my $iterCount = shift; # actually might be -seconds/pass
203 out("<P>Measuring $method for input file @$pat[0] for encoding @$pat[2] , ");
204 if ($iterCount > 0) {
205 out("$iterCount iterations/pass, $NUMPASSES passes</P>\n");
207 out(-$iterCount, " seconds/pass, $NUMPASSES passes</P>\n");
210 # is $iterCount actually -seconds/pass?
211 if ($iterCount < 0) {
213 # calibrate: estimate ms/iteration
214 print "Calibrating...";
215 my @t = callJava($method, $pat, -$CALIBRATE, 1);
218 my @data = split(/\s+/, $t[0]->[2]);
221 my $timePerIter = 1.0e-3 * $data[0] / $data[1];
223 # determine iterations/pass
224 $iterCount = int(-$iterCount / $timePerIter + 0.5);
226 out("<P>Calibration pass ($CALIBRATE sec): ");
227 out("$data[0] ms, ");
228 out("$data[1] iterations = ");
229 out(formatSeconds(4, $timePerIter), "/iteration<BR>\n");
233 print "Measuring $iterCount iterations x $NUMPASSES passes...";
234 my @t = callJava($method, $pat, $iterCount, $NUMPASSES);
239 # $a->[0]: method name, corresponds to $method
240 # $a->[1]: 'begin' data, == $iterCount
241 # $a->[2]: 'end' data, of the form <ms> <loops> <eventsPerIter>
242 # $a->[3...]: gc messages from JVM during pass
243 @b = split(/\s+/, $a->[2]);
244 push(@ms, $b[0] * 1.0e+3);
246 my $eventsPerIter = $b[2];
248 out("Iterations per pass: $iterCount<BR>\n");
249 out("Events per iteration: $eventsPerIter<BR>\n");
252 $ms_str[0] .= " (discarded)" if (@ms_str > 1);
253 out("Raw times (ms/pass): ", join(", ", @ms_str), "<BR>\n");
255 ($iterCount, $eventsPerIter, @ms);
258 #---------------------------------------------------------------------
259 # Invoke java to run $TESTCLASS, passing it the given parameters.
261 # @param the method to run
262 # @param the number of iterations, or if negative, the duration
263 # in seconds. If more than on pass is desired, pass in
264 # a string, e.g., "100 100 100".
265 # @param the pattern defining characters to test
267 # @return an array of results. Each result is an array REF
268 # describing one pass. The array REF contains:
269 # ->[0]: The method name as reported
270 # ->[1]: The params on the '= <meth> begin ...' line
271 # ->[2]: The params on the '= <meth> end ...' line
272 # ->[3..]: GC messages from the JVM, if any
280 my $n = ($n < 0) ? "-t ".(-$n) : "-i ".$n;
282 my $cmd = "java -classpath classes $TESTCLASS $method $n -p $passes -L @$pat[0] \"@$pat[1]\" \"@$pat[2]\" -r $THREADS";
283 print "[$cmd]\n"; # for debugging
284 open(PIPE, "$cmd|") or die "Can't run \"$cmd\"";
289 close(PIPE) or die "Java failed: \"$cmd\"";
291 @out = grep(!/^\#/, @out); # filter out comments
293 #print "[", join("\n", @out), "]\n";
301 if (/^=\s*(\w+)\s*(\w+)\s*(.*)/) {
302 my ($m, $state, $d) = ($1, $2, $3);
303 #print "$_ => [[$m $state $data]]\n";
304 if ($state eq 'begin') {
305 die "$method was begun but not finished" if ($method);
308 push(@$data, ''); # placeholder for end data
309 } elsif ($state eq 'end') {
311 die "$method end does not match: $_";
313 $data->[1] = $d; # insert end data at [1]
314 #print "#$method:", join(";",@$data), "\n";
315 unshift(@$data, $method); # add method to start
317 push(@results, $data);
321 die "Can't parse: $_";
329 # ignore extraneous GC notices
334 die "Can't parse: $_";
338 die "$method was begun but not finished" if ($method);
343 #|#---------------------------------------------------------------------
344 #|# Format a confidence interval, as given by a Dataset. Output is as
346 #|# 241.23 - 241.98 => 241.5 +/- 0.3
347 #|# 241.2 - 243.8 => 242 +/- 1
348 #|# 211.0 - 241.0 => 226 +/- 15 or? 230 +/- 20
349 #|# 220.3 - 234.3 => 227 +/- 7
350 #|# 220.3 - 300.3 => 260 +/- 40
351 #|# 220.3 - 1000 => 610 +/- 390 or? 600 +/- 400
352 #|# 0.022 - 0.024 => 0.023 +/- 0.001
353 #|# 0.022 - 0.032 => 0.027 +/- 0.005
354 #|# 0.022 - 1.000 => 0.5 +/- 0.5
355 #|# In other words, take one significant digit of the error value and
356 #|# display the mean to the same precision.
357 #|sub formatDataset {
359 #| my $lower = $ds->getMean() - $ds->getError();
360 #| my $upper = $ds->getMean() + $ds->getError();
362 #| # Find how many initial digits are the same
363 #| while ($lower < 1 ||
364 #| int($lower) == int($upper)) {
369 #| while ($lower >= 10 &&
370 #| int($lower) == int($upper)) {
377 #---------------------------------------------------------------------
378 # Format a number, optionally with a +/- delta, to n significant
381 # @param significant digit, a value >= 1
383 # @param time in seconds to be formatted
384 # @optional delta in seconds
386 # @return string of the form "23" or "23 +/- 10".
392 my $delta = shift; # may be undef
394 my $result = formatSigDig($sigdig, $a*$mult);
395 if (defined($delta)) {
396 my $d = formatSigDig($sigdig, $delta*$mult);
397 # restrict PRECISION of delta to that of main number
398 if ($result =~ /\.(\d+)/) {
399 # TODO make this work for values with all significant
400 # digits to the left of the decimal, e.g., 1234000.
402 # TODO the other thing wrong with this is that it
403 # isn't rounding the $delta properly. Have to put
404 # this logic into formatSigDig().
406 $d =~ s/\.(\d{$x})\d+/.$1/;
408 $result .= " $PLUS_MINUS " . $d;
413 #---------------------------------------------------------------------
414 # Format a time, optionally with a +/- delta, to n significant
417 # @param significant digit, a value >= 1
418 # @param time in seconds to be formatted
419 # @optional delta in seconds
421 # @return string of the form "23 ms" or "23 +/- 10 ms".
426 my $delta = shift; # may be undef
428 my @MULT = (1 , 1e3, 1e6, 1e9);
429 my @SUFF = ('s' , 'ms', 'us', 'ns');
431 # Determine our scale
433 ++$i while ($a*$MULT[$i] < 1 && $i < @MULT);
435 formatNumber($sigdig, $MULT[$i], $a, $delta) . ' ' . $SUFF[$i];
438 #---------------------------------------------------------------------
439 # Format a percentage, optionally with a +/- delta, to n significant
442 # @param significant digit, a value >= 1
443 # @param value to be formatted, as a fraction, e.g. 0.5 for 50%
444 # @optional delta, as a fraction
446 # @return string of the form "23 %" or "23 +/- 10 %".
451 my $delta = shift; # may be undef
453 formatNumber($sigdig, 100, $a, $delta) . ' %';
456 #---------------------------------------------------------------------
457 # Format a number to n significant digits without using exponential
460 # @param significant digit, a value >= 1
461 # @param number to be formatted
463 # @return string of the form "1234" "12.34" or "0.001234". If
464 # number was negative, prefixed by '-'.
470 local $_ = sprintf("%.${n}e", $a);
471 my $sign = (s/^-//) ? '-' : '';
475 if (/^(\d)\.(\d+)e([-+]\d+)$/) {
476 my ($d, $dn, $e) = ($1, $2, $3);
480 $d .= '0' while ($e > length($d));
485 if ($e == length($d)) {
486 $result = $sign . $d;
488 $result = $sign . substr($d, 0, $e) . '.' . substr($d, $e);
491 die "Can't parse $_";