File: include.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 (90 lines) | stat: -rw-r--r-- 2,162 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
#!perl -w
use strict;

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

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

use Config; printf "Perl/%vd %s\n", $^V, $Config{archname};
foreach my $mod(qw(Text::Xslate Text::MicroTemplate HTML::Template::Pro Template)){
    print $mod, '/', $mod->VERSION, "\n";
}

my $n = shift(@ARGV) || 10;

my @path = ("$Bin/template");
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 $ht = HTML::Template->new(
    path           => \@path,
    filename       => "include.ht",
    case_sensitive => 1,
);
my $tt = Template->new(
    INCLUDE_PATH => \@path,
    COMPILE_EXT  => '.out',
);

my %vars = (
     data => [(
        { title => 'Islands in the stream' },
        { title => 'Beautiful code' },
        { title => 'Introduction to Psychology' },
        { title => 'Programming Perl' },
        { title => 'Compilers: Principles, Techniques, and Tools' },
     ) x $n],
);

{
    my $expected = $tx->render('include.tx', \%vars);
    $expected =~ s/\n+/\n/g;

    plan tests => 3;
    my $out = $mt->render('include', \%vars);
    $out =~ s/\n+/\n/g;
    is $out, $expected, 'MT - Text::MicroTemplate::Extended';

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

    $out = '';
    $tt->process('include.tt', \%vars, \$out) or die $tt->error;
    is $out, $expected, 'TT - Template-Toolkit';
}

print "Benchmarks for include commands\n";
# suppose PSGI response body
cmpthese -1 => {
    Xslate => sub {
        my $body = [$tx->render('include.tx', \%vars)];
        return;
    },
    MT => sub {
        my $body = [$mt->render('include', \%vars)];
        return;
    },
    HT => sub {
        $ht->param(\%vars);
        my $body = [$ht->output()];
        return;
    },
    TT => sub {
        my $body = [''];
        $tt->process('include.tt', \%vars, \$body->[0]) or die $tt->error;
        return;
    },
};