« lib/Template/Benchmark/Engines/TemplateAlloyTT.pm
↑ Summary
lib/Template/Benchmark/Engines/TemplateTiny.pm »
| 1 | package Template::Benchmark::Engines::TemplateSandbox; | ||
| 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 Template::Sandbox; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use Template::Sandbox::StringFunctions qw/substr/; | ||
| 10 | |||
| 11 | use Cache::CacheFactory; | ||
| 12 | use Cache::FastMmap; | ||
| 13 | use Cache::FastMemoryCache; | ||
| 14 | use Cache::Ref::FIFO; | ||
| 15 | use CHI; | ||
| 16 | |||
| 17 | our $VERSION = '1.09_01'; | ||
| 18 | |||
| 19 | our %feature_syntaxes = ( | ||
| 20 | literal_text => | ||
| 21 | join( "\n", ( join( ' ', ( 'foo' ) x 12 ) ) x 5 ), | ||
| 22 | scalar_variable => | ||
| 23 | '<: expr scalar_variable :>', | ||
| 24 | hash_variable_value => | ||
| 25 | '<: expr hash_variable.hash_value_key :>', | ||
| 26 | array_variable_value => | ||
| 27 | '<: expr array_variable[ 2 ] :>', | ||
| 28 | deep_data_structure_value => | ||
| 29 | '<: expr this.is.a.very.deep.hash.structure :>', | ||
| 30 | array_loop_value => | ||
| 31 | '<: foreach i in array_loop :><: expr i :><: endfor :>', | ||
| 32 | hash_loop_value => | ||
| 33 | '<: foreach k in hash_loop :><: expr k :>: ' . | ||
| 34 | '<: expr k.__value__ :><: endfor :>', | ||
| 35 | records_loop_value => | ||
| 36 | '<: foreach r in records_loop :><: expr r.name :>: ' . | ||
| 37 | '<: expr r.age :><: endfor :>', | ||
| 38 | array_loop_template => | ||
| 39 | '<: foreach i in array_loop :><: expr i :><: endfor :>', | ||
| 40 | hash_loop_template => | ||
| 41 | '<: foreach k in hash_loop :><: expr k :>: ' . | ||
| 42 | '<: expr k.__value__ :><: endfor :>', | ||
| 43 | records_loop_template => | ||
| 44 | '<: foreach r in records_loop :><: expr r.name :>: ' . | ||
| 45 | '<: expr r.age :><: endfor :>', | ||
| 46 | constant_if_literal => | ||
| 47 | '<: if 1 :>true<: endif :>', | ||
| 48 | variable_if_literal => | ||
| 49 | '<: if variable_if :>true<: endif :>', | ||
| 50 | constant_if_else_literal => | ||
| 51 | '<: if 1 :>true<: else :>false<: endif :>', | ||
| 52 | variable_if_else_literal => | ||
| 53 | '<: if variable_if_else :>true<: else :>false<: endif :>', | ||
| 54 | constant_if_template => | ||
| 55 | '<: if 1 :><: expr template_if_true :><: endif :>', | ||
| 56 | variable_if_template => | ||
| 57 | '<: if variable_if :><: expr template_if_true :><: endif :>', | ||
| 58 | constant_if_else_template => | ||
| 59 | '<: if 1 :><: expr template_if_true :><: else :>' . | ||
| 60 | '<: expr template_if_false :><: endif :>', | ||
| 61 | variable_if_else_template => | ||
| 62 | '<: if variable_if_else :><: expr template_if_true :><: else :>' . | ||
| 63 | '<: expr template_if_false :><: endif :>', | ||
| 64 | constant_expression => | ||
| 65 | '<: expr 10 + 12 :>', | ||
| 66 | variable_expression => | ||
| 67 | '<: expr variable_expression_a * variable_expression_b :>', | ||
| 68 | complex_variable_expression => | ||
| 69 | '<: expr ( ( variable_expression_a * variable_expression_b ) + ' . | ||
| 70 | 'variable_expression_a - variable_expression_b ) / ' . | ||
| 71 | 'variable_expression_b :>', | ||
| 72 | constant_function => | ||
| 73 | q{<: expr substr( 'this has a substring.', 11, 9 ) :>}, | ||
| 74 | variable_function => | ||
| 75 | '<: expr substr( variable_function_arg, 4, 2 ) :>', | ||
| 76 | ); | ||
| 77 | |||
| 78 | sub syntax_type { return( 'mini-language' ); } | ||
| 79 | sub pure_perl { return( 1 ); } | ||
| 80 | |||
| 81 | sub benchmark_descriptions | ||
| 82 | { | ||
| 83 | return( { | ||
| 84 | TS => | ||
| 85 | "Template::Sandbox ($Template::Sandbox::VERSION) without caching", | ||
| 86 | TS_CF => | ||
| 87 | "Template::Sandbox ($Template::Sandbox::VERSION) with " . | ||
| 88 | "Cache::CacheFactory ($Cache::CacheFactory::VERSION) caching", | ||
| 89 | TS_CHI => | ||
| 90 | "Template::Sandbox ($Template::Sandbox::VERSION) with " . | ||
| 91 | "CHI ($CHI::VERSION) caching", | ||
| 92 | TS_CFM => | ||
| 93 | "Template::Sandbox ($Template::Sandbox::VERSION) with " . | ||
| 94 | "Cache::FastMemoryCache ($Cache::FastMemoryCache::VERSION) caching", | ||
| 95 | TS_CRF => | ||
| 96 | "Template::Sandbox ($Template::Sandbox::VERSION) with " . | ||
| 97 | "Cache::Ref::FIFO ($Cache::Ref::FIFO::VERSION) caching", | ||
| 98 | TS_FMM => | ||
| 99 | "Template::Sandbox ($Template::Sandbox::VERSION) with " . | ||
| 100 | "Cache::FastMmap ($Cache::FastMmap::VERSION) caching", | ||
| 101 | } ); | ||
| 102 | } | ||
| 103 | |||
| 104 | sub benchmark_functions_for_uncached_string | ||
| 105 | { | ||
| 106 | my ( $self ) = @_; | ||
| 107 | |||
| 108 | return( { | ||
| 109 | TS => | ||
| 110 | sub | ||
| 111 | { | ||
| 112 | my $t = Template::Sandbox->new(); | ||
| 113 | $t->set_template_string( $_[ 0 ] ); | ||
| 114 | $t->add_vars( $_[ 1 ] ); | ||
| 115 | $t->add_vars( $_[ 2 ] ); | ||
| 116 | $t->run(); | ||
| 117 | }, | ||
| 118 | } ); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub benchmark_functions_for_uncached_disk | ||
| 122 | { | ||
| 123 | my ( $self, $template_dir ) = @_; | ||
| 124 | |||
| 125 | return( { | ||
| 126 | TS => | ||
| 127 | sub | ||
| 128 | { | ||
| 129 | my $t = Template::Sandbox->new( | ||
| 130 | template_root => $template_dir, | ||
| 131 | template => $_[ 0 ], | ||
| 132 | ); | ||
| 133 | $t->add_vars( $_[ 1 ] ); | ||
| 134 | $t->add_vars( $_[ 2 ] ); | ||
| 135 | $t->run(); | ||
| 136 | }, | ||
| 137 | } ); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub benchmark_functions_for_disk_cache | ||
| 141 | { | ||
| 142 | my ( $self, $template_dir, $cache_dir ) = @_; | ||
| 143 | my ( $cf, $chi ); | ||
| 144 | |||
| 145 | $cf = Cache::CacheFactory->new( | ||
| 146 | storage => { 'file' => { cache_root => $cache_dir, }, }, | ||
| 147 | ); | ||
| 148 | $chi = CHI->new( | ||
| 149 | driver => 'File', | ||
| 150 | root_dir => $cache_dir, | ||
| 151 | ); | ||
| 152 | |||
| 153 | return( { | ||
| 154 | TS_CF => | ||
| 155 | sub | ||
| 156 | { | ||
| 157 | my $t = Template::Sandbox->new( | ||
| 158 | cache => $cf, | ||
| 159 | template_root => $template_dir, | ||
| 160 | template => $_[ 0 ], | ||
| 161 | ignore_module_dependencies => 1, | ||
| 162 | ); | ||
| 163 | $t->add_vars( $_[ 1 ] ); | ||
| 164 | $t->add_vars( $_[ 2 ] ); | ||
| 165 | $t->run(); | ||
| 166 | }, | ||
| 167 | TS_CHI => | ||
| 168 | sub | ||
| 169 | { | ||
| 170 | my $t = Template::Sandbox->new( | ||
| 171 | cache => $chi, | ||
| 172 | template_root => $template_dir, | ||
| 173 | template => $_[ 0 ], | ||
| 174 | ignore_module_dependencies => 1, | ||
| 175 | ); | ||
| 176 | $t->add_vars( $_[ 1 ] ); | ||
| 177 | $t->add_vars( $_[ 2 ] ); | ||
| 178 | $t->run(); | ||
| 179 | }, | ||
| 180 | } ); | ||
| 181 | } | ||
| 182 | |||
| 183 | sub benchmark_functions_for_shared_memory_cache | ||
| 184 | { | ||
| 185 | my ( $self, $template_dir, $cache_dir ) = @_; | ||
| 186 | my ( $fmm, $chi ); | ||
| 187 | |||
| 188 | $fmm = Cache::FastMmap->new( | ||
| 189 | share_file => "$cache_dir/cache_mmap", | ||
| 190 | cache_size => '100k', | ||
| 191 | ); | ||
| 192 | $chi = CHI->new( | ||
| 193 | driver => 'FastMmap', | ||
| 194 | root_dir => $cache_dir, | ||
| 195 | cache_size => '100k', | ||
| 196 | ); | ||
| 197 | |||
| 198 | return( { | ||
| 199 | TS_FMM => | ||
| 200 | sub | ||
| 201 | { | ||
| 202 | my $t = Template::Sandbox->new( | ||
| 203 | cache => $fmm, | ||
| 204 | template_root => $template_dir, | ||
| 205 | template => $_[ 0 ], | ||
| 206 | ignore_module_dependencies => 1, | ||
| 207 | ); | ||
| 208 | $t->add_vars( $_[ 1 ] ); | ||
| 209 | $t->add_vars( $_[ 2 ] ); | ||
| 210 | $t->run(); | ||
| 211 | }, | ||
| 212 | TS_CHI => | ||
| 213 | sub | ||
| 214 | { | ||
| 215 | my $t = Template::Sandbox->new( | ||
| 216 | cache => $chi, | ||
| 217 | template_root => $template_dir, | ||
| 218 | template => $_[ 0 ], | ||
| 219 | ignore_module_dependencies => 1, | ||
| 220 | ); | ||
| 221 | $t->add_vars( $_[ 1 ] ); | ||
| 222 | $t->add_vars( $_[ 2 ] ); | ||
| 223 | $t->run(); | ||
| 224 | }, | ||
| 225 | } ); | ||
| 226 | } | ||
| 227 | |||
| 228 | sub benchmark_functions_for_memory_cache | ||
| 229 | { | ||
| 230 | my ( $self, $template_dir, $cache_dir ) = @_; | ||
| 231 | my ( $cf, $chi, $cfm, $crf ); | ||
| 232 | |||
| 233 | $cf = Cache::CacheFactory->new( | ||
| 234 | storage => 'fastmemory', | ||
| 235 | no_deep_clone => 1, | ||
| 236 | ); | ||
| 237 | $chi = CHI->new( | ||
| 238 | driver => 'Memory', | ||
| 239 | global => 1, | ||
| 240 | ); | ||
| 241 | $cfm = Cache::FastMemoryCache->new(); | ||
| 242 | $crf = Cache::Ref::FIFO->new( | ||
| 243 | size => 1024, | ||
| 244 | ); | ||
| 245 | |||
| 246 | return( { | ||
| 247 | TS_CF => | ||
| 248 | sub | ||
| 249 | { | ||
| 250 | my $t = Template::Sandbox->new( | ||
| 251 | cache => $cf, | ||
| 252 | template_root => $template_dir, | ||
| 253 | template => $_[ 0 ], | ||
| 254 | ignore_module_dependencies => 1, | ||
| 255 | ); | ||
| 256 | $t->add_vars( $_[ 1 ] ); | ||
| 257 | $t->add_vars( $_[ 2 ] ); | ||
| 258 | $t->run(); | ||
| 259 | }, | ||
| 260 | TS_CHI => | ||
| 261 | sub | ||
| 262 | { | ||
| 263 | my $t = Template::Sandbox->new( | ||
| 264 | cache => $chi, | ||
| 265 | template_root => $template_dir, | ||
| 266 | template => $_[ 0 ], | ||
| 267 | ignore_module_dependencies => 1, | ||
| 268 | ); | ||
| 269 | $t->add_vars( $_[ 1 ] ); | ||
| 270 | $t->add_vars( $_[ 2 ] ); | ||
| 271 | $t->run(); | ||
| 272 | }, | ||
| 273 | TS_CFM => | ||
| 274 | sub | ||
| 275 | { | ||
| 276 | my $t = Template::Sandbox->new( | ||
| 277 | cache => $cfm, | ||
| 278 | template_root => $template_dir, | ||
| 279 | template => $_[ 0 ], | ||
| 280 | ignore_module_dependencies => 1, | ||
| 281 | ); | ||
| 282 | $t->add_vars( $_[ 1 ] ); | ||
| 283 | $t->add_vars( $_[ 2 ] ); | ||
| 284 | $t->run(); | ||
| 285 | }, | ||
| 286 | TS_CRF => | ||
| 287 | sub | ||
| 288 | { | ||
| 289 | my $t = Template::Sandbox->new( | ||
| 290 | cache => $crf, | ||
| 291 | template_root => $template_dir, | ||
| 292 | template => $_[ 0 ], | ||
| 293 | ignore_module_dependencies => 1, | ||
| 294 | ); | ||
| 295 | $t->add_vars( $_[ 1 ] ); | ||
| 296 | $t->add_vars( $_[ 2 ] ); | ||
| 297 | $t->run(); | ||
| 298 | }, | ||
| 299 | } ); | ||
| 300 | } | ||
| 301 | |||
| 302 | sub benchmark_functions_for_instance_reuse | ||
| 303 | { | ||
| 304 | my ( $self, $template_dir, $cache_dir ) = @_; | ||
| 305 | my ( $t ); | ||
| 306 | |||
| 307 | # clear_vars() wasn't available in older Template::Sandbox versions. | ||
| 308 | return( undef ) unless Template::Sandbox->can( 'clear_vars' ); | ||
| 309 | |||
| 310 | return( { | ||
| 311 | TS => | ||
| 312 | sub | ||
| 313 | { | ||
| 314 | if( $t ) | ||
| 315 | { | ||
| 316 | $t->clear_vars(); | ||
| 317 | } | ||
| 318 | else | ||
| 319 | { | ||
| 320 | $t = Template::Sandbox->new( | ||
| 321 | template_root => $template_dir, | ||
| 322 | template => $_[ 0 ], | ||
| 323 | ); | ||
| 324 | } | ||
| 325 | $t->add_vars( $_[ 1 ] ); | ||
| 326 | $t->add_vars( $_[ 2 ] ); | ||
| 327 | $t->run(); | ||
| 328 | }, | ||
| 329 | } ); | ||
| 330 | } | ||
| 331 | |||
| 332 | 1; | ||
| 333 | |||
| 334 | __END__ | ||