blib/lib/Template/Benchmark.pm

filter: Statementstmt 73.4% 263 / 358 Branchbran 52.4% 65 / 124 Conditioncond 67.3% 33 / 49 MC/DCmcdc 46.1% 12 / 26 Subroutinesub 95.5% 43 / 45 Podpod 100% 11 / 11 totaltotal 69.6% 427 / 613 CC 95 SCAR 45.7
CC
95
Cov
68%
benchmark
462.0
new
82.4
DESTROY
8.1
CRAP 96.4
↑ Summary lib/Template/Benchmark/Engine.pm »
1 package Template::Benchmark;
2  
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use Benchmark;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use File::Path qw(mkpath rmtree);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 11 ▶use Module::Pluggable ( search_path => 'Template::Benchmark::Engines',
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 sub_name => 'engine_plugins' );
16  
17 11 our $VERSION = '1.09_01';
18  
19 11 my @valid_features = qw/
20 literal_text
21 scalar_variable
22 hash_variable_value
23 array_variable_value
24 deep_data_structure_value
25 array_loop_value
26 hash_loop_value
27 records_loop_value
28 array_loop_template
29 hash_loop_template
30 records_loop_template
31 constant_if_literal
32 variable_if_literal
33 constant_if_else_literal
34 variable_if_else_literal
35 constant_if_template
36 variable_if_template
37 constant_if_else_template
38 variable_if_else_template
39 constant_expression
40 variable_expression
41 complex_variable_expression
42 constant_function
43 variable_function
44 /;
45  
46 11 my @valid_cache_types = qw/
47 uncached_string
48 uncached_disk
49 disk_cache
50 shared_memory_cache
51 memory_cache
52 instance_reuse
53 /;
54  
55 11 my %option_defaults = (
56 # Feature options: these should only default on if they're
57 # widely supported, so that the default benchmark covers
58 # most template engines.
59 literal_text => 1,
60 scalar_variable => 1,
61 hash_variable_value => 0,
62 array_variable_value => 0,
63 deep_data_structure_value => 0,
64 array_loop_value => 0,
65 hash_loop_value => 0,
66 records_loop_value => 1,
67 array_loop_template => 0,
68 hash_loop_template => 0,
69 records_loop_template => 1,
70 constant_if_literal => 0,
71 variable_if_literal => 1,
72 constant_if_else_literal => 0,
73 variable_if_else_literal => 1,
74 constant_if_template => 0,
75 variable_if_template => 1,
76 constant_if_else_template => 0,
77 variable_if_else_template => 1,
78 constant_expression => 0,
79 variable_expression => 0,
80 complex_variable_expression => 0,
81 constant_function => 0,
82 variable_function => 0,
83  
84 # Cache types.
85 uncached_string => 1,
86 uncached_disk => 1,
87 disk_cache => 1,
88 shared_memory_cache => 1,
89 memory_cache => 1,
90 instance_reuse => 1,
91  
92 # Other options.
93 template_repeats => 30,
94 duration => 10,
95 dataset => 'original',
96 style => 'none',
97 keep_tmp_dirs => 0,
98 skip_output_compare => 0,
99  
100 # Plugin control.
101 only_plugin => {},
102 skip_plugin => {},
103 features_from => {},
104 cache_types_from => {},
105 );
106  
107 # Which engines to try first as the 'reference output' for templates.
108 # Note that this is merely a matter of author convenience: all template
109 # engine outputs must match, this merely determines which should be
110 # cited as 'correct' in the case of a mismatch. This should generally
111 # be a template engine that provides most features, otherwise it won't
112 # be an _available_ template engine when we need it.
113 # For author convenience I'm using Template::Sandbox as the prefered
114 # reference, however Template::Toolkit will make a better reference
115 # choice once this module has stabilized.
116 11 my $reference_preference = 'TS';
117  
118 11 my %datasets = (
119 original => {
120 hash1 => {
121 scalar_variable => 'I is a scalar, yarr!',
122 hash_variable => {
123 'hash_value_key' =>
124 'I spy with my little eye, something beginning with H.',
125 },
126 array_variable => [ qw/I have an imagination honest/ ],
127 this => { is => { a => { very => { deep => { hash => {
128 # No longer "it's", to avoid HTML-escaping inconsistencies
129 structure => "My god, it be full of hashes.",
130 } } } } } },
131 template_if_true => 'True dat',
132 template_if_false => 'Nay, Mister Wilks',
133 },
134 hash2 => {
135 array_loop => [ qw/five four three two one coming ready or not/ ],
136 hash_loop => {
137 aaa => 'first',
138 bbb => 'second',
139 ccc => 'third',
140 ddd => 'fourth',
141 eee => 'fifth',
142 },
143 records_loop => [
144 { name => 'Joe Bloggs', age => 16, },
145 { name => 'Fred Bloggs', age => 23, },
146 { name => 'Nigel Bloggs', age => 43, },
147 { name => 'Tarquin Bloggs', age => 143, },
148 { name => 'Geoffrey Bloggs', age => 13, },
149 ],
150 variable_if => 1,
151 variable_if_else => 0,
152 variable_expression_a => 20,
153 variable_expression_b => 10,
154 variable_function_arg => 'Hi there',
155 },
156 },
157 );
158  
159 sub new
160 19 ▶{
Subroutine new: called CC=68 SCAR=44.1
Pod documented
161 19 my $this = shift;
162 18 my ( $self, $class, $options, $var_hash1, $var_hash2, %temp_options,
163 %keep_cache_types );
164  
165 18 $self = {};
166 19 ▶ $class = ref( $this ) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0180
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
167 18 bless $self, $class;
168  
169 18 $self->{ options } = {};
170 19 $options = $self->{ options };
171 18 ▶ while( my $opt = shift )
Branch: 1 branch
BranchTrueFalseTotal
if (my $opt = shift()) 71 17 88
172 {
173 71 ▶ if( $opt eq 'only_plugin' or $opt eq 'skip_plugin' or
Branch: 1 branch
BranchTrueFalseTotal
if ($opt eq 'only_plugin' or $opt eq 'skip_plugin' or $opt eq 'features_from' or $opt eq 'cache_types_from') { } 34 37 71
Condition100%9/9
$opt eq "only_plugin" or $opt eq "skip_plugin" or $opt eq "features_from" or $opt eq "cache_types_from"
l!l&&r!l&&!r
26837
$opt eq "only_plugin" or $opt eq "skip_plugin" or $opt eq "features_from"
l!l&&r!l&&!r
18845
$opt eq "only_plugin" or $opt eq "skip_plugin"
l!l&&r!l&&!r
17153
MC/DC100%4/4
$opt eq "only_plugin" or $opt eq "skip_plugin" or $opt eq "features_from" or $opt eq "cache_types_from"
$opt eq "only_plugin" $opt eq "skip_plugin" $opt eq "features_from" $opt eq "cache_types_from"
Truth table100%5/5
A or B or C or D
A= $opt eq "only_plugin"B= $opt eq "skip_plugin"C= $opt eq "features_from"D= $opt eq "cache_types_from"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
174 $opt eq 'features_from' or $opt eq 'cache_types_from' )
175 {
176 35 my $val = shift();
177 34 ▶ $options->{ $opt } ||= {};
Condition50%1/2
$options->{$opt} ||= {}
l!l
034
MC/DC0%0/1
$options->{$opt} ||= {}
$options->{$opt}
Truth table50%1/2
A
A= $options->{$opt}
Aresult
11
00
178 34 ▶ if( ref( $val ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) { } 21 13 34
179 {
180 0 ▶ $val = [ grep { $val->{ $_ } } keys( %{$val} ) ]
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "HASH" 2 19 21
181 if ref( $val ) eq 'HASH';
182 22 foreach ( @{$val} )
183 {
184 38 $options->{ $opt }->{ $_ } = 1;
185 }
186 }
187 else
188 {
189 14 $options->{ $opt }->{ $val } = 1;
190 }
191 }
192 else
193 {
194 # TODO: should be croak
195 die "Unknown constructor option '$opt'"
196 37 ▶ unless exists $option_defaults{ $opt };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $option_defaults{$opt} 1 36 37
197 36 $self->{ options }->{ $opt } = shift();
198 }
199 }
200 18 foreach my $opt ( keys( %option_defaults ) )
201 {
202 $options->{ $opt } = $option_defaults{ $opt }
203 680 ▶ unless defined $options->{ $opt };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{$opt} 610 70 680
204 }
205  
206 delete $options->{ only_plugin }
207 17 ▶ unless scalar( keys( %{$options->{ only_plugin }} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$$options{"only_plugin"};} 1 16 17
208 delete $options->{ skip_plugin }
209 17 ▶ unless scalar( keys( %{$options->{ skip_plugin }} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$$options{"skip_plugin"};} 17 0 17
210 delete $options->{ features_from }
211 17 ▶ unless scalar( keys( %{$options->{ features_from }} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$$options{"features_from"};} 10 7 17
212 delete $options->{ cache_types_from }
213 17 ▶ unless scalar( keys( %{$options->{ cache_types_from }} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$$options{"cache_types_from"};} 10 7 17
214  
215 17 ▶ if( ref( $options->{ dataset } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $options->{'dataset'}) { } 0 17 17
216 {
217 # TODO: should be croaks really.
218 die "Option 'dataset' must be a dataset name or a hashref, got: " .
219 ref( $options->{ dataset } )
220 0 ▶ unless ref( $options->{ dataset } ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $options->{"dataset"} eq "HASH" 0 0 0
221 die "Option 'dataset' hashref is missing required 'hash1' key"
222 0 ▶ unless defined( $options->{ dataset }->{ hash1 } );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"dataset"}{"hash1"} 0 0 0
223 die "Option 'dataset' hashref is missing required 'hash2' key"
224 0 ▶ unless defined( $options->{ dataset }->{ hash2 } );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"dataset"}{"hash2"} 0 0 0
225 0 $var_hash1 = $options->{ dataset }->{ hash1 };
226 0 $var_hash2 = $options->{ dataset }->{ hash2 };
227 }
228 else
229 {
230 # TODO: should be croaks really.
231 die "Unknown dataset name '$options->{ dataset }'"
232 17 ▶ unless defined( $datasets{ $options->{ dataset } } );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $datasets{$options->{"dataset"}} 0 17 17
233 17 $var_hash1 = $datasets{ $options->{ dataset } }->{ hash1 };
234 17 $var_hash2 = $datasets{ $options->{ dataset } }->{ hash2 };
235 }
236  
237 17 ▶ if( $options->{ features_from } )
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'features_from'}) { } 7 10 17
238 {
239 7 $self->{ features } = [ @valid_features ];
240 7 foreach my $plugin ( $self->engine_plugins() )
241 {
242 231 my $leaf = _engine_leaf( $plugin );
243  
244 231 ▶ next unless $options->{ features_from }->{ $leaf };
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"features_from"}{$leaf} 219 12 231
245  
246 12 ▶ eval "use $plugin";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
247 12 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 12 12
248  
249 $self->{ features } = [ grep
250 {
251 177 defined( $plugin->feature_syntax( $_ ) )
252 12 } @{$self->{ features }} ];
253 }
254 }
255 else
256 {
257 $self->{ features } =
258 [
259 10 grep { $options->{ $_ } } @valid_features
260 ];
261 }
262 # TODO: sanity-check some features are left.
263  
264 17 ▶ if( $options->{ cache_types_from } )
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'cache_types_from'}) { } 7 10 17
265 {
266 7 $self->{ cache_types } = [ @valid_cache_types ];
267 7 %keep_cache_types = ();
268 }
269 else
270 {
271 $self->{ cache_types } =
272 10 [ grep { $options->{ $_ } } @valid_cache_types ];
273 }
274 # TODO: sanity-check some cache_types are left.
275  
276 17 $self->{ engines } = [];
277 17 $self->{ engine_errors } = {};
278 17 foreach my $plugin ( $self->engine_plugins() )
279 {
280 558 my $leaf = _engine_leaf( $plugin );
281  
282 # Force-require any features_from or cache_types_from plugin,
283 # regardless of their only_plugin or skip_plugin settings.
284 558 ▶ if( ( not $options->{ features_from } or
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"features_from"} and $options->{"features_from"}{$leaf} or $options->{"cache_types_from"} and $options->{"cache_types_from"}{$leaf}) 534 24 558
Condition100%9/9
$options->{"features_from"} and $options->{"features_from"}{$leaf} or $options->{"cache_types_from"} and $options->{"cache_types_from"}{$leaf}
l!l&&r!l&&!r
1212534
$options->{"features_from"} and $options->{"features_from"}{$leaf}
!ll&&!rl&&r
32721912
$options->{"cache_types_from"} and $options->{"cache_types_from"}{$leaf}
!ll&&!rl&&r
31521912
MC/DC100%4/4
$options->{"features_from"} and $options->{"features_from"}{$leaf} or $options->{"cache_types_from"} and $options->{"cache_types_from"}{$leaf}
$options->{"features_from"} $options->{"features_from"}{$leaf} $options->{"cache_types_from"} $options->{"cache_types_from"}{$leaf}
Truth table71%5/7
A and B or C and D
A= $options->{"features_from"}B= $options->{"features_from"}{$leaf}C= $options->{"cache_types_from"}D= $options->{"cache_types_from"}{$leaf}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
285 not $options->{ features_from }->{ $leaf } ) and
286 ( not $options->{ cache_types_from } or
287 not $options->{ cache_types_from }->{ $leaf } ) )
288 {
289 534 ▶ if( $options->{ only_plugin } )
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"only_plugin"}) 504 30 534
290 {
291 504 ▶ next unless $options->{ only_plugin }->{ $leaf };
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"only_plugin"}{$leaf} 501 3 504
292 }
293 33 ▶ if( $options->{ skip_plugin } )
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"skip_plugin"}) 0 33 33
294 {
295 0 ▶ next if $options->{ skip_plugin }->{ $leaf };
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"skip_plugin"}{$leaf} 0 0 0
296 }
297 }
298 57 ▶ eval "use $plugin";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
299 57 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 30 27 57
300 {
301 30 $self->engine_error( $leaf, "Engine plugin load failure: $@" );
302 }
303 else
304 {
305 27 push @{$self->{ engines }}, $plugin;
306 }
307 }
308  
309 %temp_options = (
310 17 TMPDIR => 1,
311 );
312 17 ▶ $temp_options{ CLEANUP } = 0 if $self->{ options }->{ keep_tmp_dirs };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"options"}{"keep_tmp_dirs"} 0 17 17
313  
314 $self->{ file_temp } =
315 17 ▶ File::Temp->newdir( 'benchmark_XXXX', %temp_options )
Branch: 1 branch
BranchTrueFalseTotal
$self->{"file_temp"} = "File::Temp"->newdir("benchmark_XXXX", %temp_options) or die "Unable to create File::Temp" 17 0 17
316 or die "Unable to create File::Temp";
317  
318 $self->{ template_dir } = File::Spec->catfile(
319 17 $self->{ file_temp }->dirname(),
320 'templates',
321 );
322 $self->{ cache_dir } = File::Spec->catfile(
323 17 $self->{ file_temp }->dirname(),
324 'caches',
325 );
326 # TODO: failure check.
327 mkpath( $self->{ template_dir } )
328 17 ▶ or die "Unable to make template dir '$self->{ template_dir }': $!";
Branch: 1 branch
BranchTrueFalseTotal
mkpath($self->{"template_dir"}) or die qq[Unable to make template dir '$self->{"template_dir"}': $!] 17 0 17
329 mkpath( $self->{ cache_dir } )
330 17 ▶ or die "Unable to make cache dir '$self->{ cache_dir }': $!";
Branch: 1 branch
BranchTrueFalseTotal
mkpath($self->{"cache_dir"}) or die qq[Unable to make cache dir '$self->{"cache_dir"}': $!] 17 0 17
331  
332 $self->{ feature_repeats } =
333 {
334 map
335 {
336 $_ =>
337 (
338 # Retain positive integer values.
339 ( Scalar::Util::looks_like_number( $options->{ $_ } ) &&
340 ( int( $options->{ $_ } ) == $options->{ $_ } ) &&
341 ( $options->{ $_ } > 0 ) ) ?
342 $options->{ $_ } :
343 # Normalize the rest to 1 or 0 based on true/false.
344 # TODO: probably should warn.
345 82 ▶ ( $options->{ $_ } ? 1 : 0 )
Branch: 2 branches
BranchTrueFalseTotal
&Scalar::Util::looks_like_number($options->{$_}) && int $options->{$_} == $options->{$_} && $options->{$_} > 0 ? : 79 3 82
$options->{$_} ? : 0 3 3
Condition50%3/6
&Scalar::Util::looks_like_number($options->{$_}) and int $options->{$_} == $options->{$_} and $options->{$_} > 0
!ll&&!rl&&r
0379
&Scalar::Util::looks_like_number($options->{$_}) and int $options->{$_} == $options->{$_}
!ll&&!rl&&r
0082
MC/DC33%1/3
&Scalar::Util::looks_like_number($options->{$_}) and int $options->{$_} == $options->{$_} and $options->{$_} > 0
&Scalar::Util::looks_like_number($options->{$_}) int $options->{$_} == $options->{$_} $options->{$_} > 0
Truth table50%2/4
A and B and C
A= &Scalar::Util::looks_like_number($options->{$_})B= int $options->{$_} == $options->{$_}C= $options->{$_} > 0
ABCresult
0XX0
10X0
1100
1111
346 )
347 17 } @{$self->{ features }}
348 };
349  
350 17 $self->{ templates } = {};
351 17 $self->{ benchmark_functions } = {};
352 17 $self->{ descriptions } = {};
353 17 $self->{ engine_for_tag } = {};
354 17 ENGINE: foreach my $engine ( @{$self->{ engines }} )
355 {
356 27 my ( %benchmark_functions, $template_dir, $cache_dir, $template,
357 $template_filename, $fh, $descriptions, $missing_syntaxes, $leaf );
358  
359 27 $leaf = _engine_leaf( $engine );
360  
361 $template_dir =
362 27 File::Spec->catfile( $self->{ template_dir }, $leaf );
363 $cache_dir =
364 27 File::Spec->catfile( $self->{ cache_dir }, $leaf );
365 # TODO: failure check
366 27 mkpath( $template_dir );
367 27 mkpath( $cache_dir );
368  
369 27 foreach my $cache_type ( @{$self->{ cache_types }} )
370 {
371 162 my ( $method, @method_args, $functions );
372  
373 162 $method = "benchmark_functions_for_${cache_type}";
374  
375 162 ▶ next unless $engine->can( $method );
Branch: 1 branch
BranchTrueFalseTotal
unless $engine->can($method) 123 39 162
376  
377 39 @method_args = ();
378 39 ▶ push @method_args, $template_dir
Branch: 1 branch
BranchTrueFalseTotal
unless $cache_type eq "uncached_string" 16 23 39
379 unless $cache_type eq 'uncached_string';
380 39 ▶ push @method_args, $cache_dir
Branch: 1 branch
BranchTrueFalseTotal
unless $cache_type =~ /^uncached/o 8 31 39
381 unless $cache_type =~ /^uncached/o;
382  
383 39 eval { $functions = $engine->$method( @method_args ); };
384 39 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 39 39
385 {
386 0 $self->engine_error( $leaf,
387 "Error calling ${method}(): $@" );
388 0 next;
389 }
390  
391 39 ▶ next unless $functions and scalar( keys( %{$functions} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $functions and scalar keys %{$functions;} 0 39 39
Condition50%1/2
$functions and scalar keys %{$functions;}
!ll
039
MC/DC0%0/1
$functions and scalar keys %{$functions;}
$functions
Truth table50%1/2
A
A= $functions
Aresult
00
11
392 39 $benchmark_functions{ $cache_type } = $functions;
393 }
394  
395 35 %keep_cache_types = map { $_ => 1 }
396 keys( %keep_cache_types ),
397 72 grep { $benchmark_functions{ $_ } } @valid_cache_types
398 if $options->{ cache_types_from } and
399 27 ▶ $options->{ cache_types_from }->{ $leaf };
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"cache_types_from"} and $options->{"cache_types_from"}{$leaf} 12 15 27
Condition66%2/3
$options->{"cache_types_from"} and $options->{"cache_types_from"}{$leaf}
!ll&&!rl&&r
15012
MC/DC50%1/2
$options->{"cache_types_from"} and $options->{"cache_types_from"}{$leaf}
$options->{"cache_types_from"} $options->{"cache_types_from"}{$leaf}
Truth table66%2/3
A and B
A= $options->{"cache_types_from"}B= $options->{"cache_types_from"}{$leaf}
ABresult
0X0
100
111
400  
401 27 ▶ unless( %benchmark_functions )
Branch: 1 branch
BranchTrueFalseTotal
unless (%benchmark_functions) 0 27 27
402 {
403 0 $self->engine_error( $leaf, 'No matching benchmark functions.' );
404 0 next ENGINE;
405 }
406  
407 #print "Looking at $leaf.\n";
408 #use Data::Dumper;
409 #print " features: " . Data::Dumper::Dumper( $self->{ features } ) . "\n";
410  
411 27 $template = '';
412 27 $missing_syntaxes = '';
413 27 foreach my $feature ( @{$self->{ features }} )
414 {
415 132 my ( $feature_syntax );
416  
417 132 $feature_syntax = $engine->feature_syntax( $feature );
418 132 ▶ if( defined( $feature_syntax ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $feature_syntax) { } 132 0 132
419 {
420 $template .= ( $feature_syntax . "\n" ) x
421 132 ▶ ( $self->{ feature_repeats }->{ $feature } || 1 );
Condition100%2/2
$self->{'feature_repeats'}{$feature} || 1
l!l
1284
MC/DC100%1/1
$self->{'feature_repeats'}{$feature} || 1
$self->{'feature_repeats'}{$feature}
Truth table100%2/2
A
A= $self->{'feature_repeats'}{$feature}
Aresult
11
00
422 }
423 else
424 {
425 0 $missing_syntaxes .= ' ' . $feature;
426 }
427 }
428  
429 27 ▶ if( $missing_syntaxes )
Branch: 1 branch
BranchTrueFalseTotal
if ($missing_syntaxes) 0 27 27
430 {
431 0 $self->engine_error( $leaf,
432 "No syntaxes provided for:$missing_syntaxes." );
433 0 next ENGINE;
434 }
435  
436 27 $template = $template x $options->{ template_repeats };
437 # Allow the plugin a chance to rewrite the repeated sections,
438 # ie: some engines require unique loop names/labels.
439 27 ▶ $template = $engine->preprocess_template( $template )
Branch: 1 branch
BranchTrueFalseTotal
if $engine->can("preprocess_template") 0 27 27
440 if $engine->can( 'preprocess_template' );
441  
442 27 $template_filename =
443 File::Spec->catfile( $template_dir, $leaf . '.txt' );
444 27 $fh = IO::File->new( "> $template_filename" );
445 27 ▶ unless( $fh )
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 27 27
446 {
447 0 $self->engine_error( $leaf,
448 "Unable to write $template_filename: $!" );
449 0 next ENGINE;
450 }
451 27 $fh->print( $template );
452 27 $fh->close();
453  
454 27 $template_filename = $leaf . '.txt';
455  
456 27 $descriptions = $engine->benchmark_descriptions();
457  
458 27 foreach my $type ( keys( %benchmark_functions ) )
459 {
460 39 ▶ $self->{ benchmark_functions }->{ $type } ||= {};
Condition100%2/2
$self->{"benchmark_functions"}{$type} ||= {}
l!l
1227
MC/DC100%1/1
$self->{"benchmark_functions"}{$type} ||= {}
$self->{"benchmark_functions"}{$type}
Truth table100%2/2
A
A= $self->{"benchmark_functions"}{$type}
Aresult
11
00
461  
462 39 foreach my $tag ( keys( %{$benchmark_functions{ $type }} ) )
463 {
464 39 my ( $function );
465  
466 39 $function = $benchmark_functions{ $type }->{ $tag };
467 39 ▶ if( $type =~ /_string$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /_string$/) { } 23 16 39
468 {
469 $self->{ benchmark_functions }->{ $type }->{ $tag } =
470 sub
471 1 ▶ {
Subroutine __ANON__: called CC=1 SCAR=6.9
472 0 $function->( $template,
473 $var_hash1, $var_hash2 );
474 23 };
475 }
476 else
477 {
478 $self->{ benchmark_functions }->{ $type }->{ $tag } =
479 sub
480 1 ▶ {
Subroutine __ANON__: called CC=1 SCAR=6.9
481 0 $function->( $template_filename,
482 $var_hash1, $var_hash2 );
483 16 };
484 }
485 # TODO: warn on duplicates.
486 39 $self->{ descriptions }->{ $tag } = $descriptions->{ $tag };
487 39 $self->{ engine_for_tag }->{ $tag } = $leaf;
488 }
489 }
490 }
491  
492 17 ▶ if( $options->{ cache_types_from } )
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"cache_types_from"}) 7 10 17
493 {
494 # We need to delete any benchmark functions that crept in
495 # before we figured out what cache types we needed.
496 $self->{ benchmark_functions } = {
497 18 map { $_ => $self->{ benchmark_functions }->{ $_ } }
498 18 grep { $keep_cache_types{ $_ } }
499 7 keys( %{$self->{ benchmark_functions }} )
500 };
501 }
502  
503 # Strip any cache types that ended up with no functions.
504 $self->{ cache_types } = [
505 102 grep { $self->{ benchmark_functions }->{ $_ } }
506 17 @{$self->{ cache_types }}
507 ];
508  
509 17 return( $self );
510 }
511  
512 sub benchmark
513 1 ▶{
Subroutine benchmark: called CC=21 SCAR=61.4
Pod documented
514 0 my ( $self ) = @_;
515 0 my ( $duration, $style, $result, $reference, @outputs, $errors );
516  
517 0 $duration = $self->{ options }->{ duration };
518 0 $style = $self->{ options }->{ style };
519 0 $errors = {};
520  
521 # First up, check each benchmark function produces the same
522 # output as all the others. This also serves to ensure that
523 # the caches become populated for those benchmarks that are
524 # cached.
525 # We run the benchmark function twice, and use the output
526 # of the second, this is to make sure we're using the output
527 # of the cached template, otherwise we could end up with a
528 # function that produces the right output when building the
529 # cache but then benchmarks insanely well because there's
530 # an error in running the cached version so it no-ops all
531 # the expensive work.
532 0 @outputs = ();
533 0 $reference = 0;
534 0 foreach my $type ( @{$self->{ cache_types }} )
535 {
536 0 foreach my $tag
537 0 ( keys( %{$self->{ benchmark_functions }->{ $type }} ) )
538 {
539 0 my ( $output );
540  
541 # First to cache.
542 0 eval { $self->{ benchmark_functions }->{ $type }->{ $tag }->(); };
543 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
544 {
545 $self->engine_error(
546 0 $self->{ engine_for_tag }->{ $tag },
547 "Error running benchmark function for $tag: $@",
548 $errors );
549 0 delete $self->{ benchmark_functions }->{ $type }->{ $tag };
550 0 next;
551 }
552 # And second for output.
553 0 $output = eval {
554 0 $self->{ benchmark_functions }->{ $type }->{ $tag }->();
555 };
556 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
557 {
558 $self->engine_error(
559 0 $self->{ engine_for_tag }->{ $tag },
560 "Error running benchmark function for $tag: $@",
561 $errors );
562 0 delete $self->{ benchmark_functions }->{ $type }->{ $tag };
563 0 next;
564 }
565 0 ▶ $output = ${$output} if $output and ref( $output );
Branch: 1 branch
BranchTrueFalseTotal
if $output and ref $output 0 0 0
Condition0%0/3
$output and ref $output
!ll&&!rl&&r
000
MC/DC0%0/2
$output and ref $output
$output ref $output
Truth table0%0/3
A and B
A= $outputB= ref $output
ABresult
0X0
100
111
566 # [rt #59247] Normalize newline endings, some template engines
567 # produce UNIX and some Windows line-endings when on Windows.
568 0 ▶ $output =~ s/\r//g if $output;
Branch: 1 branch
BranchTrueFalseTotal
if $output 0 0 0
569 0 push @outputs, [ $type, $tag, $output ];
570 0 ▶ $reference = $#outputs if $tag eq $reference_preference;
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq $reference_preference 0 0 0
571 }
572 # Prune if all our functions have errored and been pruned.
573 delete $self->{ benchmark_functions }->{ $type }
574 0 ▶ unless %{$self->{ benchmark_functions }->{ $type }};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$self->{"benchmark_functions"}{$type};} 0 0 0
575 }
576  
577 # Strip any cache types that ended up with no functions.
578 $self->{ cache_types } = [
579 0 grep { $self->{ benchmark_functions }->{ $_ } }
580 0 @{$self->{ cache_types }}
581 ];
582  
583 0 ▶ unless( @outputs )
Branch: 1 branch
BranchTrueFalseTotal
unless (@outputs) 0 0 0
584 {
585 0 $result =
586 {
587 result => 'NO BENCHMARKS TO RUN',
588 };
589 0 ▶ $result->{ errors } = $errors if %{$errors};
Branch: 1 branch
BranchTrueFalseTotal
if %{$errors;} 0 0 0
590 0 return( $result );
591 }
592  
593 #use Data::Dumper;
594 #print "Outputs: ", Data::Dumper::Dumper( \@outputs ), "\n";
595  
596 # TODO: this nasty hackery is surely telling me I need a
597 # Template::Benchmark::Result object.
598 $result = {
599 result => 'MISMATCHED TEMPLATE OUTPUT',
600 reference =>
601 {
602 type => $outputs[ $reference ]->[ 0 ],
603 tag => $outputs[ $reference ]->[ 1 ],
604 output => $outputs[ $reference ]->[ 2 ],
605 },
606 0 descriptions => { %{$self->{ descriptions }} },
607 failures => [],
608 };
609 0 ▶ $result->{ errors } = $errors if %{$errors};
Branch: 1 branch
BranchTrueFalseTotal
if %{$errors;} 0 0 0
610  
611 0 ▶ unless( $self->{ options }->{ skip_output_compare } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"options"}{"skip_output_compare"}) 0 0 0
612 {
613 0 foreach my $output ( @outputs )
614 {
615 0 ▶ push @{$result->{ failures }},
Branch: 1 branch
BranchTrueFalseTotal
defined $output->[2] ? : 0 0 0
616 {
617 type => $output->[ 0 ],
618 tag => $output->[ 1 ],
619 output => defined( $output->[ 2 ] ) ?
620 $output->[ 2 ] : "[no content returned]\n",
621 }
622 if !defined( $output->[ 2 ] ) or
623 0 ▶ $output->[ 2 ] ne $result->{ reference }->{ output };
Branch: 1 branch
BranchTrueFalseTotal
if not defined $output->[2] or $output->[2] ne $result->{"reference"}{"output"} 0 0 0
Condition0%0/3
not defined $output->[2] or $output->[2] ne $result->{"reference"}{"output"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $output->[2] or $output->[2] ne $result->{"reference"}{"output"}
not defined $output->[2] $output->[2] ne $result->{"reference"}{"output"}
Truth table0%0/3
not(A) or B
A= not defined $output->[2]B= $output->[2] ne $result->{"reference"}{"output"}
ABresult
1X1
011
000
624 }
625  
626 0 ▶ return( $result ) unless $#{$result->{ failures }} == -1;
Branch: 1 branch
BranchTrueFalseTotal
unless $#{$$result{"failures"};} == -1 0 0 0
627 }
628  
629 # OK, all template output matched, time to do the benchmarks.
630  
631 0 delete $result->{ failures };
632 0 $result->{ result } = 'SUCCESS';
633  
634 0 $result->{ start_time } = time();
635 $result->{ title } = 'Template Benchmark @' .
636 0 localtime( $result->{ start_time } );
637  
638 0 $result->{ benchmarks } = [];
639 0 ▶ if( $duration )
Branch: 1 branch
BranchTrueFalseTotal
if ($duration) 0 0 0
640 {
641 0 foreach my $type ( @{$self->{ cache_types }} )
642 {
643 0 my ( $timings, $comparison );
644  
645 $timings = Benchmark::timethese( -$duration,
646 0 $self->{ benchmark_functions }->{ $type }, $style );
647 0 $comparison = Benchmark::cmpthese( $timings, $style );
648  
649 0 push @{$result->{ benchmarks }},
650 {
651 type => $type,
652 timings => $timings,
653 comparison => $comparison,
654 };
655 }
656 }
657  
658 0 return( $result );
659 }
660  
661 sub DESTROY
662 19 ▶{
Subroutine DESTROY: called CC=4 SCAR=20.9
663 18 my ( $self ) = @_;
664  
665 # Use a DESTROY to clean up, so that we occur in case of errors.
666 18 ▶ if( $self->{ options }->{ keep_tmp_dirs } )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'options'}{'keep_tmp_dirs'}) { } 0 18 18
667 {
668 print "Not removing cache dir ", $self->{ cache_dir }, "\n"
669 0 ▶ if $self->{ cache_dir };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache_dir"} 0 0 0
670 print "Not removing template dir ", $self->{ template_dir }, "\n"
671 0 ▶ if $self->{ template_dir };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"template_dir"} 0 0 0
672 }
673 else
674 {
675 # Try to make our benchmark closures go out of scope so they
676 # release any locks before we try to delete the temp dirs...
677 18 delete $self->{ benchmark_functions };
678 }
679 }
680  
681 2 ▶sub default_options { return( %option_defaults ); }
Subroutine default_options: called CC=1 SCAR=0.0
Pod documented
682 3 ▶sub valid_cache_types { return( @valid_cache_types ); }
Subroutine valid_cache_types: called CC=1 SCAR=0.0
Pod documented
683 4 ▶sub valid_features { return( @valid_features ); }
Subroutine valid_features: called CC=1 SCAR=0.0
Pod documented
684  
685 sub engines
686 4 ▶{
Subroutine engines: called CC=1 SCAR=0.0
Pod documented
687 3 my ( $self ) = @_;
688 3 return( @{$self->{ engines }} );
689 }
690  
691 sub features
692 1 ▶{
Subroutine features: called CC=1 SCAR=0.0
Pod documented
693 1 my ( $self ) = @_;
694 1 return( @{$self->{ features }} );
695 }
696  
697 sub engine_errors
698 1 ▶{
Subroutine engine_errors: called CC=1 SCAR=0.0
Pod documented
699 1 my ( $self ) = @_;
700 1 return( $self->{ engine_errors } );
701 }
702  
703 sub engine_error
704 30 ▶{
Subroutine engine_error: called CC=3 SCAR=11.2
Pod documented
705 30 my ( $self, $engine, $error, $errors ) = @_;
706 30 my ( $leaf );
707  
708 30 ▶ $errors = $self->{ engine_errors } unless $errors;
Branch: 1 branch
BranchTrueFalseTotal
unless $errors 30 0 30
709 30 $leaf = _engine_leaf( $engine );
710  
711 # TODO: warn if an option asks us to?
712  
713 30 ▶ $errors->{ $leaf } ||= [];
Condition50%1/2
$errors->{$leaf} ||= []
l!l
030
MC/DC0%0/1
$errors->{$leaf} ||= []
$errors->{$leaf}
Truth table50%1/2
A
A= $errors->{$leaf}
Aresult
11
00
714 30 push @{$errors->{ $leaf }}, $error;
715 }
716  
717 sub number_of_benchmarks
718 0 ▶{
Subroutine number_of_benchmarks: not called CC=2 SCAR=17.9
Pod documented
719 0 my ( $self ) = @_;
720 0 my ( $num_benchmarks );
721  
722 0 $num_benchmarks = 0;
723 0 foreach my $type ( @{$self->{ cache_types }} )
724 {
725 $num_benchmarks +=
726 0 scalar( keys( %{$self->{ benchmark_functions }->{ $type }} ) );
727 }
728  
729 0 return( $num_benchmarks );
730 }
731  
732 sub estimate_benchmark_duration
733 0 ▶{
Subroutine estimate_benchmark_duration: not called CC=1 SCAR=6.9
Pod documented
734 0 my ( $self ) = @_;
735 0 my ( $duration );
736  
737 0 $duration = $self->{ options }->{ duration };
738  
739 0 return( $duration * $self->number_of_benchmarks() );
740 }
741  
742 sub _engine_leaf
743 847 ▶{
Subroutine _engine_leaf: called CC=2 SCAR=7.0
744 847 my ( $engine ) = @_;
745  
746 847 $engine =~ /\:\:([^\:]*)$/;
747 847 ▶ return( $1 || $engine );
Condition66%2/3
$1 || $engine
l!l&&r!l&&!r
817300
MC/DC0%0/2
$1 || $engine
$1 $engine
Truth table66%2/3
A || B
A= $1B= $engine
ABresult
1X1
011
000
748 }
749  
750 11 1;
751  
752 __END__
↑ Summary lib/Template/Benchmark/Engine.pm »