script/benchmark_template_engines

filter: Statementstmt 51.7% 116 / 224 Branchbran 26.0% 24 / 92 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 80.0% 12 / 15 Podpod n/a totaltotal 43.8% 152 / 347 CC 2 SCAR 5.3
CC
2
Cov
43%
__ANON__
6.0
__ANON__
2.0
__ANON__
1.1
CRAP 1.7
« lib/Template/Benchmark/Engine.pm ↑ Summary lib/Template/Benchmark/Engines/HTMLMacro.pm »
1 #!/usr/bin/perl -w
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 benchmark_template_engines - Test the relative performance of several different types of template engines.
9  
10 =cut
11  
12 # Ensure I'm using my devel modules when running from my devel dir.
13 1 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use lib "$FindBin::Bin/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use Benchmark;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Template::Benchmark;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Text::Wrap ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Text::Matrix;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # ---------------------------------------------------------------------------
24  
25 1 my $help = 0;
26 1 my $man = 0;
27  
28 1 my $progress = 0;
29 1 my $json = 0;
30 1 my $quiet = 0;
31 1 my $all = 0;
32 1 my $allfeatures = 0;
33 1 my $alltypes = 0;
34 1 my $nofeatures = 0;
35 1 my $notypes = 0;
36 1 my @featuresfrom = ();
37 1 my @typesfrom = ();
38 1 my $showtemplate = 0;
39 1 my $showsize = 0;
40 1 my $featurematrix = 0;
41 1 my $syntaxmatrix = 0;
42 1 my @inc_path = ();
43 1 my @skip_plugin = ();
44 1 my @only_plugin = ();
45  
46 1 my %get_options = (
47 'help|?|h' => \$help,
48 'man' => \$man,
49 'progress!' => \$progress,
50 'json!' => \$json,
51 'quiet!' => \$quiet,
52 'all' => \$all,
53 'allfeatures' => \$allfeatures,
54 'all_features' => \$allfeatures,
55 'alltypes' => \$alltypes,
56 'all_types' => \$alltypes,
57 'nofeatures' => \$nofeatures,
58 'no_features' => \$nofeatures,
59 'notypes' => \$notypes,
60 'no_types' => \$notypes,
61 'featuresfrom=s' => \@featuresfrom,
62 'features_from=s' => \@featuresfrom,
63 'typesfrom=s' => \@typesfrom,
64 'types_from=s' => \@typesfrom,
65 'showtemplate!' => \$showtemplate,
66 'show_template!' => \$showtemplate,
67 'showsize!' => \$showsize,
68 'show_size!' => \$showsize,
69 'featurematrix' => \$featurematrix,
70 'feature_matrix' => \$featurematrix,
71 'syntaxmatrix' => \$syntaxmatrix,
72 'syntax_matrix' => \$syntaxmatrix,
73 'I=s' => \@inc_path,
74 'skipplugin=s' => \@skip_plugin,
75 'skip_plugin=s' => \@skip_plugin,
76 'onlyplugin=s' => \@only_plugin,
77 'only_plugin=s' => \@only_plugin,
78 );
79  
80 1 my %options = Template::Benchmark->default_options();
81  
82 # -2 fudgery is to detect whether default values are passed through.
83 1 foreach my $feature ( Template::Benchmark->valid_features() )
84 {
85 # More hackery, to get --feature, --feature=N, --nofeature to coexist.
86 24 $get_options{ "${feature}:1" } = \$options{ $feature };
87 $get_options{ "no${feature}|no-${feature}" } =
88 0 ▶ sub { $options{ $feature } = 0; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
89 24 $options{ $feature } -= 2;
90 }
91 1 foreach my $type ( Template::Benchmark->valid_cache_types() )
92 {
93 6 $get_options{ "${type}!" } = \$options{ $type };
94 6 $options{ $type } -= 2;
95 }
96 1 $get_options{ 'repeats|r=i' } = \$options{ 'template_repeats' };
97 1 $get_options{ 'duration|d=f' } = \$options{ 'duration' };
98 1 $get_options{ 'dataset=s' } = \$options{ 'dataset' };
99 1 $get_options{ 'style=s' } = \$options{ 'style' };
100 1 $get_options{ 'keeptmpdirs!' } = \$options{ 'keep_tmp_dirs' };
101 1 $get_options{ 'keep_tmp_dirs!' } = \$options{ 'keep_tmp_dirs' };
102 1 $get_options{ 'skipoutputcompare!' } = \$options{ 'skip_output_compare' };
103 1 $get_options{ 'skip_output_compare!' } = \$options{ 'skip_output_compare' };
104  
105 1 Getopt::Long::Configure( 'gnu_getopt' );
106 1 ▶Getopt::Long::GetOptions( %get_options ) or pod2usage( 2 );
Branch: 1 branch
BranchTrueFalseTotal
&Getopt::Long::GetOptions(%get_options) or pod2usage(2) 1 0 1
107  
108 1 ▶pod2usage( 1 ) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 1 1
109 1 ▶pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
Branch: 1 branch
BranchTrueFalseTotal
if $man 0 1 1
110  
111 # Force quiet on if they've set json, otherwise cruft gets in.
112 1 ▶$quiet = 1 if $json;
Branch: 1 branch
BranchTrueFalseTotal
if $json 0 1 1
113  
114 1 ▶if( $all )
Branch: 1 branch
BranchTrueFalseTotal
if ($all) 0 1 1
115 {
116 0 $allfeatures = 1;
117 0 $alltypes = 1;
118 }
119  
120 1 ▶$nofeatures = 1 if @featuresfrom;
Branch: 1 branch
BranchTrueFalseTotal
if @featuresfrom 0 1 1
121 1 ▶$notypes = 1 if @typesfrom;
Branch: 1 branch
BranchTrueFalseTotal
if @typesfrom 0 1 1
122  
123 1 ▶if( $allfeatures )
Branch: 1 branch
BranchTrueFalseTotal
if ($allfeatures) 0 1 1
124 {
125 # Here we use the -2 fudgery to check if something is disabled
126 # via command-line rather than by default.
127 0 foreach my $feature ( Template::Benchmark->valid_features() )
128 {
129 0 ▶ $options{ $feature } = 1 if $options{ $feature } < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $options{$feature} < 0 0 0 0
130 }
131 }
132 1 ▶if( $alltypes )
Branch: 1 branch
BranchTrueFalseTotal
if ($alltypes) 0 1 1
133 {
134 # Here we use the -2 fudgery to check if something is disabled
135 # via command-line rather than by default.
136 0 foreach my $type ( Template::Benchmark->valid_cache_types() )
137 {
138 0 ▶ $options{ $type } = 1 if $options{ $type } < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $options{$type} < 0 0 0 0
139 }
140 }
141 1 ▶if( $nofeatures )
Branch: 1 branch
BranchTrueFalseTotal
if ($nofeatures) 1 0 1
142 {
143 # Use the -2 fudge to disable anything that isn't explicitly enabled.
144 1 foreach my $feature ( Template::Benchmark->valid_features() )
145 {
146 24 ▶ $options{ $feature } = 0 if $options{ $feature } < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $options{$feature} < 0 23 1 24
147 }
148 }
149 1 ▶if( $notypes )
Branch: 1 branch
BranchTrueFalseTotal
if ($notypes) 0 1 1
150 {
151 # Use the -2 fudge to disable anything that isn't explicitly enabled.
152 0 foreach my $type ( Template::Benchmark->valid_cache_types() )
153 {
154 0 ▶ $options{ $type } = 0 if $options{ $type } < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $options{$type} < 0 0 0 0
155 }
156 }
157  
158 # Unwind the -2 fudgery
159 1 foreach my $feature ( Template::Benchmark->valid_features() )
160 {
161 24 ▶ $options{ $feature } += 2 if $options{ $feature } < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $options{$feature} < 0 0 24 24
162 }
163 1 foreach my $type ( Template::Benchmark->valid_cache_types() )
164 {
165 6 ▶ $options{ $type } += 2 if $options{ $type } < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $options{$type} < 0 6 0 6
166 }
167  
168 1 @inc_path = split( /,/, join( ',', @inc_path ) );
169 1 $options{ skip_plugin } = [ split( /,/, join( ',', @skip_plugin ) ) ];
170 1 $options{ only_plugin } = [ split( /,/, join( ',', @only_plugin ) ) ];
171 1 ▶$options{ features_from } = [ split( /,/, join( ',', @featuresfrom ) ) ]
Branch: 1 branch
BranchTrueFalseTotal
if @featuresfrom 0 1 1
172 if @featuresfrom;
173 1 ▶$options{ cache_types_from } = [ split( /,/, join( ',', @typesfrom ) ) ]
Branch: 1 branch
BranchTrueFalseTotal
if @typesfrom 0 1 1
174 if @typesfrom;
175  
176 # ---------------------------------------------------------------------------
177  
178 1 my ( $benchmarker, $result );
179  
180 1 foreach my $path ( @inc_path )
181 {
182 0 eval "use lib '$path';";
183 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
184 }
185  
186 # TODO: failure reasons.
187 1 ▶$benchmarker = Template::Benchmark->new( %options ) or
Branch: 1 branch
BranchTrueFalseTotal
$benchmarker = "Template::Benchmark"->new(%options) or die "Unable to create Template::Benchmark object." 1 0 1
188 die "Unable to create Template::Benchmark object.";
189  
190 1 ▶if( not $quiet )
Branch: 1 branch
BranchTrueFalseTotal
unless ($quiet) 1 0 1
191 {
192 1 ▶ if( my $errors = $benchmarker->engine_errors() )
Branch: 1 branch
BranchTrueFalseTotal
if (my $errors = $benchmarker->engine_errors) 1 0 1
193 {
194 1 ▶ print _heading( 'Engine errors' ) if %{$errors};
Branch: 1 branch
BranchTrueFalseTotal
if %{$errors;} 1 0 1
195 1 foreach my $engine ( sort( keys( %{$errors} ) ) )
196 {
197 # TODO: should go to stderr?
198 30 local $Text::Wrap::columns = 80;
199 print map
200 {
201 30 Text::Wrap::wrap( '', ' ' x 23,
202 sprintf( "%-20s - %s\n", $engine, $_ ) )
203 30 } @{$errors->{ $engine }};
204 }
205 }
206 }
207  
208 1 ▶if( $featurematrix )
Branch: 1 branch
BranchTrueFalseTotal
if ($featurematrix) 1 0 1
209 {
210 1 my ( @features, @engines, @columns, %matrix );
211  
212 # Don't sort the features list, they're logically grouped.
213 1 @features = $benchmarker->features();
214 1 @engines = sort( $benchmarker->engines() );
215 0 @columns = map { Template::Benchmark::_engine_leaf( $_ ) } @engines;
216  
217 1 ▶ unless( @features )
Branch: 1 branch
BranchTrueFalseTotal
unless (@features) 0 1 1
218 {
219 0 print "No template features enabled, ",
220 "unable to produce featurematrix.\n";
221 0 exit( 0 );
222 }
223  
224 1 ▶ unless( @engines )
Branch: 1 branch
BranchTrueFalseTotal
unless (@engines) 1 0 1
225 {
226 1 print "No available template engines support that combination of ",
227 "features, unable to produce featurematrix.\n";
228 1 exit( 0 );
229 }
230  
231 0 %matrix = map { $_ => {} } @features;
232 0 foreach my $engine ( @engines )
233 {
234 0 my ( $column );
235  
236 0 $column = Template::Benchmark::_engine_leaf( $engine );
237  
238 0 foreach my $feature ( @features )
239 {
240 0 my ( $feature_syntax );
241  
242 0 $feature_syntax = $engine->feature_syntax( $feature );
243 0 ▶ $matrix{ $feature }->{ $column } =
Branch: 1 branch
BranchTrueFalseTotal
defined $feature_syntax ? : 0 0 0
244 defined( $feature_syntax ) ? 'Y' : '-';
245 }
246 }
247  
248 0 print _heading( 'Feature Matrix' ),
249 Text::Matrix->matrix( \@features, \@columns, \%matrix );
250  
251 0 exit( 0 );
252 }
253  
254 0 ▶if( $syntaxmatrix )
Branch: 1 branch
BranchTrueFalseTotal
if ($syntaxmatrix) 0 0 0
255 {
256 0 my ( %types, @engines, @rows, @cols, %matrix );
257  
258 # Don't sort the features list, they're logically grouped.
259 0 %types = ();
260 0 @engines = sort( $benchmarker->engines() );
261 0 @rows = map { Template::Benchmark::_engine_leaf( $_ ) } @engines;
262  
263 0 ▶ unless( @engines )
Branch: 1 branch
BranchTrueFalseTotal
unless (@engines) 0 0 0
264 {
265 0 print "No available template engines, ",
266 "unable to produce syntaxmatrix.\n";
267 0 exit( 0 );
268 }
269  
270 0 %matrix = map { $_ => {} } @rows;
271 0 foreach my $engine ( @engines )
272 {
273 0 my ( $row, $syntax_type );
274  
275 0 $row = Template::Benchmark::_engine_leaf( $engine );
276 0 ▶ $syntax_type = $engine->syntax_type() || 'undef';
Condition0%0/2
$engine->syntax_type || 'undef'
l!l
00
MC/DC0%0/1
$engine->syntax_type || 'undef'
$engine->syntax_type
Truth table0%0/2
A
A= $engine->syntax_type
Aresult
11
00
277  
278 0 $types{ $syntax_type }++;
279  
280 0 $matrix{ $row }->{ $syntax_type } = 1;
281 0 ▶ $matrix{ $row }->{ 'pure-perl' } = $engine->pure_perl() || 0;
Condition0%0/2
$engine->pure_perl || 0
l!l
00
MC/DC0%0/1
$engine->pure_perl || 0
$engine->pure_perl
Truth table0%0/2
A
A= $engine->pure_perl
Aresult
11
00
282 }
283  
284 0 @cols = ( 'pure-perl', sort( keys( %types ) ) );
285  
286 print _heading( 'Engine Type Matrix' ),
287 0 ▶ Text::Matrix->mapper( sub { $_ ? 'Y' : '-' } )->matrix(
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
288 \@rows, \@cols, \%matrix );
289  
290 0 exit( 0 );
291 }
292  
293  
294 0 ▶print _heading( 'Starting Benchmarks' ),
Branch: 1 branch
BranchTrueFalseTotal
unless $quiet 0 0 0
295 'ETA: ', $benchmarker->number_of_benchmarks(), ' benchmarks to run = ',
296 $benchmarker->estimate_benchmark_duration(), " seconds minimum.\n"
297 unless $quiet;
298  
299 0 ▶if( $progress and not $quiet )
Branch: 1 branch
BranchTrueFalseTotal
if ($progress and not $quiet) 0 0 0
Condition0%0/3
$progress and not $quiet
!ll&&!rl&&r
000
MC/DC0%0/2
$progress and not $quiet
$progress not $quiet
Truth table0%0/3
A and not(B)
A= $progressB= not $quiet
ABresult
0X0
100
111
300 {
301 0 eval "use Term::ProgressBar::Simple";
302 0 ▶ if( $@ )
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif ($options{'duration'} or not defined $options{'duration'}) { } 0 0 0
303 {
304 0 warn "Unable to use --progress without Term::ProgressBar::Simple:\n" .
305 "$@\nContinuing benchmarks without --progress.\n";
306 }
307 ▶ elsif( $options{ duration } or !defined( $options{ duration } ) )
Condition0%0/3
$options{"duration"} or not defined $options{"duration"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"duration"} or not defined $options{"duration"}
$options{"duration"} not defined $options{"duration"}
Truth table0%0/3
A or not(B)
A= $options{"duration"}B= not defined $options{"duration"}
ABresult
1X1
011
000
308 {
309 0 my ( $timethis, $wrapped_timethis );
310  
311 0 our $progress = Term::ProgressBar::Simple->new(
312 $benchmarker->number_of_benchmarks() );
313  
314 # Avert your eyes, this is nasty, but the only way to do
315 # this without cut-n-pasting chunks of Benchmark.pm's code.
316 0 $timethis = \&Benchmark::timethis;
317 $wrapped_timethis = sub
318 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
319 0 my $r = $timethis->( @_ );
320 0 $progress++;
321 0 $r;
322 0 };
323 {
324 0 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.2
325 0 *Benchmark::timethis = \&{$wrapped_timethis};
326 }
327 }
328 }
329  
330 0 ▶if( $json )
Branch: 1 branch
BranchTrueFalseTotal
if ($json) 0 0 0
331 {
332 # Check this _before_ we spend 10 minutes running benchmarks. ;)
333 0 eval "use JSON::Any";
334 0 ▶ die "Unable to use --json without JSON::Any:\n$@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
335 }
336  
337 0 ▶$result = $benchmarker->benchmark() or
Branch: 1 branch
BranchTrueFalseTotal
$result = $benchmarker->benchmark or die "Template::Benchmark->benchmark() failed to return a result." 0 0 0
338 die "Template::Benchmark->benchmark() failed to return a result.";
339  
340 0 ▶if( not $quiet )
Branch: 1 branch
BranchTrueFalseTotal
unless ($quiet) 0 0 0
341 {
342 0 ▶ if( my $errors = $result->{ errors } )
Branch: 1 branch
BranchTrueFalseTotal
if (my $errors = $result->{"errors"}) 0 0 0
343 {
344 0 ▶ print _heading( 'Benchmark errors' ) if %{$errors};
Branch: 1 branch
BranchTrueFalseTotal
if %{$errors;} 0 0 0
345 0 foreach my $engine ( sort( keys( %{$errors} ) ) )
346 {
347 # TODO: should go to stderr?
348 0 local $Text::Wrap::columns = 80;
349 print map
350 {
351 0 Text::Wrap::wrap( '', ' ' x 23,
352 sprintf( "%-20s - %s\n", $engine, $_ ) )
353 0 } @{$errors->{ $engine }};
354 }
355 }
356 }
357  
358 0 ▶if( $json )
Branch: 1 branch
BranchTrueFalseTotal
if ($json) { } 0 0 0
359 {
360 0 my ( $encoder );
361  
362 0 $encoder = JSON::Any->new( allow_blessed => 1 );
363  
364 # Seems allow_blessed isn't reliable across all JSON modules. :(
365 # Fudge the timings data so it isn't a blessed array any more.
366 0 foreach my $benchmark ( @{$result->{ benchmarks }} )
367 {
368 $benchmark->{ timings } = {
369 0 map { $_ => [ @{$benchmark->{ timings }->{ $_ }} ] }
370 0 keys( %{$benchmark->{ timings }} )
371 };
372 }
373  
374 0 print $encoder->encode( $result ), "\n";
375 }
376 else
377 {
378 0 ▶ if( $result->{ result } eq 'SUCCESS' )
Branch: 2 branches
BranchTrueFalseTotal
if ($result->{'result'} eq 'SUCCESS') { } 0 0 0
elsif ($result->{'result'} eq 'MISMATCHED TEMPLATE OUTPUT') { } 0 0 0
379 {
380 0 local $Text::Wrap::columns = 80;
381 0 print _heading( $result->{ title } );
382 print map
383 {
384 Text::Wrap::wrap( '', ' ' x 13,
385 sprintf( "%-10s - %s\n", $_,
386 0 $result->{ descriptions }->{ $_ } ) )
387 0 } sort( keys( %{$result->{ descriptions }} ) );
388 0 foreach my $benchmark ( @{$result->{ benchmarks }} )
389 {
390 0 print _heading( $benchmark->{ type } );
391 0 Benchmark::cmpthese( $benchmark->{ timings } );
392 }
393 0 ▶ if( $showtemplate )
Branch: 1 branch
BranchTrueFalseTotal
if ($showtemplate) 0 0 0
394 {
395 print _heading( 'Template Output' ),
396 "--CONTENT--\n", $result->{ reference }->{ output },
397 0 "--END OF CONTENT--\n";
398 }
399 0 ▶ if( $showsize )
Branch: 1 branch
BranchTrueFalseTotal
if ($showsize) 0 0 0
400 {
401 print _heading( 'Template Size' ),
402 "Template output was ",
403 0 length( $result->{ reference }->{ output } ),
404 " bytes.\n";
405 }
406 }
407 elsif( $result->{ result } eq 'MISMATCHED TEMPLATE OUTPUT' )
408 {
409 # TODO: nice diff would be handy.
410 0 print 'Benchmark failure: ', $result->{ result }, "\n";
411 print 'Reference engine was ', $result->{ reference }->{ tag },
412 0 ' for ', $result->{ reference }->{ type }, "\n";
413 print "--EXPECTED RESULT--\n", $result->{ reference }->{ output },
414 0 "--END OF EXPECTED RESULT--\n";
415 0 foreach my $failure ( @{$result->{ failures }} )
416 {
417 print 'Failed engine was ', $failure->{ tag },
418 0 ' for ', $failure->{ type }, "\n";
419 print "--FAILED RESULT--\n", $failure->{ output },
420 0 "--END OF FAILED RESULT--\n";
421 }
422
423 }
424 else
425 {
426 0 print 'Unhandled benchmark failure: ', $result->{ result }, "\n";
427 }
428 }
429  
430 sub _heading
431 1 ▶{
Subroutine _heading: called CC=1 SCAR=0.0
432 1 my ( $heading ) = @_;
433  
434 1 return( '--- ' . $heading . ' ' . ( '-' x ( 75 - length( $heading ) ) ) .
435 "\n" );
436 }
437  
438 __END__
« lib/Template/Benchmark/Engine.pm ↑ Summary lib/Template/Benchmark/Engines/HTMLMacro.pm »