@@ -184,7 +184,9 @@ my %TESTS = (
184
184
],
185
185
);
186
186
my %FORMATTER = (
187
- text => \&summarize_results,
187
+ text => \&summarize_results_text,
188
+ html => \&summarize_results_html,
189
+ html_snippet => \&summarize_results_html_snippet,
188
190
);
189
191
190
192
@@ -357,7 +359,7 @@ sub compare_times {
357
359
return \%relative ;
358
360
}
359
361
360
- sub summarize_results {
362
+ sub summarize_results_text {
361
363
my ($times , $style ) = @_ ;
362
364
363
365
$style = -t STDOUT
@@ -439,6 +441,110 @@ sub center {
439
441
return $output ;
440
442
}
441
443
444
+ sub summarize_results_html {
445
+ my ($times , $style ) = @_ ;
446
+
447
+ # Default to including style in full HTML pages
448
+ $style = 1
449
+ if $style eq ' auto' ;
450
+
451
+ print <<'HEADER' ;
452
+ <html>
453
+ <head>
454
+ <title>Perl Bench Summary</title>
455
+ </head>
456
+
457
+ <body>
458
+ HEADER
459
+
460
+ summarize_results_html_snippet($times , $style );
461
+
462
+ print <<'FOOTER' ;
463
+ </body>
464
+ </html>
465
+ FOOTER
466
+ }
467
+
468
+ sub summarize_results_html_snippet {
469
+ my ($times , $style ) = @_ ;
470
+ my $html = ' ' ;
471
+
472
+ # Default to no style info if just generating an HTML snippet
473
+ $style = 0
474
+ if $style eq ' auto' ;
475
+
476
+ if ($style ) {
477
+ $html .= <<'CSS' ;
478
+ <style type="text/css">
479
+ bench_language { text-align : center ; }
480
+ bench_compiler { text-align : center ; }
481
+ bench_vm { text-align : center ; }
482
+ bench_time { text-align : right ; font-family : monospace ; }
483
+ bench_no_time { text-align : right ; font-family : monospace ; }
484
+ bench_good { text-align : right ; font-family : monospace ; color : green ; }
485
+ bench_bad { text-align : right ; font-family : monospace ; color : yellow ; }
486
+ bench_ugly { text-align : right ; font-family : monospace ; color : red ; }
487
+ bench_skip { text-align : right ; font-family : monospace ; color : red ; }
488
+ bench_fail { text-align : right ; font-family : monospace ; color : red ; }
489
+ </style >
490
+
491
+ CSS
492
+ }
493
+
494
+ my @test_names = map { $_->{name} } @$times;
495
+ my @perls = map { @{$COMPILERS{$_}} } @GROUPS;
496
+ @perls = grep { $_->{enabled} } @perls;
497
+ my @lang_names = map { $_->{language} } @perls;
498
+ my @comp_names = map { $_->{compiler} } @perls;
499
+ my @vm_names = map { $_->{vm} } @perls;
500
+
501
+ my %lang_count;
502
+ $lang_count{$_}++ for @lang_names ;
503
+ my @langs = uniq @lang_names;
504
+
505
+ $html .= qq{<table class ="bench _summary ">\n };
506
+ $html .= "<tr > <th > </th > \n " . join('' => map qq{ <th class ="bench _language " colspan ="$lang _count {$_}"> $_</th > \n } => @langs ) . "</tr>\n";
507
+ $html .= "<tr > <th > </th > \n " . join('' => map qq{ <th class ="bench _compiler ">$_</th >\n } => @comp_names ) . "</tr>\n";
508
+ $html .= "<tr > <th > TEST</th > \n " . join('' => map qq{ <th class ="bench _vm ">$_</th >\n } => @vm_names ) . "</tr>\n";
509
+
510
+ for my $test (@$times) {
511
+ $html .= "<tr ><td >$test- >{name }</td > \n ";
512
+ for my $perl (@perls) {
513
+ my $name = $perl- >{name };
514
+ my $best = $test- > {best }{$name };
515
+ if (defined $best) {
516
+ $html .= sprintf qq { <td class ="bench _time ">%.3fs </td >\n }, $test- > {best }{$name };
517
+ }
518
+ else {
519
+ $html .= qq { <td class ="bench _no _time ">-- </td >\n };
520
+ }
521
+ }
522
+ $html .= "</tr > \n ";
523
+
524
+ $html .= "<tr > <td > </td > \n ";
525
+ for my $perl (@perls) {
526
+ my $name = $perl- >{name };
527
+ my $rel = $test- > {compare }{$name };
528
+ if (defined $rel) {
529
+ my $class = $rel < 2 ? 'bench _good ' :
530
+ $rel < 10 ? ' bench_bad' :
531
+ ' bench_ugly' ;
532
+ $html .= sprintf qq { <td class ="$class ">%.3fx </td >\n }, $rel;
533
+ }
534
+ else {
535
+ my $is _skip = grep { $_ eq $name } @{$test- >{conf }{skip } || []};
536
+ my $class = $is_skip ? 'bench_skip' : 'bench_fail';
537
+ my $message = $is_skip ? 'SKIP' : 'FAIL';
538
+ $html .= qq{ <td class ="$class ">$message </td >\n };
539
+ }
540
+ }
541
+ $html .= "</tr > \n ";
542
+ }
543
+ $html .= "</table > \n ";
544
+
545
+ print $html;
546
+ }
547
+
442
548
443
549
__END__
444
550
@@ -449,7 +555,7 @@ bench -- Benchmark Perl-family compilers against each other
449
555
450
556
=head1 SYNOPSIS
451
557
452
- bench [--help|-h|-?] [--man] [--format=text] [--style=0|1|auto]
558
+ bench [--help |-h |-?] [--man ] [--format = text|html|html_snippet ] [--style = 0|1|auto ]
453
559
454
560
455
561
=head1 DESCRIPTION
@@ -478,18 +584,23 @@ Get basic help for this program
478
584
479
585
Display this program's entire manpage
480
586
481
- =item --format=text
587
+ =item --format=text | html |html_snippet
482
588
483
- Format the summary output in a particular format. The default is 'text',
484
- which outputs a text-rendered summary table, with ANSI coloring.
589
+ Format the summary output in a particular format. The default is C<text > ,
590
+ which outputs a text-rendered summary table with ANSI coloring. HTML output
591
+ is also available, either in full document form (C<html > ), or just a snippet
592
+ containing the summary table (C<html_snippet> ).
485
593
486
594
=item --style=0|1|auto
487
595
488
596
Select whether style settings are included in the output . C<0> turns off
489
597
style output , C<1> turns it on, and C<auto> (the default) tries to DWIM.
490
598
For text output , this option selects whether ANSI color codes are used to
491
599
highlight entries in the summary table; C<auto> turns on ANSI color whenever
492
- the output is a TTY.
600
+ the output is a TTY. For HTML output , this determines whether a CSS style
601
+ block is added to the HTML (element C<class> attributes are always output ).
602
+ C<auto> defaults to adding CSS to full HTML documents (format C<html > ), and
603
+ I<not> adding it to HTML snippets (format C<html_snippet> ).
493
604
494
605
=back
495
606
0 commit comments