summaryrefslogtreecommitdiff
path: root/Build/source/libs/icu/icu-src/source/test/perf/perldriver/Output.pm
blob: d7b8837e3b8d5e1fc8a404c627183a027dacc06e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/local/bin/perl

#  ********************************************************************
#  * Copyright (C) 2016 and later: Unicode, Inc. and others.
#  * License & terms of use: http://www.unicode.org/copyright.html#License
#  ********************************************************************
#  ********************************************************************
#  * COPYRIGHT:
#  * Copyright (c) 2002, International Business Machines Corporation and
#  * others. All Rights Reserved.
#  ********************************************************************


use strict;

use Dataset;

my $TABLEATTR = 'BORDER="1" CELLPADDING="4" CELLSPACING="0"';
my $outType = "HTML";
my $html = "noName";
my $inTable;
my @headers;
my @timetypes = ("mean per op", "error per op", "events", "per event");
my %raw;
my $current = "";
my $exp = 0;
my $mult = 1e9; #use nanoseconds
my $perc = 100; #for percent
my $printEvents = 0;
my $legend = "<a name=\"Legend\">\n<h2>Table legend</h2></a><ul>";
my $legendDone = 0;
my %options;
my $operationIs = "operation";
my $eventIs = "event";

sub startTest {
  $current = shift;
  $exp = 0;
  outputData($current);
}

sub printLeg {
  if(!$legendDone) {
    my $message;
    foreach $message (@_) {
      $legend .= "<li>".$message."</li>\n";
    }
  }
}

sub outputDist {
  my $value = shift;
  my $percent = shift;
  my $mean = $value->getMean;
  my $error = $value->getError;
  print HTML "<td class=\"";
  if($mean > 0) {
    print HTML "value";
  } else {
    print HTML "worse";
  }
  print HTML "\">";
  if($percent) {
    print HTML formatPercent(2, $mean);
  } else {
    print HTML formatNumber(2, $mult, $mean);
  }
  print HTML "</td>\n";  
  print HTML "<td class=\"";
  if((($error*$mult < 10)&&!$percent) || (($error<10)&&$percent)) {
    print HTML "error";
  } else {
    print HTML "errorLarge";
  }
  print HTML "\">&plusmn;";
  if($percent) {
    print HTML formatPercent(2, $error);
  } else {
    print HTML formatNumber(2, $mult, $error);
  }
  print HTML "</td>\n";  
}
  
sub outputValue {
  my $value = shift;
  print HTML "<td class=\"sepvalue\">";
  print HTML $value;
  #print HTML formatNumber(2, 1, $value);
  print HTML "</td>\n";  
}

sub startTable {
  #my $printEvents = shift;
  $inTable = 1;
  my $i;
  print HTML "<table $TABLEATTR>\n";
  print HTML "<tbody>\n";
  if($#headers >= 0) {
    my ($header, $i);
    print HTML "<tr>\n";
    print HTML "<th rowspan=\"2\" class=\"testNameHeader\"><a href=\"#TestName\">Test Name</a></th>\n";
    print HTML "<th rowspan=\"2\" class=\"testNameHeader\"><a href=\"#Ops\">Ops</a></th>\n";
    printLeg("<a name=\"Test Name\">TestName</a> - name of the test as set by the test writer\n", "<a name=\"Ops\">Ops</a> - number of ".$operationIs."s per iteration\n");
    if(!$printEvents) {
      print HTML "<th colspan=".((4*($#headers+1))-2)." class=\"sourceType\">Per Operation</th>\n";
    } else {
      print HTML "<th colspan=".((2*($#headers+1))-2)." class=\"sourceType\">Per Operation</th>\n";
      print HTML "<th colspan=".((5*($#headers+1))-2)." class=\"sourceType\">Per Event</th>\n";
    }
    print HTML "</tr>\n<tr>\n";
    if(!$printEvents) {
      foreach $header (@headers) {
	print HTML "<th class=\"source\" colspan=2><a href=\"#meanop_$header\">$header<br>/op</a></th>\n";
	printLeg("<a name=\"meanop_$header\">$header /op</a> - mean time and error for $header per $operationIs");
      }
    }
    for $i (1 .. $#headers) {
      print HTML "<th class=\"source\" colspan=2><a href=\"#mean_op_$i\">ratio $i<br>/op</a></th>\n";
      printLeg("<a name=\"mean_op_$i\">ratio $i /op</a> - ratio and error of per $operationIs time, calculated as: (($headers[0] - $headers[$i])/$headers[$i])*100%, mean value");
    }
    if($printEvents) {
      foreach $header (@headers) {
	print HTML "<th class=\"source\"><a href=\"#events_$header\">$header<br>events</a></th>\n";
	printLeg("<a name=\"events_$header\">$header events</a> - number of ".$eventIs."s for $header per iteration");
      }
      foreach $header (@headers) {
	print HTML "<th class=\"source\" colspan=2><a href=\"#mean_ev_$header\">$header<br>/ev</a></th>\n";
	printLeg("<a name=\"mean_ev_$header\">$header /ev</a> - mean time and error for $header per $eventIs");
      }
      for $i (1 .. $#headers) {
	print HTML "<th class=\"source\" colspan=2><a href=\"#mean_ev_$i\">ratio $i<br>/ev</a></th>\n";
	printLeg("<a name=\"mean_ev_$i\">ratio $i /ev</a> - ratio and error of per $eventIs time, calculated as: (($headers[0] - $headers[$i])/$headers[$i])*100%, mean value");
      }
    }
    print HTML "</tr>\n";
  }
  $legendDone = 1;
}

sub closeTable {
  if($inTable) {
    undef $inTable;
    print HTML "</tr>\n";
    print HTML "</tbody>";
    print HTML "</table>\n";
  }
}

sub newRow {
  if(!$inTable) {
    startTable;
  } else {
    print HTML "</tr>\n";
  }
  print HTML "<tr>";
}

sub outputData {
  if($inTable) {
    my $msg = shift;
    my $align = shift;
    print HTML "<td";
    if($align) {
      print HTML " align = $align>";
    } else {
      print HTML ">";
    }
    print HTML "$msg";
    print HTML "</td>";
  } else {
    my $message;
    foreach $message (@_) {
      print HTML "$message";
    }
  }
}

sub setupOutput {
  my $date = localtime;
  my $options = shift;
  %options = %{ $options };
  my $title = $options{ "title" };
  my $headers = $options{ "headers" };
  if($options{ "operationIs" }) {
    $operationIs = $options{ "operationIs" };
  }
  if($options{ "eventIs" }) {
    $eventIs = $options{ "eventIs" };
  }
  @headers = split(/ /, $headers);
  my ($t, $rest);
  ($t, $rest) = split(/\.\w+/, $0);
  $t =~ /^.*\W(\w+)$/;
  $t = $1;
  if($outType eq 'HTML') {
    $html = $date;
    $html =~ s/://g; # ':' illegal
    $html =~ s/\s*\d+$//; # delete year
    $html =~ s/^\w+\s*//; # delete dow
    $html = "$t $html.html";
    if($options{ "outputDir" }) {
      $html = $options{ "outputDir" }."/".$html;
    }
    $html =~ s/ /_/g;

    open(HTML,">$html") or die "Can't write to $html: $!";

#<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    print HTML <<EOF;
<HTML>
   <HEAD>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <TITLE>$title</TITLE>
<style>
<!--
body         { font-size: 10pt; font-family: sans-serif }
th           { font-size: 10pt; border: 0 solid #000080; padding: 5 }
th.testNameHeader { border-width: 1 }
th.testName  { text-align: left; border-left-width: 1; border-right-width: 1; 
               border-bottom-width: 1 }
th.source    { border-right-width: 1; border-bottom-width: 1 }
th.sourceType { border-right-width: 1; border-top-width: 1; border-bottom-width: 1 }
td           { font-size: 10pt; text-align: Right; border: 0 solid #000080; padding: 5 }
td.string    { text-align: Left; border-bottom-width:1; border-right-width:1 }
td.sepvalue  { border-bottom-width: 1; border-right-width: 1 }
td.value     { border-bottom-width: 1 }
td.worse     { color: #FF0000; font-weight: bold; border-bottom-width: 1 }
td.error     { font-size: 75%; border-right-width: 1; border-bottom-width: 1 }
td.errorLarge { font-size: 75%; color: #FF0000; font-weight: bold; border-right-width: 1; 
               border-bottom-width: 1 }
A:link    { color: black; font-weight: normal; text-decoration: none}    /* unvisited links */
A:visited { color: blue; font-weight: normal; text-decoration: none }   /* visited links   */
A:hover   { color: red; font-weight: normal; text-decoration: none } /* user hovers     */
A:active  { color: lime; font-weight: normal; text-decoration: none }   /* active links    */
-->
</style>
   </HEAD>
   <BODY bgcolor="#FFFFFF" LINK="#006666" VLINK="#000000">
EOF
    print HTML "<H1>$title</H1>\n";

    #print HTML "<H2>$TESTCLASS</H2>\n";
  }
}

sub closeOutput {
  if($outType eq 'HTML') {
    if($inTable) {
      closeTable;
    }
    $legend .= "</ul>\n";
    print HTML $legend;
    outputRaw();
    print HTML <<EOF;
   </BODY>
</HTML>
EOF
    close(HTML) or die "Can't close $html: $!";
  }
}


sub outputRaw {
  print HTML "<h2>Raw data</h2>";
  my $key;
  my $i;
  my $j;
  my $k;
  print HTML "<table $TABLEATTR>\n";
  for $key (sort keys %raw) {
    my $printkey = $key;
    $printkey =~ s/\<br\>/ /g;
    if($printEvents) {
      if($key ne "") {
	print HTML "<tr><th class=\"testNameHeader\" colspan = 7>$printkey</td></tr>\n"; # locale and data file
      }
      print HTML "<tr><th class=\"testName\">test name</th><th class=\"testName\">interesting arguments</th><th class=\"testName\">iterations</th><th class=\"testName\">operations</th><th class=\"testName\">mean time (ns)</th><th class=\"testName\">error (ns)</th><th class=\"testName\">events</th></tr>\n";
    } else {
      if($key ne "") {
	print HTML "<tr><th class=\"testName\" colspan = 6>$printkey</td></tr>\n"; # locale and data file
      }
      print HTML "<tr><th class=\"testName\">test name</th><th class=\"testName\">interesting arguments</th><th class=\"testName\">iterations</th><th class=\"testName\">operations</th><th class=\"testName\">mean time (ns)</th><th class=\"testName\">error (ns)</th></tr>\n";
    }
    $printkey =~ s/[\<\>\/ ]//g;
      
    my %done;
    for $i ( $raw{$key} ) {
      print HTML "<tr>";
      for $j ( @$i ) {
	my ($test, $args);
	($test, $args) = split(/,/, shift(@$j));

	print HTML "<th class=\"testName\">";
	if(!$done{$test}) {
	  print HTML "<a name=\"".$printkey."_".$test."\">".$test."</a>";
	  $done{$test} = 1;
	} else {
	  print HTML $test;
	}
	print HTML "</th>";

	print HTML "<td class=\"string\">".$args."</td>";
	
	print HTML "<td class=\"sepvalue\">".shift(@$j)."</td>";
	print HTML "<td class=\"sepvalue\">".shift(@$j)."</td>";

	my @data = @{ shift(@$j) };
	my $ds = Dataset->new(@data);
	print HTML "<td class=\"sepvalue\">".formatNumber(4, $mult, $ds->getMean)."</td><td class=\"sepvalue\">".formatNumber(4, $mult, $ds->getError)."</td>";
	if($#{ $j } >= 0) {
	  print HTML "<td class=\"sepvalue\">".shift(@$j)."</td>";
	}
	print HTML "</tr>\n";
      }
    }
  }
}

sub store {
  $raw{$current}[$exp++] = [@_];
}

sub outputRow {
  #$raw{$current}[$exp++] =  [@_];
  my $testName = shift;
  my @iterPerPass = @{shift(@_)};
  my @noopers =  @{shift(@_)};
   my @timedata =  @{shift(@_)};
  my @noevents;
  if($#_ >= 0) {
    @noevents =  @{shift(@_)};
  }
  if(!$inTable) {
    if(@noevents) {
      $printEvents = 1;
      startTable;
    } else {
      startTable;
    }
  }
  debug("No events: @noevents, $#noevents\n");

  my $j;
  my $loc = $current;
  $loc =~ s/\<br\>/ /g;
  $loc =~ s/[\<\>\/ ]//g;

  # Finished one row of results. Outputting
  newRow;
  #outputData($testName, "LEFT");
  print HTML "<th class=\"testName\"><a href=\"#".$loc."_".$testName."\">$testName</a></th>\n";
  #outputData($iterCount);
  #outputData($noopers[0], "RIGHT");
  outputValue($noopers[0]);

  if(!$printEvents) {
    for $j ( 0 .. $#timedata ) {
      my $perOperation = $timedata[$j]->divideByScalar($iterPerPass[$j]*$noopers[$j]); # time per operation
      #debug("Time per operation: ".formatSeconds(4, $perOperation->getMean, $perOperation->getError)."\n");
      outputDist($perOperation);
    }
  }
  my $baseLinePO = $timedata[0]->divideByScalar($iterPerPass[0]*$noopers[0]);
  for $j ( 1 .. $#timedata ) {
    my $perOperation = $timedata[$j]->divideByScalar($iterPerPass[$j]*$noopers[$j]); # time per operation
    my $ratio = $baseLinePO->subtract($perOperation);
    $ratio = $ratio->divide($perOperation);
    outputDist($ratio, "%");
  }   
  if (@noevents) {
    for $j ( 0 .. $#timedata ) {
      #outputData($noevents[$j], "RIGHT");
      outputValue($noevents[$j]);
    }
    for $j ( 0 .. $#timedata ) {
      my $perEvent =  $timedata[$j]->divideByScalar($iterPerPass[$j]*$noevents[$j]); # time per event
      #debug("Time per operation: ".formatSeconds(4, $perEvent->getMean, $perEvent->getError)."\n");
      outputDist($perEvent);
    }   
    my $baseLinePO = $timedata[0]->divideByScalar($iterPerPass[0]*$noevents[0]);
    for $j ( 1 .. $#timedata ) {
      my $perOperation = $timedata[$j]->divideByScalar($iterPerPass[$j]*$noevents[$j]); # time per operation
      my $ratio = $baseLinePO->subtract($perOperation);
      $ratio = $ratio->divide($perOperation);
      outputDist($ratio, "%");
    }   
  }
}


1;

#eof