File Coverage

blib/lib/Template/Timer.pm
Criterion Covered Total %
statement 39 40 97.5
branch 9 10 90.0
condition n/a
subroutine 7 7 100.0
pod n/a
total 55 57 96.4


line stmt bran cond sub pod time code
1             package Template::Timer;
2              
3 2     2   21993 use warnings;
  2         6  
  2         67  
4 2     2   11 use strict;
  2         3  
  2         143  
5              
6             =head1 NAME
7              
8             Template::Timer - Rudimentary profiling for Template Toolkit
9              
10             =head1 VERSION
11              
12             Version 1.01_02
13              
14             =cut
15              
16             our $VERSION = '1.01_02';
17              
18             =head1 SYNOPSIS
19              
20             Template::Timer provides inline timings of the template processing
21             througout your code. It's an overridden version of L
22             that wraps the C and C methods.
23              
24             Using Template::Timer is simple.
25              
26             use Template::Timer;
27              
28             my %config = ( # Whatever your config is
29             INCLUDE_PATH => '/my/template/path',
30             COMPILE_EXT => '.ttc',
31             COMPILE_DIR => '/tmp/tt',
32             );
33              
34             if ( $development_mode ) {
35             $config{ CONTEXT } = Template::Timer->new( %config );
36             }
37              
38             my $template = Template->new( \%config );
39              
40             Now when you process templates, HTML comments will get embedded in your
41             output, which you can easily grep for. The nesting level is also shown.
42              
43            
74              
75             Note that since INCLUDE is a wrapper around PROCESS, calls to INCLUDEs
76             will be doubled up, and slightly longer than the PROCESS call.
77              
78             =cut
79              
80 2     2   11 use base qw( Template::Context );
  2         8  
  2         3144  
81 2     2   35351 use Time::HiRes ();
  2         3537  
  2         148  
82              
83             our $depth = 0;
84             our $epoch = undef;
85             our @totals;
86              
87             foreach my $sub ( qw( process include ) ) {
88             my $ip = uc substr( $sub, 0, 1 ); # Include or Process?
89              
90 2     2   14 no strict 'refs';
  2         5  
  2         915  
91             my $super = __PACKAGE__->can($sub) or die;
92             *{$sub} = sub {
93 5     5   129415 my $self = shift;
94 5         10 my $what = shift;
95              
96 0         0 my $template
97             = ref($what) eq 'Template::Document' ? $what->name
98 5 100       49 : ref($what) eq 'ARRAY' ? join( ' + ', @{$what} )
    50          
    100          
99             : ref($what) eq 'SCALAR' ? '(evaluated block)'
100             : $what
101             ;
102              
103 5         25 local $depth = $depth + 1;
104 5         13 my $spacing = ' ' x $depth;
105              
106 5         18 my $start = Time::HiRes::time();
107 5 100       33 local $epoch = $epoch ? $epoch : $start;
108 5         14 my $epoch_elapsed_start = _diff_disp($epoch, $start);
109 5         24 my $start_stats = "L$depth $epoch_elapsed_start $spacing$ip $template";
110 5         12 push @totals, $start_stats;
111              
112 5         30 my $processed_data = $super->($self, $what, @_);
113              
114 5         20443 my $end = Time::HiRes::time();
115 5         36 my $epoch_elapsed_end = _diff_disp($epoch, $end);
116 5         11 my $level_elapsed = _diff_disp($start, $end);
117 5         25 my $end_stats = "L$depth $epoch_elapsed_end $level_elapsed $spacing$ip $template";
118 5         15 push @totals, $end_stats;
119              
120 5 100       20 if ( $depth > 1 ) {
121 4         112 return $processed_data;
122             }
123              
124 1         9 my $summary = join( "\n",
125             '',
128             '',
129             );
130 1         4 @totals = ();
131 1         10 return "$processed_data\n$summary\n";
132             }; # sub
133             } # for
134              
135              
136             sub _diff_disp {
137 15     15   26 my $starting_point = shift;
138 15         20 my $ending_point = shift;
139              
140 15         147 return sprintf( '%10.3f', ($ending_point - $starting_point) * 1000 );
141             }
142              
143              
144             =head1 AUTHOR
145              
146             Andy Lester, C<< >>
147              
148             =head1 BUGS
149              
150             Please report any bugs or feature requests to
151             C, or through the web interface at
152             L. I will be notified, and then you'll automatically
153             be notified of progress on your bug as I make changes.
154              
155             =head1 ACKNOWLEDGEMENTS
156              
157             Thanks to
158             Randal Schwartz,
159             Bill Moseley,
160             and to Gavin Estey for the original code.
161              
162             =head1 COPYRIGHT & LICENSE
163              
164             Copyright 2005-2013 Andy Lester.
165              
166             This program is free software; you can redistribute it and/or modify
167             it under the terms of the Artistic License v2.0.
168              
169             See http://www.perlfoundation.org/artistic_license_2_0 or the LICENSE.md
170             file that comes with the ack distribution.
171              
172             =cut
173              
174             1; # End of Template::Timer