]>
Dogcows Code - chaz/p5-CGI-Ex/blob - samples/benchmark/bench_various_templaters.pl
5 bench_various_templaters.pl - test the relative performance of several different types of template engines.
10 use Benchmark
qw(timethese cmpthese);
13 $file =~ s
|[^/]+$|WrapEx
.pm
|;
18 use Template
::Stash
::XS
;
21 use HTML
::Template
::Expr
;
22 use HTML
::Template
::JIT
;
23 use CGI
::Ex
::Dump
qw(debug);
24 use CGI
::Ex
::Template
;
25 use CGI
::Ex
::Template
::XS
;
27 use File
::Path
qw(mkpath rmtree);
30 my $dir2 = "$dir.cache";
33 END {rmtree
$dir; rmtree
$dir2};
38 pass_in_something
=> 'what ever you want',
41 ###----------------------------------------------------------------###
44 shell_header
=> "This is a header",
45 shell_footer
=> "This is a footer",
46 shell_start
=> "<html>",
48 a_stuff
=> [qw(one two three four)],
52 shell_header
=> "This is a header",
53 shell_footer
=> "This is a footer",
54 shell_start
=> "<html>",
56 a_stuff
=> [map {{name
=> $_}} qw(one two three four)],
59 $FOO::shell_header
= $FOO::shell_footer
= $FOO::shell_start
= $FOO::shell_end
= $FOO::a_stuff
;
60 $FOO::shell_header
= "This is a header";
61 $FOO::shell_footer
= "This is a footer";
62 $FOO::shell_start
= "<html>";
63 $FOO::shell_end
= "<end>";
64 $FOO::a_stuff
= [qw(one two three four)];
67 ###----------------------------------------------------------------###
70 my $content_tt = q{[% shell_header %]
77 [% FOREACH i IN a_stuff %][% i %][% END %]
78 [% pass_in_something %]
84 if (open (my $fh, ">$dir/foo.tt")) {
85 print $fh $content_tt;
89 ###----------------------------------------------------------------###
90 ### HTML::Template style
92 my $content_ht = q{<TMPL_VAR NAME=shell_header>
93 <TMPL_VAR NAME=shell_start>
99 <TMPL_LOOP NAME=a_stuff><TMPL_VAR NAME=name></TMPL_LOOP>
100 <TMPL_VAR NAME=pass_in_something>
102 <TMPL_VAR NAME=shell_end>
103 <TMPL_VAR NAME=shell_footer>
106 if (open (my $fh, ">$dir/foo.ht")) {
107 print $fh $content_ht;
111 ###----------------------------------------------------------------###
112 ### Text::Template style template
114 my $content_p = q{{$shell_header}
124 { $OUT .= $_ foreach @$a_stuff; }
131 ###----------------------------------------------------------------###
132 ### setup the objects
134 my $tt = Template-
>new({
135 INCLUDE_PATH
=> \
@dirs,
136 STASH
=> Template
::Stash-
>new($stash_t),
139 my $ttx = Template-
>new({
140 INCLUDE_PATH
=> \
@dirs,
141 STASH
=> Template
::Stash
::XS-
>new($stash_t),
144 my $ct = CGI
::Ex
::Template-
>new({
145 INCLUDE_PATH
=> \
@dirs,
146 VARIABLES
=> $stash_t,
149 my $ctx = CGI
::Ex
::Template
::XS-
>new({
150 INCLUDE_PATH
=> \
@dirs,
151 VARIABLES
=> $stash_t,
154 my $pt = Text
::Template-
>new(TYPE
=> 'STRING', SOURCE
=> $content_p, HASH
=> $form);
156 my $ht = HTML
::Template-
>new(type
=> 'scalarref', source
=> \
$content_ht);
157 $ht->param($stash_ht);
160 my $hte = HTML
::Template
::Expr-
>new(type
=> 'scalarref', source
=> \
$content_ht);
161 $hte->param($stash_ht);
164 my $ht_c = HTML
::Template-
>new(type
=> 'filename', source
=> "foo.ht", cache
=> 1, path
=> \
@dirs);
165 $ht_c->param($stash_ht);
168 my $ht_j = HTML
::Template
::JIT-
>new(filename
=> "foo.ht", path
=> \
@dirs, jit_path
=> $dir2);
169 $ht_j->param($stash_ht);
172 ###----------------------------------------------------------------###
173 ### make sure everything is ok by trying it once
176 $tt->process(\
$content_tt, $form, \
$out_tt);
179 $ttx->process(\
$content_tt, $form, \
$out_ttx);
182 $ct->process(\
$content_tt, $form, \
$out_ct);
185 $ctx->process(\
$content_tt, $form, \
$out_ctx);
188 $ct->process('foo.tt', $form, \
$out_c2);
191 $ct->process_simple(\
$content_tt, {%$stash_t, %$form}, \
$out_c3);
193 my $out_pt = $pt->fill_in(PACKAGE
=> 'FOO', HASH
=> $form);
195 my $out_ht = $ht->output;
196 my $out_hte = $hte->output;
197 my $out_htc = $ht_c->output;
198 my $out_htj = $ht_j->output;
200 if ($out_ct ne $out_tt) {
201 debug
$out_ct, $out_tt;
202 die "CGI::Ex::Template didn't match tt";
204 if ($out_ctx ne $out_tt) {
205 debug
$out_ctx, $out_tt;
206 die "CGI::Ex::Template::XS didn't match tt";
208 if ($out_ttx ne $out_tt) {
209 debug
$out_ttx, $out_tt;
210 die "Template::Stash::XS didn't match tt";
212 if ($out_c2 ne $out_tt) {
213 debug
$out_c2, $out_tt;
214 die "CGI::Ex::Template from file didn't match tt";
216 if ($out_c3 ne $out_tt) {
217 debug
$out_c3, $out_tt;
218 die "CGI::Ex::Template by swap didn't match tt";
220 if ($out_pt ne $out_tt) {
221 debug
$out_pt, $out_tt;
222 die "Text Template didn't match tt";
224 if ($out_ht ne $out_tt) {
225 debug
$out_ht, $out_tt;
226 die "HTML::Template didn't match tt";
228 if ($out_hte ne $out_tt) {
229 debug
$out_hte, $out_tt;
230 die "HTML::Template::Expr didn't match tt";
232 if ($out_htc ne $out_tt) {
233 debug
$out_htc, $out_tt;
234 die "HTML::Template::Expr didn't match tt";
236 if ($out_htj ne $out_tt) {
237 debug
$out_htj, $out_tt;
238 die "HTML::Template::JIT didn't match tt";
241 ###----------------------------------------------------------------###
245 my $tt = Template-
>new({
246 INCLUDE_PATH
=> \
@dirs,
247 STASH
=> Template
::Stash-
>new($stash_t),
250 $tt->process(\
$content_tt, $form, \
$out);
254 $tt->process('foo.tt', $form, \
$out);
257 my $tt = Template-
>new({
258 INCLUDE_PATH
=> \
@dirs,
259 STASH
=> Template
::Stash-
>new($stash_t),
260 COMPILE_DIR
=> $dir2,
263 $tt->process('foo.tt', $form, \
$out);
267 my $tt = Template-
>new({
268 INCLUDE_PATH
=> \
@dirs,
269 STASH
=> Template
::Stash
::XS-
>new($stash_t),
272 $tt->process(\
$content_tt, $form, \
$out);
276 $ttx->process('foo.tt', $form, \
$out);
279 my $tt = Template-
>new({
280 INCLUDE_PATH
=> \
@dirs,
281 STASH
=> Template
::Stash
::XS-
>new($stash_t),
282 COMPILE_DIR
=> $dir2,
285 $tt->process('foo.tt', $form, \
$out);
289 my $ct = CGI
::Ex
::Template-
>new({
290 INCLUDE_PATH
=> \
@dirs,
291 VARIABLES
=> $stash_t,
294 $ct->process(\
$content_tt, $form, \
$out);
298 $ct->process('foo.tt', $form, \
$out);
301 my $ct = CGI
::Ex
::Template-
>new({
302 INCLUDE_PATH
=> \
@dirs,
303 VARIABLES
=> $stash_t,
304 COMPILE_DIR
=> $dir2,
307 $ct->process('foo.tt', $form, \
$out);
311 my $ct = CGI
::Ex
::Template
::XS-
>new({
312 INCLUDE_PATH
=> \
@dirs,
313 VARIABLES
=> $stash_t,
316 $ct->process(\
$content_tt, $form, \
$out);
320 $ctx->process('foo.tt', $form, \
$out);
323 my $ct = CGI
::Ex
::Template
::XS-
>new({
324 INCLUDE_PATH
=> \
@dirs,
325 VARIABLES
=> $stash_t,
326 COMPILE_DIR
=> $dir2,
329 $ct->process('foo.tt', $form, \
$out);
332 TextTemplate
=> sub {
333 my $pt = Text
::Template-
>new(
335 SOURCE
=> $content_p,
337 my $out = $pt->fill_in(PACKAGE
=> 'FOO', HASH
=> $form);
341 my $ht = HTML
::Template-
>new(type
=> 'scalarref', source
=> \
$content_ht);
342 $ht->param($stash_ht);
344 my $out = $ht->output;
347 my $ht = HTML
::Template-
>new(type
=> 'filename', source
=> "foo.ht", path
=> \
@dirs, cache
=> 1);
348 $ht->param($stash_ht);
350 my $out = $ht->output;
353 my $ht = HTML
::Template-
>new(type
=> 'filename', source
=> "foo.ht", file_cache
=> 1, path
=> \
@dirs, file_cache_dir
=> $dir2);
354 $ht->param($stash_ht);
356 my $out = $ht->output;
360 my $ht = HTML
::Template
::Expr-
>new(type
=> 'scalarref', source
=> \
$content_ht);
361 $ht->param($stash_ht);
363 my $out = $ht->output;
366 my $ht = HTML
::Template
::Expr-
>new(type
=> 'filename', source
=> "foo.ht", path
=> \
@dirs, cache
=> 1);
367 $ht->param($stash_ht);
369 my $out = $ht->output;
373 my $ht = HTML
::Template
::JIT-
>new(filename
=> "foo.ht", path
=> \
@dirs, jit_path
=> $dir2);
374 $ht->param($stash_ht);
376 my $out = $ht->output;
381 my %mem_tests = map {($_ => $tests->{$_})} qw(TT_mem TTX_mem CET_mem HT_mem HTE_mem CTX_mem);
382 my %cpl_tests = map {($_ => $tests->{$_})} qw(TT_compile TTX_compile CET_compile HT_compile HTJ_compile CTX_compile);
383 my %str_tests = map {($_ => $tests->{$_})} qw(TT_str TTX_str CET_str HT_str HTE_str TextTemplate CTX_str);
385 print "------------------------------------------------------------------------\n";
386 print "From a string or scalarref tests\n";
387 cmpthese timethese
(-2, \
%str_tests);
389 print "------------------------------------------------------------------------\n";
390 print "Compiled and cached on the file system tests\n";
391 cmpthese timethese
(-2, \
%cpl_tests);
393 print "------------------------------------------------------------------------\n";
394 print "Cached in memory tests\n";
395 cmpthese timethese
(-2, \
%mem_tests);
397 print "------------------------------------------------------------------------\n";
398 print "All variants together\n";
399 cmpthese timethese
(-2, $tests);
401 ###----------------------------------------------------------------###
This page took 0.060088 seconds and 4 git commands to generate.