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.ConverterPerformanceTest';
\r
17 # Methods to be tested. Each pair represents a test method and
\r
18 # a baseline method which is used for comparison.
\r
20 ## ['TestByteToCharConverter', 'TestByteToCharConverterICU'],
\r
21 ## ['TestCharToByteConverter', 'TestCharToByteConverterICU'],
\r
22 ['TestCharsetDecoder', 'TestCharsetDecoderICU'],
\r
23 ['TestCharsetEncoder', 'TestCharsetEncoderICU']
\r
26 # Patterns which define the set of characters used for testing.
\r
28 my $SOURCEDIR ="src/com/ibm/icu/dev/test/perf/data/conversion/";
\r
31 # src text src encoding test encoding
\r
32 [ "arabic.txt", "UTF-8", "csisolatinarabic"],
\r
33 [ "french.txt", "UTF-8", "csisolatin1"],
\r
34 [ "greek.txt", "UTF-8", "csisolatingreek"],
\r
35 [ "hebrew.txt", "UTF-8", "csisolatinhebrew"],
\r
36 # [ "hindi.txt" , "UTF-8", "iscii"],
\r
37 [ "japanese.txt", "UTF-8", "EUC-JP"],
\r
38 # [ "japanese.txt", "UTF-8", "csiso2022jp"],
\r
39 [ "japanese.txt", "UTF-8", "shift_jis"],
\r
40 # [ "korean.txt", "UTF-8", "csiso2022kr"],
\r
41 [ "korean.txt", "UTF-8", "EUC-KR"],
\r
42 [ "s-chinese.txt", "UTF-8", "EUC_CN"],
\r
43 [ "arabic.txt", "UTF-8", "UTF-8"],
\r
44 [ "french.txt", "UTF-8", "UTF-8"],
\r
45 [ "greek.txt", "UTF-8", "UTF-8"],
\r
46 [ "hebrew.txt", "UTF-8", "UTF-8"],
\r
47 [ "hindi.txt" , "UTF-8", "UTF-8"],
\r
48 [ "japanese.txt", "UTF-8", "UTF-8"],
\r
49 [ "korean.txt", "UTF-8", "UTF-8"],
\r
50 [ "s-chinese.txt", "UTF-8", "UTF-8"],
\r
51 [ "french.txt", "UTF-8", "UTF-16BE"],
\r
52 [ "french.txt", "UTF-8", "UTF-16LE"],
\r
53 [ "english.txt", "UTF-8", "US-ASCII"],
\r
56 my $CALIBRATE = 2; # duration in seconds for initial calibration
\r
57 my $DURATION = 10; # duration in seconds for each pass
\r
58 my $NUMPASSES = 4; # number of passes. If > 1 then the first pass
\r
59 # is discarded as a JIT warm-up pass.
\r
61 my $TABLEATTR = 'BORDER="1" CELLPADDING="4" CELLSPACING="0"';
\r
63 my $PLUS_MINUS = "±";
\r
65 if ($NUMPASSES < 3) {
\r
66 die "Need at least 3 passes. One is discarded (JIT warmup) and need two to have 1 degree of freedom (t distribution).";
\r
69 my $OUT; # see out()
\r
73 #---------------------------------------------------------------------
\r
76 my $date = localtime;
\r
77 my $title = "ICU4J Performance Test $date";
\r
80 $html =~ s/://g; # ':' illegal
\r
81 $html =~ s/\s*\d+$//; # delete year
\r
82 $html =~ s/^\w+\s*//; # delete dow
\r
83 $html = "perf $html.html";
\r
85 open(HTML,">$html") or die "Can't write to $html: $!";
\r
88 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
\r
89 "http://www.w3.org/TR/html4/strict.dtd">
\r
92 <TITLE>$title</TITLE>
\r
96 print HTML "<H1>$title</H1>\n";
\r
98 print HTML "<H2>$TESTCLASS</H2>\n";
\r
102 for my $methodPair (@METHODS) {
\r
104 my $testMethod = $methodPair->[0];
\r
105 my $baselineMethod = $methodPair->[1];
\r
107 print HTML "<P><TABLE $TABLEATTR><TR><TD>\n";
\r
108 print HTML "<P><B>$testMethod vs. $baselineMethod</B></P>\n";
\r
110 print HTML "<P><TABLE $TABLEATTR BGCOLOR=\"#CCFFFF\">\n";
\r
111 print HTML "<TR><TD>Options</TD><TD>$testMethod</TD>";
\r
112 print HTML "<TD>$baselineMethod</TD><TD>Ratio</TD></TR>\n";
\r
116 for my $pat (@OPTIONS) {
\r
117 print HTML "<TR><TD>@$pat[0], @$pat[2]</TD>\n";
\r
119 out("<P><TABLE $TABLEATTR WIDTH=\"100%\">");
\r
121 # measure the test method
\r
123 print "\n$testMethod [@$pat]\n";
\r
124 my $t = measure2($testMethod, $pat, -$DURATION);
\r
126 print HTML "<TD>", formatSeconds(4, $t->getMean(), $t->getError);
\r
127 print HTML "/event</TD>\n";
\r
129 # measure baseline method
\r
131 print "\n$baselineMethod [@$pat]\n";
\r
132 my $b = measure2($baselineMethod, $pat, -$DURATION);
\r
134 print HTML "<TD>", formatSeconds(4, $b->getMean(), $t->getError);
\r
135 print HTML "/event</TD>\n";
\r
137 out("</TABLE></P>");
\r
140 my $r = $t->divide($b);
\r
141 my $mean = $r->getMean() - 1;
\r
142 my $color = $mean < 0 ? "RED" : "BLACK";
\r
143 print HTML "<TD><B><FONT COLOR=\"$color\">", formatPercent(3, $mean, $r->getError);
\r
144 print HTML "</FONT></B></TD></TR>\n";
\r
147 print HTML "</TABLE></P>\n";
\r
149 print HTML "<P>Raw data:</P>\n";
\r
151 print HTML "</TABLE></P>\n";
\r
158 close(HTML) or die "Can't close $html: $!";
\r
161 #---------------------------------------------------------------------
\r
162 # Append text to the global variable $OUT
\r
164 $OUT .= join('', @_);
\r
167 #---------------------------------------------------------------------
\r
168 # Append text to the global variable $OUT
\r
170 $OUT .= join('', @_) . "\n";
\r
173 #---------------------------------------------------------------------
\r
174 # Measure a given test method with a give test pattern using the
\r
175 # global run parameters.
\r
177 # @param the method to run
\r
178 # @param the pattern defining characters to test
\r
179 # @param if >0 then the number of iterations per pass. If <0 then
\r
180 # (negative of) the number of seconds per pass.
\r
182 # @return a Dataset object, scaled by iterations per pass and
\r
183 # events per iteration, to give time per event
\r
186 my @data = measure1(@_);
\r
187 my $iterPerPass = shift(@data);
\r
188 my $eventPerIter = shift(@data);
\r
190 shift(@data) if (@data > 1); # discard first run
\r
192 my $ds = Dataset->new(@data);
\r
193 $ds->setScale(1.0e-3 / ($iterPerPass * $eventPerIter));
\r
197 #---------------------------------------------------------------------
\r
198 # Measure a given test method with a give test pattern using the
\r
199 # global run parameters.
\r
201 # @param the method to run
\r
202 # @param the pattern defining characters to test
\r
203 # @param if >0 then the number of iterations per pass. If <0 then
\r
204 # (negative of) the number of seconds per pass.
\r
206 # @return array of:
\r
207 # [0] iterations per pass
\r
208 # [1] events per iteration
\r
209 # [2..] ms reported for each pass, in order
\r
212 my $method = shift;
\r
214 my $iterCount = shift; # actually might be -seconds/pass
\r
216 out("<P>Measuring $method for input file @$pat[0] for encoding @$pat[2] , ");
\r
217 if ($iterCount > 0) {
\r
218 out("$iterCount iterations/pass, $NUMPASSES passes</P>\n");
\r
220 out(-$iterCount, " seconds/pass, $NUMPASSES passes</P>\n");
\r
223 # is $iterCount actually -seconds/pass?
\r
224 if ($iterCount < 0) {
\r
226 # calibrate: estimate ms/iteration
\r
227 print "Calibrating...";
\r
228 my @t = callJava($method, $pat, -$CALIBRATE, 1);
\r
231 my @data = split(/\s+/, $t[0]->[2]);
\r
232 $data[0] *= 1.0e+3;
\r
234 my $timePerIter = 1.0e-3 * $data[0] / $data[1];
\r
236 # determine iterations/pass
\r
237 $iterCount = int(-$iterCount / $timePerIter + 0.5);
\r
239 out("<P>Calibration pass ($CALIBRATE sec): ");
\r
240 out("$data[0] ms, ");
\r
241 out("$data[1] iterations = ");
\r
242 out(formatSeconds(4, $timePerIter), "/iteration<BR>\n");
\r
246 print "Measuring $iterCount iterations x $NUMPASSES passes...";
\r
247 my @t = callJava($method, $pat, $iterCount, $NUMPASSES);
\r
252 # $a->[0]: method name, corresponds to $method
\r
253 # $a->[1]: 'begin' data, == $iterCount
\r
254 # $a->[2]: 'end' data, of the form <ms> <loops> <eventsPerIter>
\r
255 # $a->[3...]: gc messages from JVM during pass
\r
256 @b = split(/\s+/, $a->[2]);
\r
257 push(@ms, $b[0] * 1.0e+3);
\r
259 my $eventsPerIter = $b[2];
\r
261 out("Iterations per pass: $iterCount<BR>\n");
\r
262 out("Events per iteration: $eventsPerIter<BR>\n");
\r
265 $ms_str[0] .= " (discarded)" if (@ms_str > 1);
\r
266 out("Raw times (ms/pass): ", join(", ", @ms_str), "<BR>\n");
\r
268 ($iterCount, $eventsPerIter, @ms);
\r
271 #---------------------------------------------------------------------
\r
272 # Invoke java to run $TESTCLASS, passing it the given parameters.
\r
274 # @param the method to run
\r
275 # @param the number of iterations, or if negative, the duration
\r
276 # in seconds. If more than on pass is desired, pass in
\r
277 # a string, e.g., "100 100 100".
\r
278 # @param the pattern defining characters to test
\r
280 # @return an array of results. Each result is an array REF
\r
281 # describing one pass. The array REF contains:
\r
282 # ->[0]: The method name as reported
\r
283 # ->[1]: The params on the '= <meth> begin ...' line
\r
284 # ->[2]: The params on the '= <meth> end ...' line
\r
285 # ->[3..]: GC messages from the JVM, if any
\r
288 my $method = shift;
\r
291 my $passes = shift;
\r
293 my $fileName = $SOURCEDIR.@$pat[0] ;
\r
294 my $n = ($n < 0) ? "-t ".(-$n) : "-i ".$n;
\r
296 my $cmd = "java -classpath classes $TESTCLASS $method $n -p $passes -f $fileName -e @$pat[1] -T @$pat[2]";
\r
297 print "[$cmd]\n"; # for debugging
\r
298 open(PIPE, "$cmd|") or die "Can't run \"$cmd\"";
\r
303 close(PIPE) or die "Java failed: \"$cmd\"";
\r
305 @out = grep(!/^\#/, @out); # filter out comments
\r
307 #print "[", join("\n", @out), "]\n";
\r
313 next unless (/\S/);
\r
315 if (/^=\s*(\w+)\s*(\w+)\s*(.*)/) {
\r
316 my ($m, $state, $d) = ($1, $2, $3);
\r
317 #print "$_ => [[$m $state $data]]\n";
\r
318 if ($state eq 'begin') {
\r
319 die "$method was begun but not finished" if ($method);
\r
322 push(@$data, ''); # placeholder for end data
\r
323 } elsif ($state eq 'end') {
\r
324 if ($m ne $method) {
\r
325 die "$method end does not match: $_";
\r
327 $data->[1] = $d; # insert end data at [1]
\r
328 #print "#$method:", join(";",@$data), "\n";
\r
329 unshift(@$data, $method); # add method to start
\r
331 push(@results, $data);
\r
335 die "Can't parse: $_";
\r
343 # ignore extraneous GC notices
\r
348 die "Can't parse: $_";
\r
352 die "$method was begun but not finished" if ($method);
\r
357 #|#---------------------------------------------------------------------
\r
358 #|# Format a confidence interval, as given by a Dataset. Output is as
\r
360 #|# 241.23 - 241.98 => 241.5 +/- 0.3
\r
361 #|# 241.2 - 243.8 => 242 +/- 1
\r
362 #|# 211.0 - 241.0 => 226 +/- 15 or? 230 +/- 20
\r
363 #|# 220.3 - 234.3 => 227 +/- 7
\r
364 #|# 220.3 - 300.3 => 260 +/- 40
\r
365 #|# 220.3 - 1000 => 610 +/- 390 or? 600 +/- 400
\r
366 #|# 0.022 - 0.024 => 0.023 +/- 0.001
\r
367 #|# 0.022 - 0.032 => 0.027 +/- 0.005
\r
368 #|# 0.022 - 1.000 => 0.5 +/- 0.5
\r
369 #|# In other words, take one significant digit of the error value and
\r
370 #|# display the mean to the same precision.
\r
371 #|sub formatDataset {
\r
373 #| my $lower = $ds->getMean() - $ds->getError();
\r
374 #| my $upper = $ds->getMean() + $ds->getError();
\r
376 #| # Find how many initial digits are the same
\r
377 #| while ($lower < 1 ||
\r
378 #| int($lower) == int($upper)) {
\r
383 #| while ($lower >= 10 &&
\r
384 #| int($lower) == int($upper)) {
\r
391 #---------------------------------------------------------------------
\r
392 # Format a number, optionally with a +/- delta, to n significant
\r
395 # @param significant digit, a value >= 1
\r
396 # @param multiplier
\r
397 # @param time in seconds to be formatted
\r
398 # @optional delta in seconds
\r
400 # @return string of the form "23" or "23 +/- 10".
\r
403 my $sigdig = shift;
\r
406 my $delta = shift; # may be undef
\r
408 my $result = formatSigDig($sigdig, $a*$mult);
\r
409 if (defined($delta)) {
\r
410 my $d = formatSigDig($sigdig, $delta*$mult);
\r
411 # restrict PRECISION of delta to that of main number
\r
412 if ($result =~ /\.(\d+)/) {
\r
413 # TODO make this work for values with all significant
\r
414 # digits to the left of the decimal, e.g., 1234000.
\r
416 # TODO the other thing wrong with this is that it
\r
417 # isn't rounding the $delta properly. Have to put
\r
418 # this logic into formatSigDig().
\r
419 my $x = length($1);
\r
420 $d =~ s/\.(\d{$x})\d+/.$1/;
\r
422 $result .= " $PLUS_MINUS " . $d;
\r
427 #---------------------------------------------------------------------
\r
428 # Format a time, optionally with a +/- delta, to n significant
\r
431 # @param significant digit, a value >= 1
\r
432 # @param time in seconds to be formatted
\r
433 # @optional delta in seconds
\r
435 # @return string of the form "23 ms" or "23 +/- 10 ms".
\r
437 sub formatSeconds {
\r
438 my $sigdig = shift;
\r
440 my $delta = shift; # may be undef
\r
442 my @MULT = (1 , 1e3, 1e6, 1e9);
\r
443 my @SUFF = ('s' , 'ms', 'us', 'ns');
\r
445 # Determine our scale
\r
447 ++$i while ($a*$MULT[$i] < 1 && $i < @MULT);
\r
449 formatNumber($sigdig, $MULT[$i], $a, $delta) . ' ' . $SUFF[$i];
\r
452 #---------------------------------------------------------------------
\r
453 # Format a percentage, optionally with a +/- delta, to n significant
\r
456 # @param significant digit, a value >= 1
\r
457 # @param value to be formatted, as a fraction, e.g. 0.5 for 50%
\r
458 # @optional delta, as a fraction
\r
460 # @return string of the form "23 %" or "23 +/- 10 %".
\r
462 sub formatPercent {
\r
463 my $sigdig = shift;
\r
465 my $delta = shift; # may be undef
\r
467 formatNumber($sigdig, 100, $a, $delta) . ' %';
\r
470 #---------------------------------------------------------------------
\r
471 # Format a number to n significant digits without using exponential
\r
474 # @param significant digit, a value >= 1
\r
475 # @param number to be formatted
\r
477 # @return string of the form "1234" "12.34" or "0.001234". If
\r
478 # number was negative, prefixed by '-'.
\r
481 my $n = shift() - 1;
\r
484 local $_ = sprintf("%.${n}e", $a);
\r
485 my $sign = (s/^-//) ? '-' : '';
\r
489 if (/^(\d)\.(\d+)e([-+]\d+)$/) {
\r
490 my ($d, $dn, $e) = ($1, $2, $3);
\r
494 $d .= '0' while ($e > length($d));
\r
499 if ($e == length($d)) {
\r
500 $result = $sign . $d;
\r
502 $result = $sign . substr($d, 0, $e) . '.' . substr($d, $e);
\r
505 die "Can't parse $_";
\r