root/docs/cho45/20080515-yapc.asia-2008-tokyo/bench.pl

Revision 11644, 1.5 kB (checked in by cho45, 8 months ago)

YAPC LT

  • Property svn:executable set to *
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Benchmark qw/:all/;
7use Text::MicroMason;
8use Template;
9
10sub unindent ($) {
11        my ($str) = @_;
12        my ($ws)  = $str =~ /^(\s+)/;
13        $ws  =~ s/\n//g;
14        $str =~ s/^$ws//gm;
15        $str;
16}
17
18my $tt  = Template->new();
19my $tm  = Text::MicroMason->new(qw/-AllowGlobals/);
20my $tms = Text::MicroMason->new(qw/-AllowGlobals -SafeServerPages/);
21
22cmpthese timethese(1000, {
23        "TT" => sub {
24                my $output;
25                $tt->process(\"[% foo %]", {
26                        foo => "hogehoge"
27                }, \$output);
28        },
29
30        "Text::MicroMason" => sub {
31                $tm->set_globals( '$foo' => 'hogehoge' );
32                $tm->execute(text => '<% $foo %>');
33        },
34
35        "Text::MicroMason::SafeServerPages" => sub {
36                $tms->set_globals( '$foo' => 'hogehoge' );
37                $tms->execute(text => '<%= $foo %>');
38        }
39});
40
41print "\n\n";
42
43cmpthese timethese(1000, {
44        "TT" => sub {
45                my $output;
46                $tt->process(\unindent q{
47                        <ul>
48                        [% FOREACH i = items %]
49                                <li>[% i %]</li>
50                        [% END %]
51                        </ul>
52
53                        [% foo %]
54                }, {
55                        items => [qw/1 2 3 4 5 6 7 8 9 0/],
56                        foo => "foo",
57                }, \$output);
58        },
59
60        "Text::MicroMason" => sub {
61                $tm->set_globals(
62                        '$items' => [qw/1 2 3 4 5 6 7 8 9 0/],
63                        '$foo' => "foo",
64                );
65                $tm->execute(text => unindent q{
66                        <ul>
67                        % for my $i ($items) {
68                                <li><% $i %></li>
69                        % }
70                        </ul>
71
72                        <% $foo %>
73                });
74        },
75
76        "Text::MicroMason::SafeServerPages" => sub {
77                $tms->set_globals(
78                        '$items' => [qw/1 2 3 4 5 6 7 8 9 0/],
79                        '$foo' => "foo",
80                );
81                $tms->execute(text => unindent q{
82                        <ul>
83                        <% for my $i ($items) { %>
84                                <li><%= $i %></li>
85                        <% } %>
86                        </ul>
87
88                        <%= $foo %>
89                });
90        },
91});
Note: See TracBrowser for help on using the browser.