13 use Benchmark qw(:hireswallclock cmpthese);
15 my $opts = parse_options() ;
23 expected => 'hehe bob',
24 simple => 'hehe [% name %]',
25 toolkit => 'hehe [% name %]',
26 teeny => 'hehe [% name %]',
38 title => 'Something new',
43 expected => <<EXPECTED,
45 <head><title>Bobs Blog</title></head>
53 <h3>Something new</h3>
54 <span>3 Days ago</span>
63 <head><title>[% title %]</title></head>
68 <span>[% date %]</span>
77 <head><title>[% title %]</title></head>
80 [% FOREACH post = posts %]
82 <h3>[% post.title %]</h3>
83 <span>[% post.date %]</span>
93 <head><title>[% title %]</title></head>
95 <ul>[% SECTION post %]
98 <span>[% date %]</span>
111 template_key => 'simple',
113 return eval { require Template::Simple } ;
116 my( $bench, $info ) = @_ ;
118 my $template = $info->{$bench->{template_key}} ;
119 my $data = $info->{data} ;
120 my $name = $info->{name} ;
122 my $obj = Template::Simple->new(
123 templates => { $name => $template }
127 sub { $obj->render( $name, $data ) } ;
130 my( $bench, $info ) = @_ ;
131 my $result = $bench->{render}->() ;
132 $bench->{result} = ${$result} ;
136 name => 'T::S compiled',
137 template_key => 'simple',
139 return eval { require Template::Simple } ;
142 my( $bench, $info ) = @_ ;
144 my $template = $info->{$bench->{template_key}} ;
145 my $data = $info->{data} ;
146 my $name = $info->{name} ;
148 my $obj = Template::Simple->new(
149 templates => { $name => $template }
151 $obj->compile( $name ) ;
154 sub { $obj->render( $name, $data ) } ;
157 my( $bench, $info ) = @_ ;
158 my $result = $bench->{render}->() ;
159 $bench->{result} = ${$result} ;
164 template_key => 'teeny',
167 require Template::Teeny ;
168 require Template::Teeny::Stash ;
172 my( $bench, $info ) = @_ ;
174 my $template = $info->{$bench->{template_key}} ;
175 my $data = $info->{data} ;
176 my $name = $info->{name} ;
179 open my $fh, '>', \$results or
180 die "can't open string for output" ;
183 write_file( "tpl/$name.tpl", $template ) ;
184 my $obj = Template::Teeny->new(
185 { include_path => ['tpl'] }
189 if ( my $posts = $data->{posts} ) {
191 $stash = Template::Teeny::Stash->new(
192 { title => $data->{title} }
195 foreach my $post ( @{$posts} ) {
198 Template::Teeny::Stash->new(
201 $stash->add_section('post', $substash );
205 $stash = Template::Teeny::Stash->new(
211 #print Dumper $stash ;
214 $obj->process("$name.tpl", $stash, $fh );
215 $bench->{result} = $results ;
220 my( $bench, $info ) = @_ ;
221 $bench->{result} = $bench->{render}->() ;
230 foreach my $info ( @{$template_info} ) {
234 foreach my $bench ( @{$benches} ) {
236 my $loaded = $bench->{load}->() ;
239 Skipping $bench->{name} as it didn't load
244 $bench->{setup}->( $bench, $info ) ;
246 if ( $opts->{verify} ) {
247 $bench->{verify}->( $bench, $info ) ;
249 if ( $bench->{result} ne $info->{expected} ) {
252 Skipping $bench->{name} as it doesn't have verified results.
253 RESULT [$bench->{result}]\nEXPECTED [$info->{expected}]
259 '$bench->{name}' rendering of '$info->{name}' is verified
264 $compares{ $bench->{name} } = $bench->{render} ;
267 print "\nBenchmark of '$info->{name}' template\n" ;
268 cmpthese( $opts->{iterations}, \%compares ) ;
275 GetOptions( \my %opts,
282 usage( '' ) if $opts{ 'help' } ;
284 $opts{iterations} ||= -2 ;
286 $opts{templaters} = [split /,/, $opts{templaters}]
287 if $opts{templaters} ;
294 my $err_msg = shift || '' ;
296 my $usage = <<'=cut' ;
298 bench_templates.pl - Benchmark Multiple Templaters
302 load_menus.pl [--verify | -v] [--iterations | -i <iter>]
303 [--templaters| -t <templaters>] [--help]
307 --rid | -r <rids> Select the restaurant ID to be fetched.
308 Can be a comma separated list of rids.
309 'ALL' will select all the valid
310 ID's from a source. This is
311 required (unless <source> is set
314 [--file | -f] Load the XML from files. The file is
316 [--help | ?] Print this help text
320 $usage =~ s/^=\w+.*$//mg ;
322 $usage =~ s/\n{2,}/\n\n/g ;
323 $usage =~ s/\A\n+// ;
325 die "$err_msg\n$usage" ;