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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
|
#!/usr/bin/perl -w
#
# A script to test and demonstrate the XS version of Template::Stash.
#
# Sadly, it looks like getrusage() may not return reasonable RSS values
# on Linux and many other operating systems. It works great on FreeBSD
# though.
#
# $Id$
#
# paths to Template version we want to test
use lib qw( ./lib
../lib
./blib/arch/auto/Template/Stash/XS
../blib/arch/auto/Template/Stash/XS'
);
use strict;
use Template;
use Template::Stash;
use Template::Stash::XS;
use BSD::Resource;
use CGI;
# test package
package Foo;
sub new { return bless {}, $_[0]; }
sub new_av { return bless [ 1,2,3 ], $_[0]; }
sub new_sv { return bless "hello", $_[0]; }
sub bar { return 1001; }
sub baz { return "This is baz method from Foo."; }
sub more { return "I got " . $_[1] . " for more"; }
sub newnew { return new Foo; }
sub err { return ( undef, "This is the ''error message''\n" ); }
# main
package main;
# test data
my $params = {
nodef => undef,
zero => 0,
one => 1,
a_0 => [],
a_1 => [1],
a_2 => [2,1],
bar => { baz => { boz => { bean => 20 } } },
i => 0,
string => 'The quick brown fox jumped over the lazy dog',
spaced => "This is a test string with s p a c e s",
hash => { a => 'b', c => 'd' },
metavars => [ qw( foo bar baz qux wiz waz woz ) ],
people => [ { id => 'tom', name => 'Tomas' },
{ id => 'dick', name => 'Richard' },
{ id => 'harry', name => 'Harold' },
{ id => 'wes', name => 'Wesley' },
{ id => 'andy', name => 'Andrew' },
{ id => 'jen', name => 'Jennifer' },
{ id => 'larry', name => 'Larry' } ],
primes => [ 6899, 13, 11, 69931, 17, 19, 682547, 2, 3, 5, 7 ],
phones => { 3141 => 'Alpha', 5131 => 'Beta', 4131 => 'Gamma' },
groceries => { qw( a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10),
'Flour' => 3, 'Milk' => 1, 'Peanut Butter' => 21,
'Eggs' => 5, 'Celery' => 15, 'Apples' => 12 },
stuff => [ { a => 'apple', b => 'banana', c => 'carrot' },
{ a => 'apache', b => 'bsd', c => 'commodore' },
{ a => 'asada', b => 'beef', c => 'carne' } ],
method => new Foo,
quux => [ new Foo, new_av Foo, new_av Foo ],
cgi => CGI->new('mode=submit&debug=1'),
ref => { a => sub { return "a sub [@_]" },
j => { k => 3, l => 5, m => { n => sub { "nsub [@_]" } } },
z => sub { return "z called ".&{shift()}(10, 20, 30); } },
};
$params->{r}->{e}->{c}->{u}->{r}->{s}->{e} = $params;
$params->{recurse} = $params;
print "XS Module: " , Template::Stash::XS::cvsid() , "\n";
# setup template object, etc.
my $TMPDIR = $ENV{'TMPDIR'} || '.';
my $o;
my $fast_tt = new Template ({
STASH => new Template::Stash::XS,
ABSOLUTE => 1,
RELATIVE => 1,
COMPILE_DIR => $TMPDIR,
COMPILE_EXT => '.ttc2',
CACHE_SIZE => 64,
OUTPUT => \$o });
my $slow_tt = new Template ({
STASH => new Template::Stash,
ABSOLUTE => 1,
RELATIVE => 1,
COMPILE_DIR => $TMPDIR,
COMPILE_EXT => '.ttc2',
CACHE_SIZE => 64,
OUTPUT => \$o });
$Template::Filters::FILTERS->{ baz } = []; # avoid silly warning
$Template::Filters::FILTERS->{ baz } = [
sub {
my $context = shift;
my $word = shift || 'baz';
return sub {
my $text = shift;
$text =~ s/^/$word: /gm;
return $text;
};
}, 1 ];
my $template;
my $expected;
if (@ARGV) {
$template = shift @ARGV;
} else {
$template = "$TMPDIR/testing.tmpl";
unlink $template;
open FH, ">$template";
while(<DATA>) { last if /^__END__/; print FH; } close FH;
}
# verify that we get expected results
{
$slow_tt->process($template, $params) or die "SLOW TT: " . $slow_tt->error;
print " Template: ", $template, "\n";
print " Length: ", length($o)," bytes\n";
$expected = $o;
$o = '';
$fast_tt->process($template, $params) or die "FAST TT: " . $fast_tt->error;
if($expected ne $o) {
print " GOT: $o\n";
print "=" x 60, "\n";
print "WANTED: $expected\n";
die "unexpected output from fast_tt->process\n";
} else {
print " Status: XS output OK\n\n";
}
}
# here comes the big test
for my $loops (10, 100, 1000) {
print STDERR "=" x 70, "\n\n";
print STDERR "Evaluating template with original stash $loops times...";
my $slow_stat = get_usage();
# Do the slow test
for (1..$loops) {
$o = '';
$slow_tt->process($template, $params) or die $Template::ERROR;
die "''$o'' ne ''$expected''" unless $o eq $expected;
}
my $slow_result = get_usage($slow_stat);
print STDERR "done.\n";
printf(STDERR
"Usr: %.2fs, Sys: %.2fs, Total: %.2fs, RSS: %dKB (%dKB change)\n",
@$slow_result);
print STDERR "\nEvaluating template with new XS stash $loops times...";
my $fast_stat = get_usage();
# Do the fast test
for (1..$loops) {
$o = '';
$fast_tt->process($template, $params) or die $Template::ERROR;
die "''$o'' ne ''$expected''" unless $o eq $expected;
}
my $fast_result = get_usage($fast_stat);
print STDERR "done.\n";
printf(STDERR
"Usr: %.2fs, Sys: %.2fs, Total: %.2fs, RSS: %dKB (%dKB change)\n",
@$fast_result);
printf(STDERR
"\n\tImprovement: %.2fX\n\n", $slow_result->[2] / $fast_result->[2]);
}
# If it's been enabled...
# print Template::Stash::XS::performance(1);
unlink "$TMPDIR/testing.tmpl";
exit(0);
# returns arrayref with user, system and total time
# optionally subtracts given arrayref.
sub get_usage {
my $a_ref = shift;
my ($usertime, $systemtime, $maxrss) = getrusage(RUSAGE_SELF);
my $maxrss_delta = $maxrss;
if (defined($a_ref)) {
$usertime -= $a_ref->[0];
$systemtime -= $a_ref->[1];
$maxrss_delta -= $a_ref->[3];
} else {
$maxrss_delta = 0;
}
return [ $usertime, $systemtime, $usertime + $systemtime,
$maxrss, $maxrss_delta ];
}
__DATA__
< here is the template >
defined tests:
0) [% totallynotdef.defined ? 'fail' : 'okay' %]
1) [% totallynotdef.defined ? 'fail' : 'okay' %]
2) [% zero.defined ? 'okay' : 'fail' %]
3) [% one.defined ? 'okay' : 'fail' %]
4) [% nodef ? 'fail' : 'okay' %]
foreach:
0) size: [% primes.size %] ([% primes.nsort.first %]..[% primes.nsort.last %])
1) forward: [% FOREACH p = primes %] [% p %] [% END %]
2) reverse: [% FOREACH p = primes.reverse %] [% p %] [% END %]
3) hash: [% FOREACH p = stuff %] [% p.a %] [% p.b %] [% p.c %] [% END %]
4) hash sort keys: [% FOREACH p = phones.sort %] [% p %] [% END %]
5) [% FOREACH people.sort('id') -%] [% name +%] [% END %]
6) reverse 0. [% a_0.reverse.join(",") %]
7) reverse 1. [% a_1.reverse.join(",") %]
8) reverse 2. [% a_2.reverse.join(",") %]
first and last:
0) [% string %] [% metavars.first %] [% metavars.last %]
1) [% r.e.c.u.r.s.e.primes.nsort.first %]...[% recurse.primes.nsort.last %]
2) [% r.e.c.u.r.s.e.primes.sort.first %]...[% recurse.primes.sort.last %]
string split.join:
0) [% string.length %]
1) [% string.split.join('_') %]
2) [% spaced.split.join('_') %]
hash: (each, keys, values) join:
0) [% hash.each.join(', ') %]
1) [% hash.keys.join(', ') %]
2) [% hash.values.join(', ') %]
first, last, size:
0) [% metavars.first %]
1) [% metavars.last %]
2) [% metavars.size %]
3) [% metavars.max %]
joins:
0) [% metavars.join %]
1) [% metavars.join(', ') %]
assign and repeat:
0) [% string = 'foo' %] [% string.repeat(3) %]
more foreach, sort, etc:
0) [% FOREACH person = people.sort('id') -%] [% person.name +%] [% END %]
1) [% FOREACH person = people.sort('name') -%] [% person.name +%] [% END %]
2) [% FOREACH n = phones.sort -%] [% phones.$n %] is [% n %], [% END %]
3) [% FOREACH n = groceries.nsort.reverse -%] I got [% groceries.$n %] kilos of [% n %]! [% END %]
4) [% FOREACH item = [ 'foo', 'bar', 'baz' ] -%]
[%- "<ul>\n" IF loop.first %]
<li>[% loop.count %]/[% loop.size %]: [% item %]
[%- "</ul>\n" IF loop.last %]
[% END %]
commify:
0) [% FOREACH item = people %][%item.name%][%UNLESS loop.last%],[%END%][%END%]
1) [% FOREACH item = people; item.name; ',' UNLESS loop.last; END; %]
methods:
0) [% method.bar %]
1) [% method.baz %]
2) [% method.bad %]
3) [% method.newnew.bar %]
4) [% method.more("stuff") %]
5) [% quux.first.bar %] -- [% quux.last.more("junk") %]
6) [% x = quux.1; x.0 %]
lots o' dots:
0) [% bar.baz.boz.bean %]
1) [% a.b.c.d.e.f = [ 1 2 3 4 ] %]
2) [% a.b.c.d.e.f.join(",") %]
include/block: [% BLOCK testblock %]bar=[% bar %]foo=[% foo %][% END %]
0) [% INCLUDE testblock bar=2, foo='cat sat on mat' %]
process/block: [% BLOCK testproc %]one=[% one %]zero=[% zero %]foo=[% foo %][% END %]
0) [% PROCESS testproc bar=2, foo='matt sat on cat' %]
slices:
0) [% items = [ 'foo', 'bar', 'baz' ];
take = [ 0, 2 ];
slice = items.$take;
slice.join(', '); -%]
1) [% items = { foo = 'one', bar = 'two', baz = 'three' };
take = [ 'foo', 'baz' ];
slice = items.$take;
slice.join(', '); %]
cgi:
0) [% cgi.param('mode') %]
1) [% cgi.start_form %]
[% cgi.popup_menu(name => 'items',
values => [ 'foo' 'bar' 'baz' ]) %]
[% cgi.end_form %]
if/else:
0) [% IF one %] okay [% END %]
1) [% IF one %] okay [% ELSE %] fail [% END %]
2) [% IF one and string %] okay [% ELSE %] fail [% END %]
3) [% IF one && string %] okay [% ELSE %] fail [% END %]
4) [% IF false || one %] okay [% ELSE %] fail [% END %]
5) [% IF zero && one %] fail [% ELSE %] okay [% END %]
6) [% " okay" UNLESS zero %]
7) [% IF recurse.one %] okay [% ELSE %] fail [% END %]
8) [% IF r.e.c.u.r.s.e.one %] okay [% ELSE %] fail [% END %]
9) [% IF r.e.c.u.r.s.e.one.defined %] okay [% ELSE %] fail [% END %]
ref:
0) a: [% ref.a %] a(5): [% ref.a(5) %] a(5,10): [% ref.a(5,10) %]
assignments:
[% ten = 10
twenty = 20
thirty = twenty + ten
forty = 2 * twenty
fifty = 100 div 2
six = twenty mod 7 -%]
0) 10=[%ten%] 20=[%twenty%] 30=[%thirty%] 40=[%forty%] 50=[%fifty%] 6=[%six%]
[%- DEFAULT
seventy = 70
fifty = -5 -%]
1) 50=[%fifty%] 70=[%seventy%]
[%- foo = { bar = 'Baz' } -%]
2) foo=[% foo.bar %]
3) [% META title = 'The Cat in the Hat'
author = 'Dr. Seuss'
version = 1.23 -%][% template.title %]
[% template.author %]
errors, try, catch, etc:
0) [% TRY %]
...blah...blah...
[% CALL somecode %]
...etc...
[% INCLUDE someblock %]
...and so on...
[% CATCH %]
An error of type [% error.type %] occurred!
[% END %]
1) [% TRY -%]
[% INCLUDE missingfile -%]
[% CATCH file ; "File Error! $error.info"; END %]
2) [% TRY -%]
This gets printed
[% THROW food 'carrots' %]
This doesn't
[% CATCH food %]
culinary delights: [% error.info %]
[% END %]
3) [% TRY -%][% method.err %][% CATCH %]error type=[% error.type %]
info=[% error.info %][% END %]
views & filters
0) [% VIEW my.foo quux=33; END -%] [% my.foo.quux %]
1) [% FILTER baz('quux') -%] the itsy bitsy spider [% END %]
2) [% FILTER baz -%] the itsy bitsy spider [% END %]
more tests (crashme, etc.)
0) [% x = [1]; x.y.z = 2; %]
0) [% x = [1,2]; x.a.b.c = 3; %]
--------------------
end of template test ... have a pleasant day
--------------------
__END__
This stuff leaks HUGE amounts of memory:
ref:
[% b = \ref.a -%]
b: [% b %] b(5): [% b(5) %] b(5,10): [% b(5,10) %]
[% c = \ref.a(10,20) -%]
c: [% c %] c(30): [% c(30) %] c(30,40): [% c(30,40) %]
[% f = \ref.j.k -%]
f: [% f %]
[% f = \ref.j.m.n -%]
f: [% f %] f(11): [% f(11) %]
|