diff --git a/lib/Template/Benchmark/Engines/Mason2.pm b/lib/Template/Benchmark/Engines/Mason2.pm new file mode 100644 index 0000000..4f81e99 --- /dev/null +++ b/lib/Template/Benchmark/Engines/Mason2.pm @@ -0,0 +1,184 @@ +package Template::Benchmark::Engines::Mason2; + +use warnings; +use strict; + +use base qw/Template::Benchmark::Engine/; + +use Mason; + +use File::Spec; + +our $VERSION = '1.09_02'; + +our %feature_syntaxes = ( + literal_text => + join( "\n", ( join( ' ', ( 'foo' ) x 12 ) ) x 5 ), + scalar_variable => + '<% $.args->{scalar_variable} %>', + hash_variable_value => + '<% $.args->{hash_variable}->{hash_value_key} %>', + array_variable_value => + '<% $.args->{array_variable}->[ 2 ] %>', + deep_data_structure_value => + '<% $.args->{this}->{is}{a}{very}{deep}{hash}{structure} %>', + array_loop_value => + '<%perl>foreach ( @{$.args->{array_loop}} ) {' . + '<% $_ %>' . + '<%perl>}' . "\n", + hash_loop_value => + '<%perl>foreach ( sort( keys( %{$.args->{hash_loop}} ) ) ) {' . + '<% $_ %>: <% $.args->{hash_loop}->{$_} %>' . + '<%perl>}' . "\n", + records_loop_value => + '<%perl>foreach ( @{$.args->{records_loop}} ) {' . + '<% $_->{ name } %>: <% $_->{ age } %>' . + '<%perl>}' . "\n", + array_loop_template => + '<%perl>foreach ( @{$.args->{array_loop}} ) {' . + '<% $_ %>' . + '<%perl>}' . "\n", + hash_loop_template => + '<%perl>foreach ( sort( keys( %{$.args->{hash_loop}} ) ) ) {' . + '<% $_ %>: <% $.args->{hash_loop}->{$_} %>' . + '<%perl>}' . "\n", + records_loop_template => + '<%perl>foreach ( @{$.args->{records_loop}} ) {' . + '<% $_->{ name } %>: <% $_->{ age } %>' . + '<%perl>}' . "\n", + constant_if_literal => + '<%perl>if( 1 ) {true<%perl>}' . "\n", + variable_if_literal => + '<%perl>if( $.args->{variable_if} ) {true<%perl>}' . "\n", + constant_if_else_literal => + '<%perl>if( 1 ) {true<%perl>} else {' . + 'false<%perl>}' . "\n", + variable_if_else_literal => + '<%perl>if( $.args->{variable_if_else} ) {true<%perl>} ' . + 'else {false<%perl>}' . "\n", + constant_if_template => + '<%perl>if( 1 ) {' . + '<% $.args->{template_if_true} %><%perl>}' . "\n", + variable_if_template => + '<%perl>if( $.args->{variable_if} ) {' . + '<% $.args->{template_if_true} %><%perl>}' . "\n", + constant_if_else_template => + '<%perl>if( 1 ) {' . + '<% $.args->{template_if_true} %><%perl>} ' . + 'else {' . + '<% $.args->{template_if_false} %><%perl>}' . "\n", + variable_if_else_template => + '<%perl>if( $.args->{variable_if_else} ) {' . + '<% $.args->{template_if_true} %><%perl>} ' . + 'else {' . + '<% $.args->{template_if_false} %><%perl>}' . "\n", + constant_expression => + '<% 10 + 12 %>', + variable_expression => + '<% $.args->{variable_expression_a} * $.args->{variable_expression_b} %>', + complex_variable_expression => + '<% ( ( $.args->{variable_expression_a} * $.args->{variable_expression_b} ) + ' . + '$.args->{variable_expression_a} - $.args->{variable_expression_b} ) / ' . + '$.args->{variable_expression_b} %>', + constant_function => + q[<% substr( 'this has a substring.', 11, 9 ) %>], + variable_function => + '<% substr( $.args->{variable_function_arg}, 4, 2 ) %>', + ); + +sub syntax_type { return( 'embedded-perl' ); } +sub pure_perl { return( 1 ); } + +sub benchmark_descriptions +{ + return( { + M2 => + "Mason ($Mason::VERSION)", + } ); +} + +sub benchmark_functions_for_uncached_disk +{ + my ( $self, $template_dir ) = @_; + + return( { + M2 => + sub + { + my $out = ''; + my $t = Mason->new( + comp_root => $template_dir, + static_source => 1, + out_method => \$out, + autoextend_request_path => 0, + top_level_extensions => [], + ); + + $t->run( + # Don't use File::Spec, Mason reads it like a URL path. + '/' . $_[ 0 ], + %{$_[ 1 ]}, %{$_[ 2 ]}, + ); + \$out; + }, + } ); +} + +sub benchmark_functions_for_disk_cache +{ + my ( $self, $template_dir, $cache_dir ) = @_; + + return( { + M2 => + sub + { + my $out = ''; + my $t = Mason->new( + comp_root => $template_dir, + data_dir => $cache_dir, + static_source => 1, + out_method => \$out, + autoextend_request_path => 0, + top_level_extensions => [], + ); + + $t->run( + # Don't use File::Spec, Mason reads it like a URL path. + '/' . $_[ 0 ], + %{$_[ 1 ]}, %{$_[ 2 ]}, + ); + \$out; + }, + } ); +} + +sub benchmark_functions_for_instance_reuse +{ + my ( $self, $template_dir, $cache_dir ) = @_; + my ( $t, $out ); + + $t = Mason->new( + comp_root => $template_dir, + data_dir => $cache_dir, + static_source => 1, + out_method => \$out, + autoextend_request_path => 0, + top_level_extensions => [], + ); + + return( { + M2 => + sub + { + $out = ''; + $t->run( + # Don't use File::Spec, Mason reads it like a URL path. + '/' . $_[ 0 ], + %{$_[ 1 ]}, %{$_[ 2 ]}, + ); + \$out; + }, + } ); +} + +1;