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