1 #!/usr/local/bin/perl 2 # ******************************************************************************* 3 # * Copyright (C) 2002-2007 International Business Machines Corporation and * 4 # * others. All Rights Reserved. * 5 # ******************************************************************************* 6 7 use strict; 8 9 # Assume we are running within the icu4j root directory 10 use lib 'src/com/ibm/icu/dev/test/perf'; 11 use Dataset; 12 13 #--------------------------------------------------------------------- 14 # Test class 15 my $TESTCLASS = 'com.ibm.icu.dev.test.perf.DateFormatPerformanceTest'; 16 17 # Methods to be tested. Each pair represents a test method and 18 # a baseline method which is used for comparison. 19 my @METHODS = ( 20 ['TestJDKConstruction', 'TestICUConstruction'], 21 ['TestJDKParse', 'TestICUParse'], 22 ['TestJDKFormat', 'TestICUFormat'] 23 ); 24 # Patterns which define the set of characters used for testing. 25 my @OPTIONS = ( 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"], 33 ); 34 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. 40 41 my $TABLEATTR = 'BORDER="1" CELLPADDING="4" CELLSPACING="0"'; 42 43 my $PLUS_MINUS = "±"; 44 45 if ($NUMPASSES < 3) { 46 die "Need at least 3 passes. One is discarded (JIT warmup) and need two to have 1 degree of freedom (t distribution)."; 47 } 48 49 my $OUT; # see out() 50 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") { 54 $THREADS = $arg; 55 main(); 56 } 57 58 59 #--------------------------------------------------------------------- 60 # ... 61 sub main { 62 my $date = localtime; 63 my $threads = ($THREADS > 1) ? "($THREADS threads)" : ""; 64 my $title = "ICU4J Performance Test $threads $date"; 65 66 my $html = $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"; 71 72 open(HTML,">$html") or die "Can't write to $html: $!"; 73 74 print HTML <<EOF; 75 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 76 "http://www.w3.org/TR/html4/strict.dtd"> 77 <HTML> 78 <HEAD> 79 <TITLE>$title</TITLE> 80 </HEAD> 81 <BODY> 82 EOF 83 print HTML "<H1>$title</H1>\n"; 84 85 print HTML "<H2>$TESTCLASS</H2>\n"; 86 87 my $raw = ""; 88 89 for my $methodPair (@METHODS) { 90 91 my $testMethod = $methodPair->[0]; 92 my $baselineMethod = $methodPair->[1]; 93 94 print HTML "<P><TABLE $TABLEATTR><TR><TD>\n"; 95 print HTML "<P><B>$testMethod vs. $baselineMethod</B></P>\n"; 96 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"; 100 101 $OUT = ''; 102 103 for my $pat (@OPTIONS) { 104 print HTML "<TR><TD>@$pat[0], \"@$pat[1]\", \"@$pat[2]\"</TD>\n"; 105 106 out("<P><TABLE $TABLEATTR WIDTH=\"100%\">"); 107 108 # measure the test method 109 out("<TR><TD>"); 110 print "\n$testMethod [@$pat]\n"; 111 my $t = measure2($testMethod, $pat, -$DURATION); 112 out("</TD></TR>"); 113 print HTML "<TD>", formatSeconds(4, $t->getMean(), $t->getError); 114 print HTML "/event</TD>\n"; 115 116 # measure baseline method 117 out("<TR><TD>"); 118 print "\n$baselineMethod [@$pat]\n"; 119 my $b = measure2($baselineMethod, $pat, -$DURATION); 120 out("</TD></TR>"); 121 print HTML "<TD>", formatSeconds(4, $b->getMean(), $t->getError); 122 print HTML "/event</TD>\n"; 123 124 out("</TABLE></P>"); 125 126 # output ratio 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"; 132 } 133 134 print HTML "</TABLE></P>\n"; 135 136 print HTML "<P>Raw data:</P>\n"; 137 print HTML $OUT; 138 print HTML "</TABLE></P>\n"; 139 } 140 141 print HTML <<EOF; 142 </BODY> 143 </HTML> 144 EOF 145 close(HTML) or die "Can't close $html: $!"; 146 } 147 148 #--------------------------------------------------------------------- 149 # Append text to the global variable $OUT 150 sub out { 151 $OUT .= join('', @_); 152 } 153 154 #--------------------------------------------------------------------- 155 # Append text to the global variable $OUT 156 sub outln { 157 $OUT .= join('', @_) . "\n"; 158 } 159 160 #--------------------------------------------------------------------- 161 # Measure a given test method with a give test pattern using the 162 # global run parameters. 163 # 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. 168 # 169 # @return a Dataset object, scaled by iterations per pass and 170 # events per iteration, to give time per event 171 # 172 sub measure2 { 173 my @data = measure1(@_); 174 my $iterPerPass = shift(@data); 175 my $eventPerIter = shift(@data); 176 177 shift(@data) if (@data > 1); # discard first run 178 179 my $ds = Dataset->new(@data); 180 $ds->setScale(1.0e-3 / ($iterPerPass * $eventPerIter)); 181 $ds; 182 } 183 184 #--------------------------------------------------------------------- 185 # Measure a given test method with a give test pattern using the 186 # global run parameters. 187 # 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. 192 # 193 # @return array of: 194 # [0] iterations per pass 195 # [1] events per iteration 196 # [2..] ms reported for each pass, in order 197 # 198 sub measure1 { 199 my $method = shift; 200 my $pat = shift; 201 my $iterCount = shift; # actually might be -seconds/pass 202 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"); 206 } else { 207 out(-$iterCount, " seconds/pass, $NUMPASSES passes</P>\n"); 208 } 209 210 # is $iterCount actually -seconds/pass? 211 if ($iterCount < 0) { 212 213 # calibrate: estimate ms/iteration 214 print "Calibrating..."; 215 my @t = callJava($method, $pat, -$CALIBRATE, 1); 216 print "done.\n"; 217 218 my @data = split(/\s+/, $t[0]->[2]); 219 $data[0] *= 1.0e+3; 220 221 my $timePerIter = 1.0e-3 * $data[0] / $data[1]; 222 223 # determine iterations/pass 224 $iterCount = int(-$iterCount / $timePerIter + 0.5); 225 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"); 230 } 231 232 # run passes 233 print "Measuring $iterCount iterations x $NUMPASSES passes..."; 234 my @t = callJava($method, $pat, $iterCount, $NUMPASSES); 235 print "done.\n"; 236 my @ms = (); 237 my @b; # scratch 238 for my $a (@t) { 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); 245 } 246 my $eventsPerIter = $b[2]; 247 248 out("Iterations per pass: $iterCount<BR>\n"); 249 out("Events per iteration: $eventsPerIter<BR>\n"); 250 251 my @ms_str = @ms; 252 $ms_str[0] .= " (discarded)" if (@ms_str > 1); 253 out("Raw times (ms/pass): ", join(", ", @ms_str), "<BR>\n"); 254 255 ($iterCount, $eventsPerIter, @ms); 256 } 257 258 #--------------------------------------------------------------------- 259 # Invoke java to run $TESTCLASS, passing it the given parameters. 260 # 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 266 # 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 273 # 274 sub callJava { 275 my $method = shift; 276 my $pat = shift; 277 my $n = shift; 278 my $passes = shift; 279 280 my $n = ($n < 0) ? "-t ".(-$n) : "-i ".$n; 281 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\""; 285 my @out; 286 while (<PIPE>) { 287 push(@out, $_); 288 } 289 close(PIPE) or die "Java failed: \"$cmd\""; 290 291 @out = grep(!/^\#/, @out); # filter out comments 292 293 #print "[", join("\n", @out), "]\n"; 294 295 my @results; 296 my $method = ''; 297 my $data = []; 298 foreach (@out) { 299 next unless (/\S/); 300 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); 306 $method = $m; 307 push(@$data, $d); 308 push(@$data, ''); # placeholder for end data 309 } elsif ($state eq 'end') { 310 if ($m ne $method) { 311 die "$method end does not match: $_"; 312 } 313 $data->[1] = $d; # insert end data at [1] 314 #print "#$method:", join(";",@$data), "\n"; 315 unshift(@$data, $method); # add method to start 316 317 push(@results, $data); 318 $method = ''; 319 $data = []; 320 } else { 321 die "Can't parse: $_"; 322 } 323 } 324 325 elsif (/^\[/) { 326 if ($method) { 327 push(@$data, $_); 328 } else { 329 # ignore extraneous GC notices 330 } 331 } 332 333 else { 334 die "Can't parse: $_"; 335 } 336 } 337 338 die "$method was begun but not finished" if ($method); 339 340 @results; 341 } 342 343 #|#--------------------------------------------------------------------- 344 #|# Format a confidence interval, as given by a Dataset. Output is as 345 #|# as follows: 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 { 358 #| my $ds = shift; 359 #| my $lower = $ds->getMean() - $ds->getError(); 360 #| my $upper = $ds->getMean() + $ds->getError(); 361 #| my $scale = 0; 362 #| # Find how many initial digits are the same 363 #| while ($lower < 1 || 364 #| int($lower) == int($upper)) { 365 #| $lower *= 10; 366 #| $upper *= 10; 367 #| $scale++; 368 #| } 369 #| while ($lower >= 10 && 370 #| int($lower) == int($upper)) { 371 #| $lower /= 10; 372 #| $upper /= 10; 373 #| $scale--; 374 #| } 375 #|} 376 377 #--------------------------------------------------------------------- 378 # Format a number, optionally with a +/- delta, to n significant 379 # digits. 380 # 381 # @param significant digit, a value >= 1 382 # @param multiplier 383 # @param time in seconds to be formatted 384 # @optional delta in seconds 385 # 386 # @return string of the form "23" or "23 +/- 10". 387 # 388 sub formatNumber { 389 my $sigdig = shift; 390 my $mult = shift; 391 my $a = shift; 392 my $delta = shift; # may be undef 393 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. 401 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(). 405 my $x = length($1); 406 $d =~ s/\.(\d{$x})\d+/.$1/; 407 } 408 $result .= " $PLUS_MINUS " . $d; 409 } 410 $result; 411 } 412 413 #--------------------------------------------------------------------- 414 # Format a time, optionally with a +/- delta, to n significant 415 # digits. 416 # 417 # @param significant digit, a value >= 1 418 # @param time in seconds to be formatted 419 # @optional delta in seconds 420 # 421 # @return string of the form "23 ms" or "23 +/- 10 ms". 422 # 423 sub formatSeconds { 424 my $sigdig = shift; 425 my $a = shift; 426 my $delta = shift; # may be undef 427 428 my @MULT = (1 , 1e3, 1e6, 1e9); 429 my @SUFF = ('s' , 'ms', 'us', 'ns'); 430 431 # Determine our scale 432 my $i = 0; 433 ++$i while ($a*$MULT[$i] < 1 && $i < @MULT); 434 435 formatNumber($sigdig, $MULT[$i], $a, $delta) . ' ' . $SUFF[$i]; 436 } 437 438 #--------------------------------------------------------------------- 439 # Format a percentage, optionally with a +/- delta, to n significant 440 # digits. 441 # 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 445 # 446 # @return string of the form "23 %" or "23 +/- 10 %". 447 # 448 sub formatPercent { 449 my $sigdig = shift; 450 my $a = shift; 451 my $delta = shift; # may be undef 452 453 formatNumber($sigdig, 100, $a, $delta) . ' %'; 454 } 455 456 #--------------------------------------------------------------------- 457 # Format a number to n significant digits without using exponential 458 # notation. 459 # 460 # @param significant digit, a value >= 1 461 # @param number to be formatted 462 # 463 # @return string of the form "1234" "12.34" or "0.001234". If 464 # number was negative, prefixed by '-'. 465 # 466 sub formatSigDig { 467 my $n = shift() - 1; 468 my $a = shift; 469 470 local $_ = sprintf("%.${n}e", $a); 471 my $sign = (s/^-//) ? '-' : ''; 472 473 my $a_e; 474 my $result; 475 if (/^(\d)\.(\d+)e([-+]\d+)$/) { 476 my ($d, $dn, $e) = ($1, $2, $3); 477 $a_e = $e; 478 $d .= $dn; 479 $e++; 480 $d .= '0' while ($e > length($d)); 481 while ($e < 1) { 482 $e++; 483 $d = '0' . $d; 484 } 485 if ($e == length($d)) { 486 $result = $sign . $d; 487 } else { 488 $result = $sign . substr($d, 0, $e) . '.' . substr($d, $e); 489 } 490 } else { 491 die "Can't parse $_"; 492 } 493 $result; 494 } 495 496 #eof 497