]> Dogcows Code - chaz/p5-CGI-Ex/blob - samples/benchmark/bench_various_templaters.pl
1dc6b6958efcbb561ebdaec809ebce8c257ad926
[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 Text::Template;
19 use CGI::Ex::Dump qw(debug);
20 use CGI::Ex::Template;
21 use POSIX qw(tmpnam);
22 use File::Path qw(mkpath rmtree);
23
24 my $dir = tmpnam;
25 mkpath($dir);
26 END {rmtree $dir};
27 my @dirs = ($dir);
28
29 my $form = {
30 foo => 'bar',
31 pass_in_something => 'what ever you want',
32 };
33
34 ###----------------------------------------------------------------###
35
36 my $stash_w = {
37 shell => {
38 header => "This is a header",
39 footer => "This is a footer",
40 start => "<html>",
41 end => "<end>",
42 foo => $form->{'foo'},
43 },
44 a => {
45 stuff => [qw(one two three four)],
46 },
47 };
48
49 my $stash_t = {
50 shell_header => "This is a header",
51 shell_footer => "This is a footer",
52 shell_start => "<html>",
53 shell_end => "<end>",
54 a_stuff => [qw(one two three four)],
55 };
56
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)];
63
64
65 ###----------------------------------------------------------------###
66
67 my $content_w = q{[shell.header]
68 [shell.start]
69
70 [if shell.foo q{
71 This is some text.
72 }]
73
74 [loop i a.stuff.length q{[a.stuff]}]
75 [form.pass_in_something]
76
77 [shell.end]
78 [shell.footer]
79 };
80
81 my $content_t = q{[% shell_header %]
82 [% shell_start %]
83
84 [% IF foo %]
85 This is some text.
86 [% END %]
87
88 [% FOREACH i IN a_stuff %][% i %][% END %]
89 [% pass_in_something %]
90
91 [% shell_end %]
92 [% shell_footer %]
93 };
94
95 my $content_h = q{<TMPL_VAR NAME=shell_header>
96 [% shell_start %]
97
98 [% IF foo %]
99 This is some text.
100 [% END %]
101
102 [% FOREACH i IN a_stuff %][% i %][% END %]
103 [% pass_in_something %]
104
105 [% shell_end %]
106 [% shell_footer %]
107 };
108
109 if (open (my $fh, ">$dir/foo.tt")) {
110 print $fh $content_t;
111 close $fh;
112 }
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 #my $wrap = WrapEx->new({
132 # dirs => \@dirs,
133 # W => $stash_w,
134 # form => [$form],
135 #});
136
137 my $tt = Template->new({
138 INCLUDE_PATH => \@dirs,
139 STASH => Template::Stash->new($stash_t),
140 });
141
142 my $ct = CGI::Ex::Template->new({
143 INCLUDE_PATH => \@dirs,
144 VARIABLES => $stash_t,
145 });
146
147 my $pt = Text::Template->new(TYPE => 'STRING', SOURCE => $content_p, HASH => $form);
148
149 ###----------------------------------------------------------------###
150 ### make sure everything is ok
151
152 #my $out_wr = $content_w;
153 #$wrap->wrap(\$out_wr);
154
155 my $out_tt = "";
156 $tt->process(\$content_t, $form, \$out_tt);
157
158 my $out_ct = "";
159 $ct->process(\$content_t, $form, \$out_ct);
160
161 my $out_c2 = "";
162 $ct->process('foo.tt', $form, \$out_c2);
163
164 my $out_c3 = '';
165 $ct->process_simple(\$content_t, {%$stash_t, %$form}, \$out_c3);
166
167 my $out_pt = $pt->fill_in(PACKAGE => 'FOO', HASH => $form);
168
169 if ($out_wr ne $out_tt) {
170 debug $out_wr, $out_tt;
171 die "Wrap didn't match tt";
172 }
173 if ($out_ct ne $out_tt) {
174 debug $out_ct, $out_tt;
175 die "CGI::Ex::Template didn't match tt";
176 }
177 if ($out_c2 ne $out_tt) {
178 debug $out_c2, $out_tt;
179 die "CGI::Ex::Template from file didn't match tt";
180 }
181 if ($out_c3 ne $out_tt) {
182 debug $out_c3, $out_tt;
183 die "CGI::Ex::Template by swap didn't match tt";
184 }
185 if ($out_pt ne $out_tt) {
186 debug $out_pt, $out_tt;
187 die "Text Template didn't match tt";
188 }
189
190 ###----------------------------------------------------------------###
191
192 cmpthese timethese (-2, {
193 # wrap => sub {
194 # my $out = $content_w;
195 # $wrap->wrap(\$out);
196 # },
197 TemplateToolkit => sub {
198 my $out = "";
199 $tt->process(\$content_t, $form, \$out);
200 },
201 CET => sub {
202 my $out = "";
203 $ct->process(\$content_t, $form, \$out);
204 },
205 CET_mem => sub {
206 my $out = "";
207 $ct->process('foo.tt', $form, \$out);
208 },
209 CET_process_s => sub {
210 my $out = '';
211 $ct->process_simple(\$content_t, {%$stash_t, %$form}, \$out);
212 },
213 CET_cache => sub {
214 my $ct = CGI::Ex::Template->new({
215 INCLUDE_PATH => \@dirs,
216 STASH => Template::Stash->new($stash_t),
217 CACHE_DIR => $dir,
218 });
219 my $out = '';
220 $ct->process('foo.tt', {%$stash_t, %$form}, \$out);
221 },
222 TextTemplate => sub {
223 my $out = $pt->fill_in(PACKAGE => 'FOO', HASH => $form);
224 },
225 TextTemplate2 => sub {
226 my $out = $pt->fill_in(PACKAGE => 'FOO', HASH => {%$stash_t, %$form});
227 },
228 });
229
230 ###----------------------------------------------------------------###
This page took 0.046862 seconds and 3 git commands to generate.