]> Dogcows Code - chaz/p5-CGI-Ex/blob - samples/benchmark/bench_various_templaters.pl
CGI::Ex 2.10
[chaz/p5-CGI-Ex] / samples / benchmark / bench_various_templaters.pl
1 #!/usr/bin/perl -w
2
3 =head1 NAME
4
5 bench_various_templaters.pl - test the relative performance of several different types of template engines.
6
7 =cut
8
9 use strict;
10 use Benchmark qw(timethese cmpthese);
11
12 my $file = $0;
13 $file =~ s|[^/]+$|WrapEx.pm|;
14 #require $file;
15
16 use Template;
17 use Template::Stash;
18 use Template::Stash::XS;
19 use Text::Template;
20 use HTML::Template;
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;
26 use POSIX qw(tmpnam);
27 use File::Path qw(mkpath rmtree);
28
29 my $dir = tmpnam;
30 my $dir2 = "$dir.cache";
31 mkpath($dir);
32 mkpath($dir2);
33 END {rmtree $dir; rmtree $dir2};
34 my @dirs = ($dir);
35
36 my $form = {
37 foo => 'bar',
38 pass_in_something => 'what ever you want',
39 };
40
41 ###----------------------------------------------------------------###
42
43 my $stash_t = {
44 shell_header => "This is a header",
45 shell_footer => "This is a footer",
46 shell_start => "<html>",
47 shell_end => "<end>",
48 a_stuff => [qw(one two three four)],
49 };
50
51 my $stash_ht = {
52 shell_header => "This is a header",
53 shell_footer => "This is a footer",
54 shell_start => "<html>",
55 shell_end => "<end>",
56 a_stuff => [map {{name => $_}} qw(one two three four)],
57 };
58
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)];
65
66
67 ###----------------------------------------------------------------###
68 ### TT style template
69
70 my $content_tt = q{[% shell_header %]
71 [% shell_start %]
72
73 [% IF foo %]
74 This is some text.
75 [% END %]
76
77 [% FOREACH i IN a_stuff %][% i %][% END %]
78 [% pass_in_something %]
79
80 [% shell_end %]
81 [% shell_footer %]
82 };
83
84 if (open (my $fh, ">$dir/foo.tt")) {
85 print $fh $content_tt;
86 close $fh;
87 }
88
89 ###----------------------------------------------------------------###
90 ### HTML::Template style
91
92 my $content_ht = q{<TMPL_VAR NAME=shell_header>
93 <TMPL_VAR NAME=shell_start>
94
95 <TMPL_IF NAME=foo>
96 This is some text.
97 </TMPL_IF>
98
99 <TMPL_LOOP NAME=a_stuff><TMPL_VAR NAME=name></TMPL_LOOP>
100 <TMPL_VAR NAME=pass_in_something>
101
102 <TMPL_VAR NAME=shell_end>
103 <TMPL_VAR NAME=shell_footer>
104 };
105
106 if (open (my $fh, ">$dir/foo.ht")) {
107 print $fh $content_ht;
108 close $fh;
109 }
110
111 ###----------------------------------------------------------------###
112 ### Text::Template style template
113
114 my $content_p = q{{$shell_header}
115 {$shell_start}
116
117 { if ($foo) {
118 $OUT .= "
119 This is some text.
120 ";
121 }
122 }
123
124 { $OUT .= $_ foreach @$a_stuff; }
125 {$pass_in_something}
126
127 {$shell_end}
128 {$shell_footer}
129 };
130
131 ###----------------------------------------------------------------###
132 ### setup the objects
133
134 my $tt = Template->new({
135 INCLUDE_PATH => \@dirs,
136 STASH => Template::Stash->new($stash_t),
137 });
138
139 my $ttx = Template->new({
140 INCLUDE_PATH => \@dirs,
141 STASH => Template::Stash::XS->new($stash_t),
142 });
143
144 my $ct = CGI::Ex::Template->new({
145 INCLUDE_PATH => \@dirs,
146 VARIABLES => $stash_t,
147 });
148
149 my $ctx = CGI::Ex::Template::XS->new({
150 INCLUDE_PATH => \@dirs,
151 VARIABLES => $stash_t,
152 });
153
154 my $pt = Text::Template->new(TYPE => 'STRING', SOURCE => $content_p, HASH => $form);
155
156 my $ht = HTML::Template->new(type => 'scalarref', source => \$content_ht);
157 $ht->param($stash_ht);
158 $ht->param($form);
159
160 my $hte = HTML::Template::Expr->new(type => 'scalarref', source => \$content_ht);
161 $hte->param($stash_ht);
162 $hte->param($form);
163
164 my $ht_c = HTML::Template->new(type => 'filename', source => "foo.ht", cache => 1, path => \@dirs);
165 $ht_c->param($stash_ht);
166 $ht_c->param($form);
167
168 my $ht_j = HTML::Template::JIT->new(filename => "foo.ht", path => \@dirs, jit_path => $dir2);
169 $ht_j->param($stash_ht);
170 $ht_j->param($form);
171
172 ###----------------------------------------------------------------###
173 ### make sure everything is ok by trying it once
174
175 my $out_tt = "";
176 $tt->process(\$content_tt, $form, \$out_tt);
177
178 my $out_ttx = "";
179 $ttx->process(\$content_tt, $form, \$out_ttx);
180
181 my $out_ct = "";
182 $ct->process(\$content_tt, $form, \$out_ct);
183
184 my $out_ctx = "";
185 $ctx->process(\$content_tt, $form, \$out_ctx);
186
187 my $out_c2 = "";
188 $ct->process('foo.tt', $form, \$out_c2);
189
190 my $out_c3 = '';
191 $ct->process_simple(\$content_tt, {%$stash_t, %$form}, \$out_c3);
192
193 my $out_pt = $pt->fill_in(PACKAGE => 'FOO', HASH => $form);
194
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;
199
200 if ($out_ct ne $out_tt) {
201 debug $out_ct, $out_tt;
202 die "CGI::Ex::Template didn't match tt";
203 }
204 if ($out_ctx ne $out_tt) {
205 debug $out_ctx, $out_tt;
206 die "CGI::Ex::Template::XS didn't match tt";
207 }
208 if ($out_ttx ne $out_tt) {
209 debug $out_ttx, $out_tt;
210 die "Template::Stash::XS didn't match tt";
211 }
212 if ($out_c2 ne $out_tt) {
213 debug $out_c2, $out_tt;
214 die "CGI::Ex::Template from file didn't match tt";
215 }
216 if ($out_c3 ne $out_tt) {
217 debug $out_c3, $out_tt;
218 die "CGI::Ex::Template by swap didn't match tt";
219 }
220 if ($out_pt ne $out_tt) {
221 debug $out_pt, $out_tt;
222 die "Text Template didn't match tt";
223 }
224 if ($out_ht ne $out_tt) {
225 debug $out_ht, $out_tt;
226 die "HTML::Template didn't match tt";
227 }
228 if ($out_hte ne $out_tt) {
229 debug $out_hte, $out_tt;
230 die "HTML::Template::Expr didn't match tt";
231 }
232 if ($out_htc ne $out_tt) {
233 debug $out_htc, $out_tt;
234 die "HTML::Template::Expr didn't match tt";
235 }
236 if ($out_htj ne $out_tt) {
237 debug $out_htj, $out_tt;
238 die "HTML::Template::JIT didn't match tt";
239 }
240
241 ###----------------------------------------------------------------###
242
243 my $tests = {
244 TT_str => sub {
245 my $tt = Template->new({
246 INCLUDE_PATH => \@dirs,
247 STASH => Template::Stash->new($stash_t),
248 });
249 my $out = "";
250 $tt->process(\$content_tt, $form, \$out);
251 },
252 TT_mem => sub {
253 my $out = "";
254 $tt->process('foo.tt', $form, \$out);
255 },
256 TT_compile => sub {
257 my $tt = Template->new({
258 INCLUDE_PATH => \@dirs,
259 STASH => Template::Stash->new($stash_t),
260 COMPILE_DIR => $dir2,
261 });
262 my $out = "";
263 $tt->process('foo.tt', $form, \$out);
264 },
265
266 TTX_str => sub {
267 my $tt = Template->new({
268 INCLUDE_PATH => \@dirs,
269 STASH => Template::Stash::XS->new($stash_t),
270 });
271 my $out = "";
272 $tt->process(\$content_tt, $form, \$out);
273 },
274 TTX_mem => sub {
275 my $out = "";
276 $ttx->process('foo.tt', $form, \$out);
277 },
278 TTX_compile => sub {
279 my $tt = Template->new({
280 INCLUDE_PATH => \@dirs,
281 STASH => Template::Stash::XS->new($stash_t),
282 COMPILE_DIR => $dir2,
283 });
284 my $out = "";
285 $tt->process('foo.tt', $form, \$out);
286 },
287
288 CET_str => sub {
289 my $ct = CGI::Ex::Template->new({
290 INCLUDE_PATH => \@dirs,
291 VARIABLES => $stash_t,
292 });
293 my $out = "";
294 $ct->process(\$content_tt, $form, \$out);
295 },
296 CET_mem => sub {
297 my $out = "";
298 $ct->process('foo.tt', $form, \$out);
299 },
300 CET_compile => sub {
301 my $ct = CGI::Ex::Template->new({
302 INCLUDE_PATH => \@dirs,
303 VARIABLES => $stash_t,
304 COMPILE_DIR => $dir2,
305 });
306 my $out = '';
307 $ct->process('foo.tt', $form, \$out);
308 },
309
310 CTX_str => sub {
311 my $ct = CGI::Ex::Template::XS->new({
312 INCLUDE_PATH => \@dirs,
313 VARIABLES => $stash_t,
314 });
315 my $out = "";
316 $ct->process(\$content_tt, $form, \$out);
317 },
318 CTX_mem => sub {
319 my $out = "";
320 $ctx->process('foo.tt', $form, \$out);
321 },
322 CTX_compile => sub {
323 my $ct = CGI::Ex::Template::XS->new({
324 INCLUDE_PATH => \@dirs,
325 VARIABLES => $stash_t,
326 COMPILE_DIR => $dir2,
327 });
328 my $out = '';
329 $ct->process('foo.tt', $form, \$out);
330 },
331
332 TextTemplate => sub {
333 my $pt = Text::Template->new(
334 TYPE => 'STRING',
335 SOURCE => $content_p,
336 HASH => $form);
337 my $out = $pt->fill_in(PACKAGE => 'FOO', HASH => $form);
338 },
339
340 HT_str => sub {
341 my $ht = HTML::Template->new(type => 'scalarref', source => \$content_ht);
342 $ht->param($stash_ht);
343 $ht->param($form);
344 my $out = $ht->output;
345 },
346 HT_mem => sub {
347 my $ht = HTML::Template->new(type => 'filename', source => "foo.ht", path => \@dirs, cache => 1);
348 $ht->param($stash_ht);
349 $ht->param($form);
350 my $out = $ht->output;
351 },
352 HT_compile => sub {
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);
355 $ht->param($form);
356 my $out = $ht->output;
357 },
358
359 HTE_str => sub {
360 my $ht = HTML::Template::Expr->new(type => 'scalarref', source => \$content_ht);
361 $ht->param($stash_ht);
362 $ht->param($form);
363 my $out = $ht->output;
364 },
365 HTE_mem => sub {
366 my $ht = HTML::Template::Expr->new(type => 'filename', source => "foo.ht", path => \@dirs, cache => 1);
367 $ht->param($stash_ht);
368 $ht->param($form);
369 my $out = $ht->output;
370 },
371
372 HTJ_compile => sub {
373 my $ht = HTML::Template::JIT->new(filename => "foo.ht", path => \@dirs, jit_path => $dir2);
374 $ht->param($stash_ht);
375 $ht->param($form);
376 my $out = $ht->output;
377 },
378 };
379
380
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);
384
385 print "------------------------------------------------------------------------\n";
386 print "From a string or scalarref tests\n";
387 cmpthese timethese (-2, \%str_tests);
388
389 print "------------------------------------------------------------------------\n";
390 print "Compiled and cached on the file system tests\n";
391 cmpthese timethese (-2, \%cpl_tests);
392
393 print "------------------------------------------------------------------------\n";
394 print "Cached in memory tests\n";
395 cmpthese timethese (-2, \%mem_tests);
396
397 print "------------------------------------------------------------------------\n";
398 print "All variants together\n";
399 cmpthese timethese (-2, $tests);
400
401 ###----------------------------------------------------------------###
This page took 0.06205 seconds and 4 git commands to generate.