-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathanalyze
executable file
·1679 lines (1391 loc) · 57.2 KB
/
analyze
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
# ABSTRACT: Analyze benchmark data produced by timeall
use 5.010;
use strict;
use warnings;
use Pod::Usage;
use Getopt::Long;
use List::Util 'min', 'max';
use List::MoreUtils 'uniq';
use File::Basename;
use IO::File;
use DateTime;
use JSON;
use FindBin;
use lib "$FindBin::Bin/lib";
use Analyze::Summary;
my %FORMATTER = (
json => \&summarize_results_json,
text => \&summarize_results_text,
html => \&summarize_results_html,
html_snippet => \&summarize_results_html_snippet,
html_plot => \&summarize_results_html_plot,
);
my %DIAGNOSIS_ICON = (
SEGFAULT => '☠',
SPAWN_FAILED => '⛔',
TIMED_OUT => '⏲', # Or ⏰ or ⏱
RECEIVED_SIGNAL => '⚡',
RECEIVED_SIGINT => '⎊',
NONZERO_EXIT => 'Ø',
WROTE_TO_STDERR => 'E',
OUTPUT_MISMATCH => '≠',
unknown => '?',
mixed => '⦻',
'' => '.',
);
MAIN();
sub MAIN {
# Process options and command line arguments
my $main_opt = process_options_and_arguments();
# Open outfile
my $out = $main_opt->{outfile};
my $out_fh = $out eq '-' ? \*STDOUT : IO::File->new($out, '>')
or die "Could not open outfile '$out': $!";
# Analyze results
analyze_timings_files($main_opt, $out_fh, @ARGV);
}
sub process_options_and_arguments {
my %opt;
GetOptions(\%opt, 'help|h|?!', 'man!', 'format=s', 'style=s', 'outfile=s',
'verbose!', 'min-time|min_time|mintime=f',
'ignore-startup|ignore_startup|ignorestartup!',
'ignore-compile|ignore_compile|ignorecompile!',
'skip-incomplete|skip_incomplete|skipincomplete!',
'show-failures|show_failures|showfailures!',
'show-relative|show_relative|showrelative!',
'show-rates|show_rates|showrates!',
'compare!', 'history!')
or pod2usage(-verbose => 0);
pod2usage(-verbose => 1) if $opt{help};
pod2usage(-verbose => 2) if $opt{man};
$opt{'min-time'} //= 1e-2;
$opt{'show-rates'} //= 1;
$opt{'show-relative'} //= $opt{compare};
$opt{outfile} //= '-';
my $suffix = (fileparse($opt{outfile}, qr/\.[^.]+$/))[2] || '.';
my $ext = lc substr $suffix, 1;
$opt{format} //= exists $FORMATTER{$ext} ? $ext : 'text';
$opt{format} = lc $opt{format};
my $formatter = $FORMATTER{$opt{format}}
or pod2usage(-msg => "Unknown output format '$opt{format}'");
$opt{formatter} = $formatter;
$opt{style} //= 'auto';
$opt{style} = lc $opt{style};
(grep {$_ eq $opt{style}} 0, 1, 'auto')
or pod2usage(-msg => "Unknown output style setting '$opt{style}'");
pod2usage(-msg => "Must list timing files to analyze") unless @ARGV;
return \%opt;
}
sub analyze_timings_files {
my ($opt, $out_fh, @files) = @_;
my $min_time = $opt->{'min-time'};
my $ignore_startup = $opt->{'ignore-startup'};
my $ignore_compile = $opt->{'ignore-compile'};
my $skip_incomplete = $opt->{'skip-incomplete'};
my $analyze_timing_data = sub {
my $data = shift;
my $startup = $data->{run}{startup} || {};
for my $test (@{$data->{times}}) {
$test->{diagnoses} = diagnose_runs($test->{raw});
$test->{compare}
= compare_scaled_times($test->{best}, $startup, $min_time,
$ignore_startup, $ignore_compile);
}
($data->{score}, $data->{test_score})
= compute_scores($data, $skip_incomplete);
$opt->{formatter}->($data, $opt, $out_fh);
};
if ($opt->{compare}) {
my $merged = merge_timing_files(@files);
$analyze_timing_data->($merged);
}
else {
for my $timing_file (@files) {
my $data = load_timing_file($timing_file);
$analyze_timing_data->($data);
}
}
}
sub merge_timing_files {
my (%times);
my (@test_order, @groups, @compilers);
my (@start_times, @end_times, @versions);
for my $file (@_) {
my $basename = fileparse($file, qr/\.[^.]+$/);
my $data = load_timing_file($file);
# Collect all values for lists that will be summarized
# (using min, max, or uniq) after all files are processed
push @groups, @{$data->{config}{groups}};
push @versions, $data->{run}{versions}{bench};
push @end_times, $data->{run}{end_time};
push @start_times, $data->{run}{start_time};
# Merge compiler groups
my $compilers = $data->{config}{compilers};
for my $compiler (@$compilers) {
next unless $compiler->{enabled};
$compiler->{run} = $basename;
$compiler->{key} = "$compiler->{name}/$basename";
push @compilers, $compiler;
}
# Merge test timings
for my $test (@{$data->{times}}) {
my $name = $test->{name};
push @test_order, $name;
my $merged = $times{$name} ||=
{
name => $name,
conf => $test->{conf},
raw => {},
best => {},
};
# Merge in any new configuration keys
for my $key (keys %{$test->{conf}}) {
$merged->{conf}{$key} //= $test->{conf}{$key};
}
for my $compiler (keys %{$test->{best}}) {
my $key = "$compiler/$basename";
$merged->{$_}{$key} = $test->{$_}{$compiler} for qw( raw best );
}
}
}
# Summarize collected lists
my $start_time = min @start_times;
my $end_time = max @end_times;
@groups = uniq @groups;
@versions = uniq sort @versions;
@test_order = uniq @test_order;
# Bad user, no biscuit! But we'll still try ....
if (@versions > 1) {
print STDERR "WARNING: Comparing results from different benchmark versions:\n";
print STDERR " $_\n" for @versions;
}
my $merged = {
config => {
groups => \@groups,
compilers => \@compilers,
},
run => {
start_time => $start_time,
end_time => $end_time,
versions => { bench => join ', ' => @versions },
},
times => [ map $times{$_}, @test_order ],
};
return $merged;
}
sub load_timing_file {
my $timing_file = shift;
my $decoder = JSON->new->utf8;
my $in_fh = $timing_file eq '-' ? \*STDIN : IO::File->new($timing_file, '<')
or die "Could not open timing file '$timing_file': $!";
local $/;
my $json = <$in_fh>;
return $decoder->decode($json);
}
sub diagnose_runs {
my ($run_info) = @_;
my %diagnoses;
while (my ($comp, $runs) = each %$run_info) {
my (%by_scale, %details);
for my $run (@$runs) {
my $scale = $run->{scale};
my $diagnosis = $run->{diagnosis} //= 'unknown';
push @{$by_scale{$scale} ||= []}, $diagnosis;
push @{$details{$scale} ||= []}, $run;
}
my %summary;
while (my ($scale, $diagnoses) = each %by_scale) {
my @diagnoses = uniq @$diagnoses;
$summary{$scale} = @diagnoses > 1 ? 'mixed' : $diagnoses[0];
}
my @sorted = map { $summary{$_} }
sort { $a <=> $b } keys %summary;
my @details = map { $details{$_} }
sort { $a <=> $b } keys %details;
$diagnoses{$comp} = {
by_scale => \%by_scale,
summary => \%summary,
details => \@details,
sorted => \@sorted,
};
}
return \%diagnoses;
}
sub most_diagnoses {
my ($tests) = @_;
my @test_maxes;
for my $test (@$tests) {
my @diagnoses = values %{$test->{diagnoses}};
push @test_maxes, max map { scalar @{$_->{sorted}} } @diagnoses;
}
return max @test_maxes;
}
sub compare_scaled_times {
my ($times, $startup, $min_time, $ignore_startup, $ignore_compile) = @_;
my $max_rate = 0;
my %max_rates;
my %times_by_scale;
my %rates_by_scale;
my %rates_by_comp;
while (my ($comp, $scale_runs) = each %$times) {
my $startup_time = $ignore_startup ? $startup->{$comp} || 0 : 0;
my $compile_time = $ignore_compile ? $scale_runs->{0}{time} || 0 : 0;
my $ignore_time = max($startup_time, $compile_time);
while (my ($scale, $run) = each %$scale_runs) {
next unless $scale && defined $run && defined $run->{time};
my $time = $run->{time};
my $work = $run->{work};
# If below the minimum time after removing ignored
# startup/compile time, the calculated rate is going to be
# effectively garbage, so just skip this data point
$time -= $ignore_time;
next if $time < $min_time;
$times_by_scale{$scale}{$comp} = $time;
my $rate =
$rates_by_scale{$scale}{$comp} =
$rates_by_comp{$comp}{$scale} = $work / $time;
$max_rate = $rate if $max_rate < $rate;
$max_rates{$scale} = $rate
if !$max_rates{$scale} || $max_rates{$scale} < $rate;
}
}
my %relative_rates;
while (my ($scale, $comp_rates) = each %rates_by_scale) {
while (my ($comp, $rate) = each %$comp_rates) {
$relative_rates{$scale}{$comp} = $rate / $max_rates{$scale};
}
}
my %peak_rate;
while (my ($comp, $scale_rates) = each %rates_by_comp) {
my @sorted = sort { $scale_rates->{$b} <=> $scale_rates->{$a} } keys %$scale_rates;
my $fastest = $sorted[0];
my $rate = $scale_rates->{$fastest};
$peak_rate{$comp} = {
scale => $fastest,
rate => $rate,
relative_to_max => $rate / $max_rate,
relative_at_scale => $rate / $max_rates{$fastest},
};
}
return {
times_by_scale => \%times_by_scale,
rates_by_scale => \%rates_by_scale,
rates_by_comp => \%rates_by_comp,
relative_rates => \%relative_rates,
peak_rate => \%peak_rate,
max_rate => $max_rate,
max_rates => \%max_rates,
};
}
# Compute overall 'score' by geometric mean of relative rates to
# a standard compiler serving as the reference 1.0 value.
sub compute_scores {
my ($data, $skip_incomplete) = @_;
my $tests = $data->{times};
my @compilers = map { $_->{key} || $_->{name} }
@{$data->{config}{compilers}};
my $standard = $compilers[0];
my %score;
$score{$_} = 1.0 for @compilers;
my %test_score;
my $summarized = 0;
TEST: for my $test (@$tests) {
my $peak_rate = $test->{compare}{peak_rate};
# Optionally skip any test that doesn't have a peak rate
# specified for every compiler being compared
if ($skip_incomplete) {
for my $compiler (@compilers) {
next TEST unless defined $peak_rate->{$compiler}{rate};
}
}
# Can't compute scores at all if we lack a reference point
my $reference = $peak_rate->{$standard}{rate};
return unless $reference;
# Determine if test should be included in summary
my $summarize = $test->{conf}{summarize} // 1;
$summarized++ if $summarize;
for my $compiler (@compilers) {
my $rate = $peak_rate->{$compiler}{rate};
$score{$compiler} = undef, next
unless defined $rate && defined $score{$compiler};
my $relative = $rate / $reference;
$score{$compiler} *= $relative if $summarize;
$test_score{$test->{name}}{$compiler} = $relative * 100;
}
}
my $power = 1 / ($summarized || 1);
for my $compiler (@compilers) {
next unless defined $score{$compiler};
$score{$compiler} **= $power;
# To get "% of reference compiler speed"
$score{$compiler} *= 100;
}
return (\%score, \%test_score);
}
sub summarize_results_json {
my ($data, $opt, $out_fh) = @_;
my $style = $opt->{style};
$style = 1 if $style eq 'auto';
my $encoder = JSON->new->utf8->canonical;
$encoder->pretty if $style;
my $json = $encoder->encode($data);
print $out_fh $json;
}
sub summarize_results_text {
my ($data, $opt, $out_fh) = @_;
$opt->{style} = -t $out_fh
if $opt->{style} eq 'auto';
return summarize_results_text_history($data, $opt, $out_fh)
if $opt->{history};
my $style = $opt->{style};
my $RED = $style ? "\e[1;31m" : '';
my $GREEN = $style ? "\e[32m" : '';
my $YELLOW = $style ? "\e[1;33m" : '';
my $CLEAR = $style ? "\e[0m" : '';
my $s = Analyze::Summary::Compare->new(data => $data, opt => $opt);
my $longest_test = max 13, map { length } @{$s->{test_names}};
my $longest_comp = max 8, map { length } @{$s->{run_names}},
@{$s->{lang_names}}, @{$s->{comp_names}}, @{$s->{vm_names}};
my $most_diagnoses = most_diagnoses($data->{times});
$longest_comp = max $longest_comp, $most_diagnoses;
my $spacer_length = 3;
my $spacer = ' ' x $spacer_length;
my @comps = @{$s->{compilers}};
my $format = join $spacer => "%-${longest_test}s", (("%${longest_comp}s ") x @comps);
my @lang_titles = map { center(" $_ ", $s->{lang_count}{$_} * ($longest_comp + $spacer_length + 2) - $spacer_length, bg => '-') } @{$s->{langs}};
my $titles = join $spacer => @lang_titles;
my @ignore = @{$s->{ignoring}};
my $ignore = @ignore ? ' (ignoring ' . join(' and ' => @ignore) . ')' : '';
my $start = friendly_time($data->{run}{start_time});
my $run_at = $opt->{compare} ? '' : " run at $start";
my $showing = 'showing ' . english_list(@{$s->{showing}});
my $output = "$CLEAR\n==> perl6-bench version $data->{run}{versions}{bench}$run_at$ignore\n--- $showing\n\n";
$output .= ' ' x $longest_test . "$spacer$titles\n";
$output .= sprintf "$format\n", '', @{$s->{run_names}}
if grep {length} @{$s->{run_names}};
$output .= sprintf "$format\n", '', @{$s->{comp_names}};
$output .= sprintf "$format\n", 'TEST', @{$s->{vm_names}};
$output .= ' ' x $longest_test . $spacer . '-' x length($titles) . "\n";
my $row_height = grep $_, @$opt{qw( show-rates show-failures compare )};
my $double_space = !$style && $row_height > 1;
my %test_name_shown;
my $show_test_name = sub {
my $test = shift;
my $name = $test_name_shown{$test->{name}}++ ? '' : $test->{name};
$output .= sprintf "%-${longest_test}s", $name;
};
my $icon_set = sub {
my @icons = map { $DIAGNOSIS_ICON{$_}
// $DIAGNOSIS_ICON{unknown} } @_;
join '' => @icons;
};
for my $test (@{$data->{times}}) {
$output .= "\n" if $double_space;
if ($opt->{'show-rates'}) {
$show_test_name->($test);
for my $comp (@comps) {
my $key = $comp->{key} || $comp->{name};
my $peak = $test->{compare}{peak_rate}{$key};
if (defined $peak && defined $peak->{rate}) {
$output .= sprintf "$spacer%${longest_comp}.0f/s", $peak->{rate};
}
else {
$output .= sprintf "$spacer%${longest_comp}s ", '-- ';
}
}
$output .= "\n";
}
if ($opt->{'show-relative'}) {
$show_test_name->($test);
for my $comp (@comps) {
my $key = $comp->{key} || $comp->{name};
my $peak = $test->{compare}{peak_rate}{$key};
my $rel = $peak->{relative_to_max};
if ($rel) {
$rel = 1 / $rel;
my $color = $rel < 2 ? $GREEN :
$rel < 10 ? $YELLOW :
$RED ;
$output .= sprintf "$spacer$color%${longest_comp}.1fx $CLEAR", $rel;
}
else {
# XXXX: May have to make this based on key instead of name
my $conf = $test->{conf};
my $is_skip = !defined $conf->{$comp->{group}}
|| (grep { $_ eq $comp->{name} } @{$conf->{skip} || []})
|| (exists $conf->{skip} && !defined $conf->{skip});
$output .= sprintf "$spacer$RED%${longest_comp}s $CLEAR",
$is_skip ? 'SKIP' : 'FAIL';
}
}
$output .= "\n";
}
if ($opt->{'show-failures'}) {
my $len = $longest_comp + 2;
my %icon_sets;
for my $comp (@comps) {
my $key = $comp->{key} || $comp->{name};
my $diags = $test->{diagnoses}{$key};
my @icon_sets;
if ($opt->{verbose}) {
my $by_scale = $diags->{by_scale};
my @scales = sort { $a <=> $b } keys %$by_scale;
for my $scale (@scales) {
my $diag = $by_scale->{$scale};
push @icon_sets, $icon_set->(@$diag);
}
}
else {
my $diag = $diags->{sorted};
push @icon_sets, $icon_set->(@$diag);
}
$icon_sets{$key} = \@icon_sets;
}
if ($opt->{verbose} && $opt->{compare}) {
my $max = max map { scalar @$_ } values %icon_sets;
for my $i (0 .. $max - 1) {
$show_test_name->($test);
for my $comp (@comps) {
my $key = $comp->{key} || $comp->{name};
my $icons = $icon_sets{$key}[$i] || '';
$output .= sprintf "$spacer%${len}s", $icons;
}
$output .= "\n";
}
}
else {
$show_test_name->($test);
for my $comp (@comps) {
my $key = $comp->{key} || $comp->{name};
$output .= sprintf "$spacer%${len}s",
join ',' => @{$icon_sets{$key}};
}
$output .= "\n";
}
}
}
my $scores = $data->{score};
if ($scores && $opt->{'show-relative'}) {
$output .= ' ' x $longest_test . $spacer . '=' x length($titles) . "\n";
$output .= sprintf "%-${longest_test}s", 'SUMMARY SCORE';
for my $comp (@comps) {
my $key = $comp->{key} || $comp->{name};
my $score = $scores->{$key};
if (defined $score) {
my $color = $score > 50 ? $GREEN :
$score > 10 ? $YELLOW :
$RED ;
$output .= sprintf "$spacer$color%${longest_comp}.1f $CLEAR", $score;
}
else {
$output .= sprintf "$spacer%${longest_comp}s ", '-- ';
}
}
$output .= "\n";
}
print $out_fh $output;
}
sub summarize_results_text_history {
my ($data, $opt, $out_fh) = @_;
my $scores = $data->{score}
or die "Can't show history without comparison scores!";
my $style = $opt->{style};
my $RED = $style ? "\e[1;31m" : '';
my $GREEN = $style ? "\e[32m" : '';
my $YELLOW = $style ? "\e[1;33m" : '';
my $CLEAR = $style ? "\e[0m" : '';
my $s = Analyze::Summary->new(data => $data, opt => $opt);
my @comp_names = uniq map { $_->{name} } @{$s->{compilers}};
my $longest_comp = max 8, map { length } @comp_names;
my %column;
my $col = 0;
$column{$_} = ++$col for @comp_names;
my $spacer_length = 3;
my $spacer = ' ' x $spacer_length;
my $format = join $spacer => "%-10s", (("%${longest_comp}s ") x $col);
$format .= "\n";
my @date_sorted = sort { ($a->{commit_time} || 0)
<=> ($b->{commit_time} || 0) } @{$s->{compilers}};
my @ignore = @{$s->{ignoring}};
my $ignore = @ignore ? ' (ignoring ' . join(' and ' => @ignore) . ')' : '';
my $start = friendly_time($data->{run}{start_time});
my $run_at = $opt->{compare} ? '' : " run at $start";
my $skip = $opt->{'skip-incomplete'} ? ' (skipping incomplete data)' : '';
my $output = "$CLEAR\n==> perl6-bench version $data->{run}{versions}{bench}$run_at$ignore\n";
$output .= "--- showing HISTORICAL SCORES$skip\n\n";
$output .= sprintf $format, 'DATE', @comp_names;
# Put scores into columns by compiler name, allowing multiple scores
# for different compilers on the same date to appear in the same row.
my @row;
my $old_date = '';
my $new_row = sub {
my $date = shift;
$old_date = $date;
$output .= sprintf($format, @row) if @row;
@row = ($date, ('') x $col);
};
for my $comp (@date_sorted) {
my $commit = $comp->{commit_time} || 0;
my $date = DateTime->from_epoch(epoch => $commit)->ymd;
$new_row->($date) if $old_date ne $date;
my $key = $comp->{key} || $comp->{name};
my $score = $scores->{$key};
my $column = $column{$comp->{name}};
# Don't let new scores on the same date overwrite old ones
$new_row->($date) if $row[$column] ne '';
if (defined $score) {
my $color = $score > 50 ? $GREEN :
$score > 10 ? $YELLOW :
$RED ;
$row[$column] = sprintf "$color%${longest_comp}.1f$CLEAR", $score;
}
else {
$row[$column] = sprintf "$RED%${longest_comp}s$CLEAR", '--';
}
}
$output .= sprintf $format, @row if @row;
print $out_fh $output;
}
sub center {
my ($string, $length, %opts) = @_;
my $bg_char = $opts{bg} || ' ';
my $output = $bg_char x ($length / length $bg_char);
my $left = int(($length - length $string) / 2);
substr($output, $left, length $string, $string);
return $output;
}
sub summarize_results_html {
my ($data, $opt, $out_fh) = @_;
# Default to including style in full HTML pages
$opt->{style} = 1
if $opt->{style} eq 'auto';
print $out_fh <<'HEADER';
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Perl Bench Summary</title>
</head>
<body>
HEADER
summarize_results_html_snippet($data, $opt, $out_fh);
print $out_fh <<'FOOTER';
</body>
</html>
FOOTER
}
sub summarize_results_html_snippet {
my ($data, $opt, $out_fh) = @_;
my $html = '';
# Default to no style info if just generating an HTML snippet
$opt->{style} = 0
if $opt->{style} eq 'auto';
if ($opt->{style}) {
$html .= <<'CSS';
<style type="text/css">
.bench_summary { font-family: sans-serif; }
.bench_summary caption { font-style: italic; }
.bench_summary td { padding-left: .4em; padding-right: .4em; }
.bench_summary th { padding-left: .4em; padding-right: .4em; }
.bench_ver { font-family: monospace; }
.bench_start_time { font-family: monospace; }
.bench_language { text-align: center; border-bottom: 1px solid #999; border-left: .4em solid #fff; border-right: .4em solid #fff; }
.bench_run { text-align: center; padding-top: .1em; }
.bench_compiler { text-align: center; padding-top: .1em; }
.bench_vm { text-align: center; padding-top: .1em; }
.bench_time_row td { }
.bench_compare_row td { }
.bench_diagnoses_row td { vertical-align: top; }
.bench_spacer_row { height: .7em; }
.bench_rate { text-align: right; font-family: monospace; }
.bench_time { text-align: right; font-family: monospace; }
.bench_no_time { text-align: right; font-family: monospace; }
.bench_diagnoses { text-align: right; font-family: monospace; }
.bench_good { text-align: right; font-family: monospace; background-color: #3f7; color: black; }
.bench_bad { text-align: right; font-family: monospace; background-color: #ee0; color: black; }
.bench_ugly { text-align: right; font-family: monospace; background-color: #f55; color: white; }
.bench_skip { text-align: right; font-family: monospace; background-color: #666; color: white; }
.bench_fail { text-align: right; font-family: monospace; background-color: #f55; color: white; }
</style>
CSS
}
return summarize_results_html_history($html, $data, $opt, $out_fh)
if $opt->{history};
my $s = Analyze::Summary::Compare->new(data => $data, opt => $opt);
my @ignore = @{$s->{ignoring}};
my $ignore = @ignore ? ' (ignoring ' . join(' and ' => @ignore) . ')' : '';
my $start = friendly_time($data->{run}{start_time});
my $run_at = $opt->{compare} ? '' : qq{ run at <span class="bench_start_time">$start</span>};
my $showing = 'showing ' . english_list(@{$s->{showing}});
$showing =~ s/\((\S+?)\)/(<strong>$1<\/strong>)/g;
$html .= qq{<table class="bench_summary" cellspacing="0" cellpadding="0">\n};
$html .= qq{<caption>perl6-bench version <span class="bench_ver">$data->{run}{versions}{bench}</span>$run_at$ignore<br>$showing</caption>\n};
$html .= "<tr><th></th>\n" . join('' => map qq{ <th class="bench_language" colspan="$s->{lang_count}{$_}">$_</th>\n} => @{$s->{langs}}) . "</tr>\n";
$html .= "<tr><th></th>\n" . join('' => map qq{ <th class="bench_run">$_</th>\n} => @{$s->{run_names}}) . "</tr>\n" if grep {length} @{$s->{run_names}};
$html .= "<tr><th></th>\n" . join('' => map qq{ <th class="bench_compiler">$_</th>\n} => @{$s->{comp_names}}) . "</tr>\n";
$html .= "<tr><th>TEST</th>\n" . join('' => map qq{ <th class="bench_vm">$_</th>\n} => @{$s->{vm_names}}) . "</tr>\n";
my $row_height = grep $_, @$opt{qw( show-rates show-failures compare )};
my $double_space = $row_height > 1;
my %test_name_shown;
my $show_test_name = sub {
my ($test, $class) = @_;
my $name = $test_name_shown{$test->{name}}++ ? '' : $test->{name};
$html .= qq{<tr class="$class"><td>$name</td>\n};
};
my $icon_set = sub {
my @icons = map { my $icon = $DIAGNOSIS_ICON{$_}
// $DIAGNOSIS_ICON{unknown};
qq{<span title="$_">$icon</span>}; } @_;
join '' => @icons;
};
my $detailed_icons = sub {
my @icons;
for my $run (@_) {
my $diag = $run->{diagnosis};
my $icon = $DIAGNOSIS_ICON{$diag}
// $DIAGNOSIS_ICON{unknown};
my $hover = $diag ? "$diag: $run->{reason}" : 'ok';
push @icons, qq{<span title="$hover">$icon</span>};
}
join '' => @icons;
};
my @comps = @{$s->{compilers}};
for my $test (@{$data->{times}}) {
if ($opt->{'show-rates'}) {
$show_test_name->($test, 'bench_time_row');
for my $comp (@comps) {
my $key = $comp->{key} || $comp->{name};
my $peak = $test->{compare}{peak_rate}{$key};
if (defined $peak && defined $peak->{rate}) {
$html .= sprintf qq{ <td class="bench_rate">%.0f/s</td>\n}, $peak->{rate};
}
else {
$html .= qq{ <td class="bench_no_time">--</td>\n};
}
}
$html .= "</tr>\n";
}
if ($opt->{'show-relative'}) {
$show_test_name->($test, 'bench_compare_row');
for my $comp (@comps) {
my $key = $comp->{key} || $comp->{name};
my $peak = $test->{compare}{peak_rate}{$key};
my $rel = $peak->{relative_to_max};
if ($rel) {
$rel = 1 / $rel;
my $class = $rel < 2 ? 'bench_good' :
$rel < 10 ? 'bench_bad' :
'bench_ugly' ;
$html .= sprintf qq{ <td class="$class">%.1fx</td>\n}, $rel;
}
else {
# XXXX: May have to make this based on key instead of name
my $conf = $test->{conf};
my $is_skip = !defined $conf->{$comp->{group}}
|| (grep { $_ eq $comp->{name} } @{$conf->{skip} || []})
|| (exists $conf->{skip} && !defined $conf->{skip});
my $class = $is_skip ? 'bench_skip' : 'bench_fail';
my $message = $is_skip ? 'SKIP' : 'FAIL';
$html .= qq{ <td class="$class">$message</td>\n};
}
}
$html .= "</tr>\n";
}
if ($opt->{'show-failures'}) {
$show_test_name->($test, 'bench_diagnoses_row');
for my $comp (@comps) {
my $key = $comp->{key} || $comp->{name};
my $diags = $test->{diagnoses}{$key};
my (@icon_sets, $icons);
if ($opt->{verbose}) {
my $details = $diags->{details};
my @icon_sets;
for my $run_set (@$details) {
push @icon_sets, $detailed_icons->(@$run_set);
}
$icons = join '<br>' => @icon_sets;
}
else {
my $diag = $diags->{sorted};
$icons = $icon_set->(@$diag);
}
$html .= qq{ <td class="bench_diagnoses">$icons</td>\n};
}
$html .= "</tr>\n";
}
$html .= qq{<tr class="bench_spacer_row"></tr>\n}
if $double_space;
}
my $scores = $data->{score};
if ($scores && $opt->{'show-relative'}) {
$html .= qq{<tr class="bench_spacer_row"></tr>\n};
$html .= qq{<tr class="bench_compare_row"><th>SUMMARY SCORE</th>\n};
for my $comp (@comps) {
my $key = $comp->{key} || $comp->{name};
my $score = $scores->{$key};
if (defined $score) {
my $class = $score > 50 ? 'bench_good' :
$score > 10 ? 'bench_bad' :
'bench_ugly' ;
$html .= sprintf qq{ <td class="$class">%.1f</td>\n}, $score;
}
else {
$html .= qq{ <td class="bench_skip">--</td>\n};
}
}
$html .= qq{</tr>\n};
}
$html .= "</table>\n";
print $out_fh $html;
}
sub summarize_results_html_history {
my ($html, $data, $opt, $out_fh) = @_;
my $scores = $data->{score}
or die "Can't show history without comparison scores!";
my $s = Analyze::Summary->new(data => $data, opt => $opt);
my @comp_names = uniq map { $_->{name} } @{$s->{compilers}};
my %column;
my $col = 0;
$column{$_} = ++$col for @comp_names;
my @date_sorted = sort { ($a->{commit_time} || 0)
<=> ($b->{commit_time} || 0) } @{$s->{compilers}};
my @ignore = @{$s->{ignoring}};
my $ignore = @ignore ? ' (ignoring ' . join(' and ' => @ignore) . ')' : '';
my $start = friendly_time($data->{run}{start_time});
my $run_at = $opt->{compare} ? '' : qq{ run at <span class="bench_start_time">$start</span>};
my $skip = $opt->{'skip-incomplete'} ? ' (skipping incomplete data)' : '';
my $showing = "showing HISTORICAL SCORES$skip";
# XXXX: WIP
$html .= qq{<table class="bench_summary" cellspacing="0" cellpadding="0">\n};
$html .= qq{<caption>perl6-bench version <span class="bench_ver">$data->{run}{versions}{bench}</span>$run_at$ignore<br>$showing</caption>\n};
$html .= join "\n" => '<tr><th>DATE</th>', (map qq{ <th class="bench_compiler">$_</th>} => @comp_names), "</tr>\n";
# Put scores into columns by compiler name, allowing multiple scores
# for different compilers on the same date to appear in the same row.
my @row;
my $old_date = '';
my $new_row = sub {
my $date = shift;
$old_date = $date;
if (@row) {
$_ ||= ' <td></td>' for @row;
$html .= join "\n" => '<tr>', @row, "</tr>\n";
}
@row = (" <td>$date</td>", ('') x $col);
};
for my $comp (@date_sorted) {
my $commit = $comp->{commit_time} || 0;
my $date = DateTime->from_epoch(epoch => $commit)->ymd;
$new_row->($date) if $old_date ne $date;
my $key = $comp->{key} || $comp->{name};
my $score = $scores->{$key};
my $column = $column{$comp->{name}};
# Don't let new scores on the same date overwrite old ones
$new_row->($date) if $row[$column] ne '';
if (defined $score) {
my $class = $score > 50 ? 'bench_good' :
$score > 10 ? 'bench_bad' :
'bench_ugly' ;
$row[$column] = sprintf qq{ <td class="$class">%.1f</td>}, $score;
}
else {
$row[$column] = sprintf qq{ <td class="bench_ugly">%.1f</td>}, '--';
}
}
if (@row) {
$_ ||= ' <td></td>' for @row;
$html .= join "\n" => '<tr>', @row, "</tr>\n";
}
$html .= "</table>\n";
print $out_fh $html;
}
sub plot_header {
return <<'PLOT_HEADER';
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Benchmark Plots</title>
<link rel="stylesheet" type="text/css" href="jqplot/jquery.jqplot.min.css" />
<!--[if lt IE 9]><script language="javascript" type="text/javascript" src="jqplot/excanvas.min.js"></script><![endif]-->
<script type="text/javascript" src="jqplot/jquery.min.js"></script>
<script type="text/javascript" src="jqplot/jquery.jqplot.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.barRenderer.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.canvasAxisTickRenderer.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.canvasAxisLabelRenderer.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.canvasTextRenderer.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.categoryAxisRenderer.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.dateAxisRenderer.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.enhancedLegendRenderer.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.highlighter.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.logAxisRenderer.min.js"></script>
<script type="text/javascript">
function handleToggle (ev) {
var d=ev.data, s=d.series, replot=d.replot, plot=d.plot, speed=d.speed, sidx=s.index, showing=false;
if (s.canvas._elem.is(':hidden') || !s.show) { showing = true }
var doLegendToggle = function() {
if (replot) {
var opts = {};
if ($.isPlainObject(replot)) { $.extend(true, opts, replot) }
plot.replot(opts);
if (showing && speed) {
var s = plot.series[sidx];