| 1 | package Template::Benchmark::Engines::HTMLMacro; | ||
| 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 | 0 | ▶ | use HTML::Macro; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | |||
| 10 | use File::Spec; | ||
| 11 | |||
| 12 | our $VERSION = '1.09_01'; | ||
| 13 | |||
| 14 | our %feature_syntaxes = ( | ||
| 15 | literal_text => | ||
| 16 | join( "\n", ( join( ' ', ( 'foo' ) x 12 ) ) x 5 ), | ||
| 17 | scalar_variable => | ||
| 18 | '#scalar_variable#', | ||
| 19 | hash_variable_value => | ||
| 20 | undef, | ||
| 21 | array_variable_value => | ||
| 22 | undef, | ||
| 23 | deep_data_structure_value => | ||
| 24 | undef, | ||
| 25 | array_loop_value => | ||
| 26 | '<loop id="array_loop_value">#value#</loop>', | ||
| 27 | hash_loop_value => | ||
| 28 | '<loop id="hash_loop_value">#key#: #value#</loop>', | ||
| 29 | records_loop_value => | ||
| 30 | '<loop id="records_loop_value">#name#: #age#</loop>', | ||
| 31 | array_loop_template => | ||
| 32 | '<loop id="array_loop_template">#value#</loop>', | ||
| 33 | hash_loop_template => | ||
| 34 | '<loop id="hash_loop_template">#key#: #value#</loop>', | ||
| 35 | records_loop_template => | ||
| 36 | '<loop id="records_loop_template">#name#: #age#</loop>', | ||
| 37 | constant_if_literal => | ||
| 38 | '<if expr="1">true</if>', | ||
| 39 | variable_if_literal => | ||
| 40 | '<if expr="#variable_if#">true</if>', | ||
| 41 | constant_if_else_literal => | ||
| 42 | '<if expr="1">true<else/>false</if>', | ||
| 43 | variable_if_else_literal => | ||
| 44 | '<if expr="#variable_if_else#">true<else/>false</if>', | ||
| 45 | constant_if_template => | ||
| 46 | '<if expr="1">#template_if_true#</if>', | ||
| 47 | variable_if_template => | ||
| 48 | '<if expr="#variable_if#">#template_if_true#</if>', | ||
| 49 | constant_if_else_template => | ||
| 50 | '<if expr="1">#template_if_true#<else/>' . | ||
| 51 | '#template_if_false#</if>', | ||
| 52 | variable_if_else_template => | ||
| 53 | '<if expr="#variable_if_else#">#template_if_true#<else/>' . | ||
| 54 | '#template_if_false#</if>', | ||
| 55 | constant_expression => | ||
| 56 | undef, | ||
| 57 | variable_expression => | ||
| 58 | undef, | ||
| 59 | complex_variable_expression => | ||
| 60 | undef, | ||
| 61 | constant_function => | ||
| 62 | undef, | ||
| 63 | variable_function => | ||
| 64 | undef, | ||
| 65 | ); | ||
| 66 | |||
| 67 | sub syntax_type { return( 'mini-language' ); } | ||
| 68 | sub pure_perl { return( 1 ); } | ||
| 69 | |||
| 70 | sub benchmark_descriptions | ||
| 71 | { | ||
| 72 | return( { | ||
| 73 | HMac => | ||
| 74 | "HTML::Macro ($HTML::Macro::VERSION)", | ||
| 75 | } ); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub benchmark_functions_for_uncached_string | ||
| 79 | { | ||
| 80 | my ( $self ) = @_; | ||
| 81 | |||
| 82 | return( undef ); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub benchmark_functions_for_uncached_disk | ||
| 86 | { | ||
| 87 | my ( $self, $template_dir ) = @_; | ||
| 88 | |||
| 89 | return( { | ||
| 90 | HMac => | ||
| 91 | sub | ||
| 92 | { | ||
| 93 | my $file = File::Spec->catfile( $template_dir, $_[ 0 ] ); | ||
| 94 | |||
| 95 | my $t = HTML::Macro->new( $file ); | ||
| 96 | my $h = { %{$_[ 1 ]}, %{$_[ 2 ]} }; | ||
| 97 | # Somewhat unfair since we cause the transform hit even | ||
| 98 | # if we're not benchmarking this feature, then again, everyone | ||
| 99 | # else has to store it in their stash too. | ||
| 100 | foreach my $id ( qw/value template/ ) | ||
| 101 | { | ||
| 102 | my $array_loop = $t->new_loop( "array_loop_$id", | ||
| 103 | 'value' ); | ||
| 104 | foreach my $value ( @{$h->{ array_loop }} ) | ||
| 105 | { | ||
| 106 | $array_loop->push_array( $value ); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | foreach my $id ( qw/value template/ ) | ||
| 110 | { | ||
| 111 | my $hash_loop = $t->new_loop( "hash_loop_$id", | ||
| 112 | 'key', 'value' ); | ||
| 113 | foreach my $key ( sort( keys( %{$h->{ hash_loop }} ) ) ) | ||
| 114 | { | ||
| 115 | $hash_loop->push_array( $key, | ||
| 116 | $h->{ hash_loop }{ $key } ); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | foreach my $id ( qw/value template/ ) | ||
| 120 | { | ||
| 121 | my $records_loop = $t->new_loop( "records_loop_$id" ); | ||
| 122 | foreach my $record ( @{$h->{ records_loop }} ) | ||
| 123 | { | ||
| 124 | $records_loop->push_hash( $record ); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | $t->set_hash( $h ); | ||
| 128 | \$t->process(); | ||
| 129 | }, | ||
| 130 | } ); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub benchmark_functions_for_disk_cache | ||
| 134 | { | ||
| 135 | my ( $self, $template_dir, $cache_dir ) = @_; | ||
| 136 | |||
| 137 | return( undef ); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub benchmark_functions_for_shared_memory_cache | ||
| 141 | { | ||
| 142 | my ( $self, $template_dir, $cache_dir ) = @_; | ||
| 143 | |||
| 144 | return( undef ); | ||
| 145 | } | ||
| 146 | |||
| 147 | sub benchmark_functions_for_memory_cache | ||
| 148 | { | ||
| 149 | my ( $self, $template_dir, $cache_dir ) = @_; | ||
| 150 | |||
| 151 | return( undef ); | ||
| 152 | } | ||
| 153 | |||
| 154 | sub benchmark_functions_for_instance_reuse | ||
| 155 | { | ||
| 156 | my ( $self, $template_dir, $cache_dir ) = @_; | ||
| 157 | my ( $t ); | ||
| 158 | |||
| 159 | return( { | ||
| 160 | HMac => | ||
| 161 | sub | ||
| 162 | { | ||
| 163 | unless( $t ) | ||
| 164 | { | ||
| 165 | my $file = File::Spec->catfile( $template_dir, $_[ 0 ] ); | ||
| 166 | $t = HTML::Macro->new( $file, | ||
| 167 | { | ||
| 168 | cache_files => 1, | ||
| 169 | } ); | ||
| 170 | } | ||
| 171 | |||
| 172 | my $h = { %{$_[ 1 ]}, %{$_[ 2 ]} }; | ||
| 173 | # Somewhat unfair since we cause the transform hit even | ||
| 174 | # if we're not benchmarking this feature, then again, everyone | ||
| 175 | # else has to store it in their stash too. | ||
| 176 | foreach my $id ( qw/value template/ ) | ||
| 177 | { | ||
| 178 | my $array_loop = $t->new_loop( "array_loop_$id", | ||
| 179 | 'value' ); | ||
| 180 | foreach my $value ( @{$h->{ array_loop }} ) | ||
| 181 | { | ||
| 182 | $array_loop->push_array( $value ); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | foreach my $id ( qw/value template/ ) | ||
| 186 | { | ||
| 187 | my $hash_loop = $t->new_loop( "hash_loop_$id", | ||
| 188 | 'key', 'value' ); | ||
| 189 | foreach my $key ( sort( keys( %{$h->{ hash_loop }} ) ) ) | ||
| 190 | { | ||
| 191 | $hash_loop->push_array( $key, | ||
| 192 | $h->{ hash_loop }{ $key } ); | ||
| 193 | } | ||
| 194 | } | ||
| 195 | foreach my $id ( qw/value template/ ) | ||
| 196 | { | ||
| 197 | my $records_loop = $t->new_loop( "records_loop_$id" ); | ||
| 198 | foreach my $record ( @{$h->{ records_loop }} ) | ||
| 199 | { | ||
| 200 | $records_loop->push_hash( $record ); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | $t->set_hash( $h ); | ||
| 204 | \$t->process(); | ||
| 205 | }, | ||
| 206 | } ); | ||
| 207 | } | ||
| 208 | |||
| 209 | 1; | ||
| 210 | |||
| 211 | __END__ | ||