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