Commit | Line | Data |
387178d0 |
1 | #!/usr/bin/env perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Template::Teeny; |
7 | use Template::Simple; |
8 | use Template::Teeny::Stash; |
9 | use Template; |
10 | |
11 | my $iter = shift || -2 ; |
12 | |
13 | use Benchmark qw(:hireswallclock cmpthese); |
14 | basic: { |
15 | |
16 | my $ts = Template::Simple->new() ; |
17 | $ts->add_templates( { bench => 'hehe [% name %]' } ) ; |
18 | |
19 | my $tsc = Template::Simple->new() ; |
20 | $tsc->add_templates( { bench => 'hehe [% name %]' } ) ; |
21 | $tsc->compile( 'bench' ) ; |
22 | |
23 | my $tt = Template::Teeny->new({ include_path => ['t/tpl'] }); |
24 | my $stash = Template::Teeny::Stash->new({ name => 'bob' }); |
25 | |
26 | my $t = Template->new({ INCLUDE_PATH => 't/tpl', COMPILE_EXT => '.tc' }); |
27 | my $out; |
28 | open my $fh, '>/dev/null'; |
29 | |
30 | $tt->process('bench.tpl', $stash, $fh); |
31 | $t->process('bench.tpl', { name => 'bob' }, $fh); |
32 | |
33 | sub teeny { |
34 | $tt->process('bench.tpl', $stash, $fh); |
35 | } |
36 | sub plain { |
37 | $t->process('bench.tpl', { name => 'bob' }, $fh); |
38 | } |
39 | |
40 | sub simple { |
41 | $ts->render('bench', { name => 'bob' } ); |
42 | } |
43 | |
44 | sub ts_compiled { |
45 | $tsc->render('bench', { name => 'bob' } ); |
46 | } |
47 | |
48 | print "Very simple interpolation:\n"; |
49 | cmpthese( $iter, { teeny => \&teeny, template_toolkit => \&plain, |
50 | simple => \&simple, ts_compiled => \&ts_compiled }) ; |
51 | } |
52 | |
53 | some_looping_etc: { |
54 | |
55 | my $tmpl = <<TMPL ; |
56 | <html> |
57 | <head><title>[% title %]</title></head> |
58 | <body> |
59 | <ul> |
60 | [% SECTION post %] |
61 | <li> |
62 | <h3>[% title %]</h3> |
63 | <span>[% date %]</span> |
64 | </li> |
65 | [% END %] |
66 | </ul> |
67 | </body> |
68 | </html> |
69 | TMPL |
70 | |
71 | my $ts = Template::Simple->new() ; |
72 | $ts->add_templates( { bench2 => $tmpl } ) ; |
73 | |
74 | my $tsc = Template::Simple->new() ; |
75 | $tsc->add_templates( { bench2 => $tmpl } ) ; |
76 | $tsc->compile( 'bench2' ) ; |
77 | |
78 | my $tt = Template::Teeny->new({ include_path => ['t/tpl'] }); |
79 | my $stash = Template::Teeny::Stash->new({ title => q{Bobs Blog} }); |
80 | |
81 | my $post1 = Template::Teeny::Stash->new({ date => 'Today', title => 'hehe' }); |
82 | my $post2 = Template::Teeny::Stash->new({ date => '3 Days ago', title => 'Something new' }); |
83 | $stash->add_section('post', $post1); |
84 | $stash->add_section('post', $post2); |
85 | |
86 | my $t = Template->new({ INCLUDE_PATH => 't/tpl', COMPILE_EXT => '.tc' }); |
87 | my $out; |
88 | open my $fh, '>/dev/null'; |
89 | |
90 | my $tt_vars = { |
91 | title => 'Bobs Blog', |
92 | posts => [ |
93 | { title => 'hehe', date => 'Today' }, |
94 | { date => '3 Days ago', title => 'Something new' }, |
95 | ], |
96 | }; |
97 | teeny2(); |
98 | plain2(); |
99 | |
100 | sub teeny2 { |
101 | $tt->process('bench2-teeny.tpl', $stash, $fh); |
102 | } |
103 | sub plain2 { |
104 | $t->process('bench2-tt.tpl', $tt_vars, $fh); |
105 | } |
106 | |
107 | sub simple2 { |
108 | $ts->render('bench2', $tt_vars ); |
109 | } |
110 | |
111 | sub ts_compiled2 { |
112 | $tsc->render('bench2', $tt_vars ); |
113 | } |
114 | |
115 | print "\nLoop and interpolation:\n"; |
116 | cmpthese( $iter, { teeny => \&teeny2, template_toolkit => \&plain2, |
117 | simple => \&simple2, ts_compiled => \&ts_compiled2 }) ; |
118 | |
119 | } |
120 | |
121 | |
122 | |
123 | |