File: x-poor-env.pl

package info (click to toggle)
libtext-xslate-perl 3.5.9-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,108 kB
  • sloc: perl: 19,756; ansic: 214; pascal: 182; makefile: 9; cs: 8
file content (159 lines) | stat: -rw-r--r-- 3,755 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!perl
# For poor environment, e.g. CGI applications without XS
BEGIN { $ENV{PERL_ONLY}= 1 }
use strict;
use warnings;

use Getopt::Long;

use Test::More;
use Benchmark qw(:all);
use FindBin qw($Bin);

use Template;
use HTML::Template;
use Text::MicroTemplate::Extended;

use Config; printf "Perl/%vd %s\n", $^V, $Config{archname};

GetOptions(
    'booster'    => \my $pp_booster,
    'opcode'     => \my $pp_opcode,

    'size=i'     => \my $n,
    'template=s' => \my $tmpl,
    'help'       => \my $help,
);

die <<'HELP' if $help;
perl -Mblib benchmark/x-poor-env.pl [--size N] [--template NAME] [--opcode|booster]

This is a general benchmark utility for poor environment,
assuming CGI applications using only pure Perl modules.
See also benchmark/x-rich-env.pl.
HELP

$tmpl = 'include' if not defined $tmpl;
$n    = 100       if not defined $n;

if(defined $pp_booster) {
    $ENV{XSLATE} = 'pp=booster';
}
elsif(defined $pp_opcode) {
    $ENV{XSLATE} = 'pp=opcode';
}

$ENV{MOUSE_PUREPERL} = 1;
$Template::Config::STASH = 'Template::Stash'; # Instead of Stash::XS

require Text::Xslate::PP;

foreach my $mod(qw(
    Text::Xslate
    Template
    HTML::Template
    Text::MicroTemplate
    Text::MicroTemplate::Extended
)){
    print $mod, '/', $mod->VERSION, "\n" if $mod->VERSION;
}

my $path = "$Bin/template";

my $vars = {
    data => [ ({
            title    => "<FOO>",
            author   => "BAR",
            abstract => "BAZ",
        }) x $n
   ],
};

TEST: {
    my $tx = Text::Xslate->new(
        path       => [$path],
        cache_dir  =>  '.xslate_cache',
        cache      => 2,
    );
    my $mt = Text::MicroTemplate::Extended->new(
        include_path => [$path],
        use_cache    => 2,
    );
    my $tt = Template->new(
        INCLUDE_PATH => [$path],
        COMPILE_EXT  => '.out',
    );

    my $ht = HTML::Template->new(
        path              => [$path],
        filename          => "$tmpl.ht",
        case_sensitive    => 1,
        die_on_bad_params => 0,
        double_file_cache => 1,
        file_cache_dir    => '.xslate_cache',
    );

    my $expected = $tx->render("$tmpl.tx", $vars);
    $expected =~ s/\n+/\n/g;


    plan tests => 3;

    $tt->process("$tmpl.tt", $vars, \my $out) or die $tt->error;
    $out =~ s/\n+/\n/g;
    is $out, $expected, 'TT: Template-Toolkit';

    $out = $mt->render_file($tmpl, $vars);
    $out =~ s/\n+/\n/g;
    is $out, $expected, 'MT: Text::MicroTemplate';

    $ht->param($vars);
    $out = $ht->output();
    $out =~ s/\n+/\n/g;
    is $out, $expected, 'HT: HTML::Template';
}

print "Benchmarks with '$tmpl' (datasize=$n)\n";
cmpthese -1 => {
    Xslate => sub {
        my $tx = Text::Xslate->new(
            path       => [$path],
            cache_dir  =>  '.xslate_cache',
            cache      => 2,
        );
        my $body = $tx->render("$tmpl.tx", $vars);
        return;
    },
    MT => sub {
        my $mt = Text::MicroTemplate::Extended->new(
            include_path => [$path],
            cache        => 2,
        );
        my $body = $mt->render_file($tmpl, $vars);
        return;
    },
    TT => sub {
        my $tt = Template->new(
            INCLUDE_PATH => [$path],
            COMPILE_EXT  => '.out',
        );
        my $body;
        $tt->process("$tmpl.tt", $vars, \$body) or die $tt->error;
        return;
    },
    HT => sub {
        my $ht = HTML::Template->new(
            path              => [$path],
            filename          => "$tmpl.ht",
            case_sensitive    => 1,
            die_on_bad_params => 0,
            double_file_cache => 1,
            file_cache_dir    => '.xslate_cache',
        );

        $ht->param($vars);
        my $body = $ht->output();
        return;
    },
};