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.UCharacterPerf';
17 # Methods to be tested. Each pair represents a test method and
18 # a baseline method which is used for comparison.
19 my @METHODS = (['JDKDigit', 'Digit'],
20 ['JDKGetNumericValue', 'GetNumericValue'],
21 ['JDKGetType', 'GetType'],
22 ['JDKIsDefined', 'IsDefined'],
23 ['JDKIsDigit', 'IsDigit'],
24 ['JDKIsIdentifierIgnorable', 'IsIdentifierIgnorable'],
25 ['JDKIsISOControl', 'IsISOControl'],
26 ['JDKIsLetter', 'IsLetter'],
27 ['JDKIsLetterOrDigit', 'IsLetterOrDigit'],
28 ['JDKIsLowerCase', 'IsLowerCase'],
29 ['JDKIsSpaceChar', 'IsSpaceChar'],
30 ['JDKIsTitleCase', 'IsTitleCase'],
31 ['JDKIsUnicodeIdentifierPart', 'IsUnicodeIdentifierPart'],
32 ['JDKIsUnicodeIdentifierStart', 'IsUnicodeIdentifierStart'],
33 ['JDKIsUpperCase', 'IsUpperCase'],
34 ['JDKIsWhiteSpace', 'IsWhiteSpace'],
37 # Patterns which define the set of characters used for testing.
38 my @PATTERNS = ('0 ffff');
40 my $CALIBRATE = 2; # duration in seconds for initial calibration
41 my $DURATION = 10; # duration in seconds for each pass
42 my $NUMPASSES = 4; # number of passes. If > 1 then the first pass
43 # is discarded as a JIT warm-up pass.
45 my $TABLEATTR = 'BORDER="1" CELLPADDING="4" CELLSPACING="0"';
47 my $PLUS_MINUS = "±";
50 die "Need at least 3 passes. One is discarded (JIT warmup) and need two to have 1 degree of freedom (t distribution).";
57 #---------------------------------------------------------------------
61 my $title = "ICU4J Performance Test $date";
64 $html =~ s/://g; # ':' illegal
65 $html =~ s/\s*\d+$//; # delete year
66 $html =~ s/^\w+\s*//; # delete dow
67 $html = "perf $html.html";
69 open(HTML,">$html") or die "Can't write to $html: $!";
72 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
73 "http://www.w3.org/TR/html4/strict.dtd">
80 print HTML "<H1>$title</H1>\n";
82 print HTML "<H2>$TESTCLASS</H2>\n";
86 for my $methodPair (@METHODS) {
88 my $testMethod = $methodPair->[0];
89 my $baselineMethod = $methodPair->[1];
91 print HTML "<P><TABLE $TABLEATTR><TR><TD>\n";
92 print HTML "<P><B>$testMethod vs. $baselineMethod</B></P>\n";
94 print HTML "<P><TABLE $TABLEATTR BGCOLOR=\"#CCFFFF\">\n";
95 print HTML "<TR><TD>Pattern</TD><TD>$testMethod</TD>";
96 print HTML "<TD>$baselineMethod</TD><TD>Ratio</TD></TR>\n";
100 for my $pat (@PATTERNS) {
101 print HTML "<TR><TD>$pat</TD>\n";
103 out("<P><TABLE $TABLEATTR WIDTH=\"100%\">");
105 # measure the test method
107 print "\n$testMethod $pat\n";
108 my $t = measure2($testMethod, $pat, -$DURATION);
110 print HTML "<TD>", formatSeconds(4, $t->getMean(), $t->getError);
111 print HTML "/event</TD>\n";
113 # measure baseline method
115 print "\nBegin $baselineMethod $pat\n";
116 my $b = measure2($baselineMethod, $pat, -$DURATION);
118 print HTML "<TD>", formatSeconds(4, $b->getMean(), $t->getError);
119 print HTML "/event</TD>\n";
124 my $r = $t->divide($b);
125 my $mean = $r->getMean() - 1;
126 my $color = $mean < 0 ? "RED" : "BLACK";
127 print HTML "<TD><B><FONT COLOR=\"$color\">", formatPercent(3, $mean, $r->getError);
128 print HTML "</FONT></B></TD></TR>\n";
131 print HTML "</TABLE></P>\n";
133 print HTML "<P>Raw data:</P>\n";
135 print HTML "</TABLE></P>\n";
142 close(HTML) or die "Can't close $html: $!";
145 #---------------------------------------------------------------------
146 # Append text to the global variable $OUT
148 $OUT .= join('', @_);
151 #---------------------------------------------------------------------
152 # Append text to the global variable $OUT
154 $OUT .= join('', @_) . "\n";
157 #---------------------------------------------------------------------
158 # Measure a given test method with a give test pattern using the
159 # global run parameters.
161 # @param the method to run
162 # @param the pattern defining characters to test
163 # @param if >0 then the number of iterations per pass. If <0 then
164 # (negative of) the number of seconds per pass.
166 # @return a Dataset object, scaled by iterations per pass and
167 # events per iteration, to give time per event
170 my @data = measure1(@_);
171 my $iterPerPass = shift(@data);
172 my $eventPerIter = shift(@data);
174 shift(@data) if (@data > 1); # discard first run
176 my $ds = Dataset->new(@data);
177 $ds->setScale(1.0e-3 / ($iterPerPass * $eventPerIter));
181 #---------------------------------------------------------------------
182 # Measure a given test method with a give test pattern using the
183 # global run parameters.
185 # @param the method to run
186 # @param the pattern defining characters to test
187 # @param if >0 then the number of iterations per pass. If <0 then
188 # (negative of) the number of seconds per pass.
191 # [0] iterations per pass
192 # [1] events per iteration
193 # [2..] ms reported for each pass, in order
198 my $iterCount = shift; # actually might be -seconds/pass
200 out("<P>Measuring $method using $pat, ");
201 if ($iterCount > 0) {
202 out("$iterCount iterations/pass, $NUMPASSES passes</P>\n");
204 out(-$iterCount, " seconds/pass, $NUMPASSES passes</P>\n");
207 # is $iterCount actually -seconds/pass?
208 if ($iterCount < 0) {
210 # calibrate: estimate ms/iteration
211 print "Calibrating...";
212 my @t = callJava($method, $pat, -$CALIBRATE, 1);
215 my @data = split(/\s+/, $t[0]->[2]);
218 my $timePerIter = 1.0e-3 * $data[0] / $data[1];
220 # determine iterations/pass
221 $iterCount = int(-$iterCount / $timePerIter + 0.5);
223 out("<P>Calibration pass ($CALIBRATE sec): ");
224 out("$data[0] ms, ");
225 out("$data[1] iterations = ");
226 out(formatSeconds(4, $timePerIter), "/iteration<BR>\n");
230 print "Measuring $iterCount iterations x $NUMPASSES passes...";
231 my @t = callJava($method, $pat, $iterCount, $NUMPASSES);
236 # $a->[0]: method name, corresponds to $method
237 # $a->[1]: 'begin' data, == $iterCount
238 # $a->[2]: 'end' data, of the form <ms> <loops> <eventsPerIter>
239 # $a->[3...]: gc messages from JVM during pass
240 @b = split(/\s+/, $a->[2]);
241 push(@ms, $b[0] * 1.0e+3);
243 my $eventsPerIter = $b[2];
245 out("Iterations per pass: $iterCount<BR>\n");
246 out("Events per iteration: $eventsPerIter<BR>\n");
249 $ms_str[0] .= " (discarded)" if (@ms_str > 1);
250 out("Raw times (ms/pass): ", join(", ", @ms_str), "<BR>\n");
252 ($iterCount, $eventsPerIter, @ms);
255 #---------------------------------------------------------------------
256 # Invoke java to run $TESTCLASS, passing it the given parameters.
258 # @param the method to run
259 # @param the number of iterations, or if negative, the duration
260 # in seconds. If more than on pass is desired, pass in
261 # a string, e.g., "100 100 100".
262 # @param the pattern defining characters to test, values in hex digits without 0x
264 # @return an array of results. Each result is an array REF
265 # describing one pass. The array REF contains:
266 # ->[0]: The method name as reported
267 # ->[1]: The params on the '= <meth> begin ...' line
268 # ->[2]: The params on the '= <meth> end ...' line
269 # ->[3..]: GC messages from the JVM, if any
277 my $n = ($n < 0) ? "-t ".(-$n) : "-i ".$n;
279 my $cmd = "java -cp classes $TESTCLASS $method $n -p $passes $pat";
280 print "[$cmd]\n"; # for debugging
281 open(PIPE, "$cmd|") or die "Can't run \"$cmd\"";
286 close(PIPE) or die "Java failed: \"$cmd\"";
288 @out = grep(!/^\#/, @out); # filter out comments
290 #print "[", join("\n", @out), "]\n";
298 if (/^=\s*(\w+)\s*(\w+)\s*(.*)/) {
299 my ($m, $state, $d) = ($1, $2, $3);
300 #print "$_ => [[$m $state $data]]\n";
301 if ($state eq 'begin') {
302 die "$method was begun but not finished" if ($method);
305 push(@$data, ''); # placeholder for end data
306 } elsif ($state eq 'end') {
308 die "$method end does not match: $_";
310 $data->[1] = $d; # insert end data at [1]
311 #print "#$method:", join(";",@$data), "\n";
312 unshift(@$data, $method); # add method to start
314 push(@results, $data);
318 die "Can't parse: $_";
326 # ignore extraneous GC notices
331 die "Can't parse: $_";
335 die "$method was begun but not finished" if ($method);
340 #|#---------------------------------------------------------------------
341 #|# Format a confidence interval, as given by a Dataset. Output is as
343 #|# 241.23 - 241.98 => 241.5 +/- 0.3
344 #|# 241.2 - 243.8 => 242 +/- 1
345 #|# 211.0 - 241.0 => 226 +/- 15 or? 230 +/- 20
346 #|# 220.3 - 234.3 => 227 +/- 7
347 #|# 220.3 - 300.3 => 260 +/- 40
348 #|# 220.3 - 1000 => 610 +/- 390 or? 600 +/- 400
349 #|# 0.022 - 0.024 => 0.023 +/- 0.001
350 #|# 0.022 - 0.032 => 0.027 +/- 0.005
351 #|# 0.022 - 1.000 => 0.5 +/- 0.5
352 #|# In other words, take one significant digit of the error value and
353 #|# display the mean to the same precision.
354 #|sub formatDataset {
356 #| my $lower = $ds->getMean() - $ds->getError();
357 #| my $upper = $ds->getMean() + $ds->getError();
359 #| # Find how many initial digits are the same
360 #| while ($lower < 1 ||
361 #| int($lower) == int($upper)) {
366 #| while ($lower >= 10 &&
367 #| int($lower) == int($upper)) {
374 #---------------------------------------------------------------------
375 # Format a number, optionally with a +/- delta, to n significant
378 # @param significant digit, a value >= 1
380 # @param time in seconds to be formatted
381 # @optional delta in seconds
383 # @return string of the form "23" or "23 +/- 10".
389 my $delta = shift; # may be undef
391 my $result = formatSigDig($sigdig, $a*$mult);
392 if (defined($delta)) {
393 my $d = formatSigDig($sigdig, $delta*$mult);
394 # restrict PRECISION of delta to that of main number
395 if ($result =~ /\.(\d+)/) {
396 # TODO make this work for values with all significant
397 # digits to the left of the decimal, e.g., 1234000.
399 # TODO the other thing wrong with this is that it
400 # isn't rounding the $delta properly. Have to put
401 # this logic into formatSigDig().
403 $d =~ s/\.(\d{$x})\d+/.$1/;
405 $result .= " $PLUS_MINUS " . $d;
410 #---------------------------------------------------------------------
411 # Format a time, optionally with a +/- delta, to n significant
414 # @param significant digit, a value >= 1
415 # @param time in seconds to be formatted
416 # @optional delta in seconds
418 # @return string of the form "23 ms" or "23 +/- 10 ms".
423 my $delta = shift; # may be undef
425 my @MULT = (1 , 1e3, 1e6, 1e9);
426 my @SUFF = ('s' , 'ms', 'us', 'ns');
428 # Determine our scale
430 ++$i while ($a*$MULT[$i] < 1 && $i < @MULT);
432 formatNumber($sigdig, $MULT[$i], $a, $delta) . ' ' . $SUFF[$i];
435 #---------------------------------------------------------------------
436 # Format a percentage, optionally with a +/- delta, to n significant
439 # @param significant digit, a value >= 1
440 # @param value to be formatted, as a fraction, e.g. 0.5 for 50%
441 # @optional delta, as a fraction
443 # @return string of the form "23 %" or "23 +/- 10 %".
448 my $delta = shift; # may be undef
450 formatNumber($sigdig, 100, $a, $delta) . ' %';
453 #---------------------------------------------------------------------
454 # Format a number to n significant digits without using exponential
457 # @param significant digit, a value >= 1
458 # @param number to be formatted
460 # @return string of the form "1234" "12.34" or "0.001234". If
461 # number was negative, prefixed by '-'.
467 local $_ = sprintf("%.${n}e", $a);
468 my $sign = (s/^-//) ? '-' : '';
472 if (/^(\d)\.(\d+)e([-+]\d+)$/) {
473 my ($d, $dn, $e) = ($1, $2, $3);
477 $d .= '0' while ($e > length($d));
482 if ($e == length($d)) {
483 $result = $sign . $d;
485 $result = $sign . substr($d, 0, $e) . '.' . substr($d, $e);
488 die "Can't parse $_";