]>
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
|;
19 use CGI
::Ex
::Dump
qw(debug);
20 use CGI
::Ex
::Template
;
22 use File
::Path
qw(mkpath rmtree);
31 pass_in_something
=> 'what ever you want',
34 ###----------------------------------------------------------------###
38 header
=> "This is a header",
39 footer
=> "This is a footer",
42 foo
=> $form->{'foo'},
45 stuff
=> [qw(one two three four)],
50 shell_header
=> "This is a header",
51 shell_footer
=> "This is a footer",
52 shell_start
=> "<html>",
54 a_stuff
=> [qw(one two three four)],
57 $FOO::shell_header
= $FOO::shell_footer
= $FOO::shell_start
= $FOO::shell_end
= $FOO::a_stuff
;
58 $FOO::shell_header
= "This is a header";
59 $FOO::shell_footer
= "This is a footer";
60 $FOO::shell_start
= "<html>";
61 $FOO::shell_end
= "<end>";
62 $FOO::a_stuff
= [qw(one two three four)];
65 ###----------------------------------------------------------------###
67 my $content_w = q{[shell.header]
74 [loop i a
.stuff
.length q{[a.stuff]}]
75 [form
.pass_in_something
]
81 my $content_t = q{[% shell_header %]
88 [% FOREACH i IN a_stuff %][% i %][% END %]
89 [% pass_in_something %]
95 my $content_h = q{<TMPL_VAR NAME=shell_header>
102 [% FOREACH i IN a_stuff %][% i %][% END %]
103 [% pass_in_something %]
109 if (open (my $fh, ">$dir/foo.tt")) {
110 print $fh $content_t;
114 my $content_p = q{{$shell_header}
124 { $OUT .= $_ foreach @$a_stuff; }
131 #my $wrap = WrapEx->new({
137 my $tt = Template-
>new({
138 INCLUDE_PATH
=> \
@dirs,
139 STASH
=> Template
::Stash-
>new($stash_t),
142 my $ct = CGI
::Ex
::Template-
>new({
143 INCLUDE_PATH
=> \
@dirs,
144 VARIABLES
=> $stash_t,
147 my $pt = Text
::Template-
>new(TYPE
=> 'STRING', SOURCE
=> $content_p, HASH
=> $form);
149 ###----------------------------------------------------------------###
150 ### make sure everything is ok
152 #my $out_wr = $content_w;
153 #$wrap->wrap(\$out_wr);
156 $tt->process(\
$content_t, $form, \
$out_tt);
159 $ct->process(\
$content_t, $form, \
$out_ct);
162 $ct->process('foo.tt', $form, \
$out_c2);
165 $ct->process_simple(\
$content_t, {%$stash_t, %$form}, \
$out_c3);
167 my $out_pt = $pt->fill_in(PACKAGE
=> 'FOO', HASH
=> $form);
169 if ($out_wr ne $out_tt) {
170 debug
$out_wr, $out_tt;
171 die "Wrap didn't match tt";
173 if ($out_ct ne $out_tt) {
174 debug
$out_ct, $out_tt;
175 die "CGI::Ex::Template didn't match tt";
177 if ($out_c2 ne $out_tt) {
178 debug
$out_c2, $out_tt;
179 die "CGI::Ex::Template from file didn't match tt";
181 if ($out_c3 ne $out_tt) {
182 debug
$out_c3, $out_tt;
183 die "CGI::Ex::Template by swap didn't match tt";
185 if ($out_pt ne $out_tt) {
186 debug
$out_pt, $out_tt;
187 die "Text Template didn't match tt";
190 ###----------------------------------------------------------------###
192 cmpthese timethese
(-2, {
194 # my $out = $content_w;
195 # $wrap->wrap(\$out);
197 TemplateToolkit
=> sub {
199 $tt->process(\
$content_t, $form, \
$out);
203 $ct->process(\
$content_t, $form, \
$out);
207 $ct->process('foo.tt', $form, \
$out);
209 CET_process_s
=> sub {
211 $ct->process_simple(\
$content_t, {%$stash_t, %$form}, \
$out);
214 my $ct = CGI
::Ex
::Template-
>new({
215 INCLUDE_PATH
=> \
@dirs,
216 STASH
=> Template
::Stash-
>new($stash_t),
220 $ct->process('foo.tt', {%$stash_t, %$form}, \
$out);
222 TextTemplate
=> sub {
223 my $out = $pt->fill_in(PACKAGE
=> 'FOO', HASH
=> $form);
225 TextTemplate2
=> sub {
226 my $out = $pt->fill_in(PACKAGE
=> 'FOO', HASH
=> {%$stash_t, %$form});
230 ###----------------------------------------------------------------###
This page took 0.045913 seconds and 4 git commands to generate.