From: Uri Guttman Date: Fri, 6 May 2011 06:52:03 +0000 (-0400) Subject: first version of new benchmark script X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b8f7f9ecb05c5b9f4bc01da4fb8ba5168add7bef;p=urisagit%2FTemplate-Simple.git first version of new benchmark script --- diff --git a/extras/bench_new.pl b/extras/bench_new.pl new file mode 100644 index 0000000..62101dc --- /dev/null +++ b/extras/bench_new.pl @@ -0,0 +1,254 @@ + +use warnings ; +use strict ; + +use Getopt::Long ; +use Data::Dumper ; + +use File::Slurp ; + +use Benchmark qw(:hireswallclock cmpthese); + +my $opts = parse_options() ; + +my $template_info = [ + { + name => 'basic', + data => { + name => 'bob', + }, + expected => 'hehe bob', + simple => 'hehe [% name %]', + toolkit => 'hehe [% name %]', + teeny => 'hehe [% name %]', + }, + { + name => 'nested', + data => { + title => 'Bobs Blog', + posts => [ + { + title => 'hehe', + date => 'Today' + }, + { + title => 'Something new', + date => '3 Days ago', + }, + ], + }, + expected => < + Bobs Blog + + + + +EXPECTED + + simple => < + [% title %] + +
    [% START posts %] +
  • +

    [% title %]

    + [% date %] +
  • [% END posts %] +
+ + +SIMPLE + + toolkit => < + [% title %] + +
    + [% FOREACH post = posts %] +
  • +

    [% post.title %]

    + [% post.date %] +
  • + [% END %] +
+ + +TOOLKIT + + teeny => < + [% title %] + +
    + [% SECTION post %] +
  • +

    [% title %]

    + [% date %] +
  • + [% END %] +
+ + +TEENY + }, +] ; + +my $benches = [ + { + name => 'T::S', + template_key => 'simple', + load => sub { + return eval { require Template::Simple } ; + }, + setup => sub { + my( $bench, $info ) = @_ ; + + my $template = $info->{$bench->{template_key}} ; + my $data = $info->{data} ; + my $name = $info->{name} ; + + my $obj = Template::Simple->new( + templates => { $name => $template } + ) ; + + $bench->{render} = + sub { $obj->render( $name, $data ) } ; + }, + verify => sub { + my( $bench, $info ) = @_ ; + my $result = $bench->{render}->() ; + $bench->{result} = ${$result} ; + }, + }, + { + name => 'T::S compiled', + template_key => 'simple', + load => sub { + return eval { require Template::Simple } ; + }, + setup => sub { + my( $bench, $info ) = @_ ; + + my $template = $info->{$bench->{template_key}} ; + my $data = $info->{data} ; + my $name = $info->{name} ; + + my $obj = Template::Simple->new( + templates => { $name => $template } + ) ; + $obj->compile( $name ) ; + + $bench->{render} = + sub { $obj->render( $name, $data ) } ; + }, + verify => sub { + my( $bench, $info ) = @_ ; + my $result = $bench->{render}->() ; + $bench->{result} = ${$result} ; + }, + }, +] ; + +run_benchmarks() ; + +sub run_benchmarks { + + foreach my $info ( @{$template_info} ) { + + my %compares ; + + foreach my $bench ( @{$benches} ) { + + my $loaded = $bench->{load}->() ; + unless( $loaded ) { + print + "skipping $bench->{name} as it didn't load\n" ; + next ; + } + + $bench->{setup}->( $bench, $info ) ; + $bench->{verify}->( $bench, $info ) ; + + if ( $bench->{result} ne $info->{expected} ) { + + print + "RESULT [$bench->{result}]\nEXPECTED [$info->{expected}]\n" ; + } + else { + print "'$bench->{name}' rendering of '$info->{name}' is verified\n" ; + } + + $compares{ $bench->{name} } = $bench->{render} ; + } + + cmpthese( $opts->{iterations}, \%compares ) ; + } +} + + + + +sub parse_options { + + GetOptions( \my %opts, + 'verify|v', + 'iterations|i', + 'templaters|t', + 'help|?', + ) ; + + usage( '' ) if $opts{ 'help' } ; + + $opts{iterations} ||= -2 ; + + $opts{templaters} = [split /,/, $opts{templaters}] + if $opts{templaters} ; + + return \%opts ; +} + +sub usage { + + my $err_msg = shift || '' ; + + my $usage = <<'=cut' ; + +bench_templates.pl - Benchmark Multiple Templaters + +=head1 SYNOPSIS + +load_menus.pl [--verify | -v] [--iterations | -i ] + [--templaters| -t ] [--help] + +=head1 DESCRIPTION + + --rid | -r Select the restaurant ID to be fetched. + Can be a comma separated list of rids. + 'ALL' will select all the valid + ID's from a source. This is + required (unless is set + to 'ALL'. + + [--file | -f] Load the XML from files. The file is + + [--help | ?] Print this help text + +=cut + + $usage =~ s/^=\w+.*$//mg ; + + $usage =~ s/\n{2,}/\n\n/g ; + $usage =~ s/\A\n+// ; + + die "$err_msg\n$usage" ; +}