1 package Template::Timer;
8 Template::Timer - Rudimentary profiling for Template Toolkit
16 our $VERSION = '1.00';
20 Template::Timer provides inline timings of the template processing
21 througout your code. It's an overridden version of L<Template::Context>
22 that wraps the C<process()> and C<include()> methods.
24 Using Template::Timer is simple.
28 my %config = ( # Whatever your config is
29 INCLUDE_PATH => '/my/template/path',
30 COMPILE_EXT => '.ttc',
31 COMPILE_DIR => '/tmp/tt',
34 if ( $development_mode ) {
35 $config{ CONTEXT } = Template::Timer->new( %config );
38 my $template = Template->new( \%config );
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.
43 <!-- TIMER START: L1 process mainmenu/mainmenu.ttml -->
44 <!-- TIMER START: L2 include mainmenu/cssindex.tt -->
45 <!-- TIMER START: L3 process mainmenu/cssindex.tt -->
46 <!-- TIMER END: L3 process mainmenu/cssindex.tt (17.279 ms) -->
47 <!-- TIMER END: L2 include mainmenu/cssindex.tt (17.401 ms) -->
51 <!-- TIMER END: L3 process mainmenu/footer.tt (3.016 ms) -->
52 <!-- TIMER END: L2 include mainmenu/footer.tt (3.104 ms) -->
53 <!-- TIMER END: L1 process mainmenu/mainmenu.ttml (400.409 ms) -->
55 Note that since INCLUDE is a wrapper around PROCESS, calls to INCLUDEs
56 will be doubled up, and slightly longer than the PROCESS call.
60 use base qw( Template::Context );
67 foreach my $sub ( qw( process include ) ) {
69 my $super = __PACKAGE__->can("SUPER::$sub") or die;
76 ? join( ' + ', @{$what} )
83 my $epoch_elapsed_start;
84 my $epoch_elapsed_end;
85 my $now = [Time::HiRes::gettimeofday];
86 my $start = [@{$now}];
88 local $epoch = $epoch ? $epoch : [@{$now}];
89 local $depth = $depth + 1;
91 $epoch_elapsed_start = _diff_disp($epoch);
92 $processed_data = $super->($self, $what, @_);
93 $epoch_elapsed_end = _diff_disp($epoch);
95 my $spacing = ' ' x $level;
96 my $level_elapsed = _diff_disp($start);
97 my $ip = uc substr( $sub, 0, 1 );
98 my $start_stats = "L$level $epoch_elapsed_start $spacing$ip $template";
99 my $end_stats = "L$level $epoch_elapsed_end $level_elapsed $spacing$ip $template";
100 @totals = ( $start_stats, @totals, $end_stats );
102 return $processed_data;
105 my $summary = join( "\n",
112 return "$processed_data\n$summary\n";
118 my $starting_point = shift;
120 return sprintf( '%7.3f', Time::HiRes::tv_interval($starting_point) * 1000 );
126 Andy Lester, C<< <andy at petdance.com> >>
130 Please report any bugs or feature requests to
131 C<bug-template-timer at rt.cpan.org>, or through the web interface at
132 L<http://rt.cpan.org>. I will be notified, and then you'll automatically
133 be notified of progress on your bug as I make changes.
135 =head1 ACKNOWLEDGEMENTS
140 and to Gavin Estey for the original code.
142 =head1 COPYRIGHT & LICENSE
144 This library is free software; you can redistribute it and/or modify
145 it under the terms of either the GNU Public License v3, or the Artistic
148 * http://www.gnu.org/copyleft/gpl.html
150 * http://www.opensource.org/licenses/artistic-license-2.0.php
154 1; # End of Template::Timer