added support for template::teeny
Uri Guttman [Fri, 6 May 2011 07:42:54 +0000 (03:42 -0400)]
extras/bench_new.pl [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 62101dc..24bd979
@@ -1,12 +1,15 @@
+#!/usr/bin/perl
+
+use lib '../lib' ;
+use lib 'lib' ;
 
 use warnings ;
 use strict ;
 
-use Getopt::Long ;
 use Data::Dumper ;
 
+use Getopt::Long ;
 use File::Slurp ;
-
 use Benchmark qw(:hireswallclock cmpthese);
 
 my $opts = parse_options() ;
@@ -89,16 +92,15 @@ TOOLKIT
 <html>
   <head><title>[% title %]</title></head>
   <body>
-    <ul>
-      [% SECTION post %]
+    <ul>[% SECTION post %]
         <li>
             <h3>[% title %]</h3>
             <span>[% date %]</span>
-        </li>
-      [% END %]
+        </li>[% END %]
     </ul>
   </body>
 </html>
+
 TEENY
        },
 ] ;
@@ -157,6 +159,68 @@ my $benches = [
                        $bench->{result} = ${$result} ;
                },
        },
+       {
+               name    => 'Teeny',
+               template_key => 'teeny',
+               load    => sub {
+                       return eval {
+                               require Template::Teeny ;
+                               require Template::Teeny::Stash ;
+                       } ;
+               },
+               setup   => sub {
+                       my( $bench, $info ) = @_ ;
+
+                       my $template = $info->{$bench->{template_key}} ;
+                       my $data = $info->{data} ;
+                       my $name = $info->{name} ;
+
+                       my $results ;
+                       open my $fh, '>', \$results or
+                               die "can't open string for output" ;
+
+                       mkdir 'tpl' ;
+                       write_file( "tpl/$name.tpl", $template ) ;
+                       my $obj = Template::Teeny->new(
+                               { include_path => ['tpl'] }
+                       ) ;
+
+                       my $stash ;
+                       if ( my $posts = $data->{posts} ) {
+                       
+                               $stash = Template::Teeny::Stash->new(
+                                       { title => $data->{title} }
+                               ) ;
+
+                               foreach my $post ( @{$posts} ) {
+
+                                       my $substash =
+                                               Template::Teeny::Stash->new(
+                                                       $post
+                                       ) ;
+                                       $stash->add_section('post', $substash );
+                               }
+                       }
+                       else {
+                               $stash = Template::Teeny::Stash->new(
+                                       $data
+                               ) ;
+
+                       }
+
+#print Dumper $stash ;
+                       $bench->{render} =
+                               sub {
+                                     $obj->process("$name.tpl", $stash, $fh );
+                                     $bench->{result} = $results ;
+                               }
+
+               },
+               verify  => sub {
+                       my( $bench, $info ) = @_ ;
+                       $bench->{result} = $bench->{render}->() ;
+               },
+       },
 ] ;
 
 run_benchmarks() ;
@@ -171,33 +235,41 @@ sub run_benchmarks {
 
                        my $loaded = $bench->{load}->() ;
                        unless( $loaded ) {
-                               print
-                               "skipping $bench->{name} as it didn't load\n" ;
+                               print <<BAD ;
+Skipping $bench->{name} as it didn't load
+BAD
                                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" ;
+                       if ( $opts->{verify} ) {
+                               $bench->{verify}->( $bench, $info ) ;
+
+                               if ( $bench->{result} ne $info->{expected} ) {
+
+                                       print <<BAD ;
+Skipping $bench->{name} as it doesn't have verified results.
+RESULT [$bench->{result}]\nEXPECTED [$info->{expected}]
+BAD
+                                       next ;
+                               }
+                               else {
+                                       print <<GOOD ;
+'$bench->{name}' rendering of '$info->{name}' is verified
+GOOD
+                               }
                        }
 
                        $compares{ $bench->{name} } = $bench->{render} ;
                }
 
+               print "\nBenchmark of '$info->{name}' template\n" ;
                cmpthese( $opts->{iterations}, \%compares ) ;
+               print "\n" ;
        }
 }
 
-
-
-
 sub parse_options {
 
        GetOptions( \my %opts,