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