« lib/Template/Benchmark/Engines/TextXslateTT.pm
↑ Summary
lib/Template/Benchmark/Engines/Tenjin.pm »
| 1 | package Template::Benchmark::Engines::MojoTemplate; | ||
| 2 | |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use base qw/Template::Benchmark::Engine/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use File::Spec; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 0 | ▶ | use Mojo; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use Mojo::Template; | ||
| 12 | |||
| 13 | use IO::File; | ||
| 14 | |||
| 15 | our $VERSION = '1.09_01'; | ||
| 16 | |||
| 17 | our %feature_syntaxes = ( | ||
| 18 | literal_text => | ||
| 19 | join( "\n", ( join( ' ', ( 'foo' ) x 12 ) ) x 5 ), | ||
| 20 | scalar_variable => | ||
| 21 | '<%= $_[0]->{scalar_variable} %>', | ||
| 22 | hash_variable_value => | ||
| 23 | '<%= $_[0]->{hash_variable}{hash_value_key} %>', | ||
| 24 | array_variable_value => | ||
| 25 | '<%= $_[0]->{array_variable}[ 2 ] %>', | ||
| 26 | deep_data_structure_value => | ||
| 27 | '<%= $_[0]->{this}{is}{a}{very}{deep}{hash}{structure} %>', | ||
| 28 | array_loop_value => | ||
| 29 | '<% foreach ( @{$_[0]->{array_loop}} ) { %><%= $_ %><% } %>', | ||
| 30 | hash_loop_value => | ||
| 31 | '<% foreach ( sort( keys( %{$_[0]->{hash_loop}} ) ) ) { %>' . | ||
| 32 | '<%= $_ %>: <%= $_[0]->{hash_loop}{$_} %>' . | ||
| 33 | '<% } %>', | ||
| 34 | records_loop_value => | ||
| 35 | '<% foreach ( @{$_[0]->{records_loop}} ) { %>' . | ||
| 36 | '<%= $_->{name} %>: <%= $_->{age} %>' . | ||
| 37 | '<% } %>', | ||
| 38 | array_loop_template => | ||
| 39 | '<% foreach ( @{$_[0]->{array_loop}} ) { %><%= $_ %><% } %>', | ||
| 40 | hash_loop_template => | ||
| 41 | '<% foreach ( sort( keys( %{$_[0]->{hash_loop}} ) ) ) { %>' . | ||
| 42 | '<%= $_ %>: <%= $_[0]->{hash_loop}{$_} %>' . | ||
| 43 | '<% } %>', | ||
| 44 | records_loop_template => | ||
| 45 | '<% foreach ( @{$_[0]->{records_loop}} ) { %>' . | ||
| 46 | '<%= $_->{name} %>: <%= $_->{age} %>' . | ||
| 47 | '<% } %>', | ||
| 48 | constant_if_literal => | ||
| 49 | '<% if( 1 ) { %>true<% } %>', | ||
| 50 | variable_if_literal => | ||
| 51 | '<% if( $_[0]->{variable_if} ) { %>true<% } %>', | ||
| 52 | constant_if_else_literal => | ||
| 53 | '<% if( 1 ) { %>true<% } else { %>false<% } %>', | ||
| 54 | variable_if_else_literal => | ||
| 55 | '<% if( $_[0]->{variable_if_else} ) { %>true<% } else { %>' . | ||
| 56 | 'false<% } %>', | ||
| 57 | constant_if_template => | ||
| 58 | '<% if( 1 ) { %><%= $_[0]->{template_if_true} %><% } %>', | ||
| 59 | variable_if_template => | ||
| 60 | '<% if( $_[0]->{variable_if} ) { %><%= $_[0]->{template_if_true} %>' . | ||
| 61 | '<% } %>', | ||
| 62 | constant_if_else_template => | ||
| 63 | '<% if( 1 ) { %><%= $_[0]->{template_if_true} %><% } else { %>' . | ||
| 64 | '<%= $_[0]->{template_if_false} %><% } %>', | ||
| 65 | variable_if_else_template => | ||
| 66 | '<% if( $_[0]->{variable_if_else} ) { %>' . | ||
| 67 | '<%= $_[0]->{template_if_true} %><% } else { %>' . | ||
| 68 | '<%= $_[0]->{template_if_false} %><% } %>', | ||
| 69 | constant_expression => | ||
| 70 | '<%= 10 + 12 %>', | ||
| 71 | variable_expression => | ||
| 72 | '<%= $_[0]->{variable_expression_a} * ' . | ||
| 73 | '$_[0]->{variable_expression_b} %>', | ||
| 74 | complex_variable_expression => | ||
| 75 | '<%= ( ( $_[0]->{variable_expression_a} * ' . | ||
| 76 | '$_[0]->{variable_expression_b} ) + ' . | ||
| 77 | '$_[0]->{variable_expression_a} - ' . | ||
| 78 | '$_[0]->{variable_expression_b} ) / ' . | ||
| 79 | '$_[0]->{variable_expression_b} %>', | ||
| 80 | constant_function => | ||
| 81 | q{<%= substr( 'this has a substring.', 11, 9 ) %>}, | ||
| 82 | variable_function => | ||
| 83 | '<%= substr( $_[0]->{variable_function_arg}, 4, 2 ) %>', | ||
| 84 | ); | ||
| 85 | |||
| 86 | sub syntax_type { return( 'embedded-perl' ); } | ||
| 87 | sub pure_perl { return( 1 ); } | ||
| 88 | |||
| 89 | sub benchmark_descriptions | ||
| 90 | { | ||
| 91 | return( { | ||
| 92 | MoTe => | ||
| 93 | "Mojo::Template ($Mojo::VERSION)", | ||
| 94 | } ); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub benchmark_functions_for_uncached_string | ||
| 98 | { | ||
| 99 | my ( $self ) = @_; | ||
| 100 | |||
| 101 | return( { | ||
| 102 | MoTe => | ||
| 103 | sub | ||
| 104 | { | ||
| 105 | my $t = Mojo::Template->new(); | ||
| 106 | \$t->render( $_[ 0 ], { %{$_[ 1 ]}, %{$_[ 2 ]} } ); | ||
| 107 | }, | ||
| 108 | } ); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub benchmark_functions_for_uncached_disk | ||
| 112 | { | ||
| 113 | my ( $self, $template_dir ) = @_; | ||
| 114 | |||
| 115 | return( { | ||
| 116 | MoTe => | ||
| 117 | sub | ||
| 118 | { | ||
| 119 | my $t = Mojo::Template->new(); | ||
| 120 | \$t->render_file( | ||
| 121 | File::Spec->catfile( $template_dir, $_[ 0 ] ), | ||
| 122 | { %{$_[ 1 ]}, %{$_[ 2 ]} } ); | ||
| 123 | }, | ||
| 124 | } ); | ||
| 125 | } | ||
| 126 | |||
| 127 | sub benchmark_functions_for_disk_cache | ||
| 128 | { | ||
| 129 | my ( $self, $template_dir, $cache_dir ) = @_; | ||
| 130 | |||
| 131 | return( undef ); | ||
| 132 | } | ||
| 133 | |||
| 134 | sub benchmark_functions_for_shared_memory_cache | ||
| 135 | { | ||
| 136 | my ( $self, $template_dir, $cache_dir ) = @_; | ||
| 137 | |||
| 138 | return( undef ); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub benchmark_functions_for_memory_cache | ||
| 142 | { | ||
| 143 | my ( $self, $template_dir, $cache_dir ) = @_; | ||
| 144 | |||
| 145 | return( undef ); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub benchmark_functions_for_instance_reuse | ||
| 149 | { | ||
| 150 | my ( $self, $template_dir, $cache_dir ) = @_; | ||
| 151 | my ( $t ); | ||
| 152 | |||
| 153 | # Ew ick, I was almost templated to leave this as "unsupported" | ||
| 154 | # given how much crud I need to wrap around the template engine | ||
| 155 | # to get it to do this basic task. | ||
| 156 | return( { | ||
| 157 | MoTe => | ||
| 158 | sub | ||
| 159 | { | ||
| 160 | unless( $t ) | ||
| 161 | { | ||
| 162 | my ( $fh, $template ); | ||
| 163 | |||
| 164 | $t = Mojo::Template->new(); | ||
| 165 | $fh = IO::File->new( | ||
| 166 | File::Spec->catfile( $template_dir, $_[ 0 ] ), '<' ); | ||
| 167 | { | ||
| 168 | local $/ = undef; | ||
| 169 | $template = <$fh>; | ||
| 170 | } | ||
| 171 | $fh->close(); | ||
| 172 | |||
| 173 | $t->parse( $template ); | ||
| 174 | $t->build(); | ||
| 175 | $t->compile(); | ||
| 176 | } | ||
| 177 | \$t->interpret( { %{$_[ 1 ]}, %{$_[ 2 ]} } ); | ||
| 178 | }, | ||
| 179 | } ); | ||
| 180 | } | ||
| 181 | |||
| 182 | 1; | ||
| 183 | |||
| 184 | __END__ | ||