blib/lib/Template/Timer.pm

filter: Statementstmt 98.0% 49 / 50 Branchbran 83.3% 10 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 95.6% 66 / 69 CC 6 SCAR 12.5
CC
6
Cov
95%
__ANON__
6.0
BEGIN
1.0
BEGIN
1.0
CRAP 3.5
↑ Summary
1 package Template::Timer;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 2 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<Template::Context>
22 that wraps the C<process()> and C<include()> 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 <!-- SUMMARY
44 L1 0.000 P page/search/display.ttml
45 L2 0.189 I element/framework/page-start.tt
46 L3 0.206 P element/framework/page-start.tt
47 L4 0.571 I element/framework/preface.tt
48 L5 0.585 P element/framework/preface.tt
49 L5 2.318 1.733 P element/framework/preface.tt
50 L4 2.332 1.761 I element/framework/preface.tt
51 L4 2.348 I element/framework/header.tt
52  
53 L4 378.117 0.439 I element/framework/content/line-item/paging.tt
54 L3 379.055 221.078 P element/framework/content/line-items.tt
55 L2 379.099 221.131 I element/framework/content/line-items.tt
56 L2 379.310 I element/atom/inplace-edit-mynotes.tt
57 L3 379.321 P element/atom/inplace-edit-mynotes.tt
58 L3 381.038 1.717 P element/atom/inplace-edit-mynotes.tt
59 L2 381.050 1.740 I element/atom/inplace-edit-mynotes.tt
60 L2 381.061 I element/framework/page-end.tt
61 L3 381.068 P element/framework/page-end.tt
62 L4 381.350 I element/framework/footer.tt
63 L5 381.361 P element/framework/footer.tt
64 L5 382.988 1.627 P element/framework/footer.tt
65 L4 383.001 1.651 I element/framework/footer.tt
66 L4 383.015 I element/framework/epilogue.tt
67 L5 383.022 P element/framework/epilogue.tt
68 L5 383.320 0.298 P element/framework/epilogue.tt
69 L4 383.331 0.316 I element/framework/epilogue.tt
70 L3 383.369 2.301 P element/framework/page-end.tt
71 L2 383.377 2.316 I element/framework/page-end.tt
72 L1 387.852 387.852 P page/search/display.ttml
73 -->
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 ▶use base qw( Template::Context );
Subroutine BEGIN: called CC=1 SCAR=0.0
81 2 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 2 our $depth = 0;
84 2 our $epoch = undef;
85 2 our @totals;
86  
87 2 foreach my $sub ( qw( process include ) ) {
88 4 my $ip = uc substr( $sub, 0, 1 ); # Include or Process?
89  
90 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
91 4 ▶ my $super = __PACKAGE__->can($sub) or die;
Branch: 1 branch
BranchTrueFalseTotal
my $super = __PACKAGE__->can($sub) or die 4 0 4
92 4 ▶ *{$sub} = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
93 5 my $self = shift;
94 5 my $what = shift;
95  
96 my $template
97 = ref($what) eq 'Template::Document' ? $what->name
98 0 ▶ : ref($what) eq 'ARRAY' ? join( ' + ', @{$what} )
Branch: 3 branches
BranchTrueFalseTotal
ref $what eq 'Template::Document' ? : 1 4 5
ref $what eq 'ARRAY' ? : 0 4 4
ref $what eq 'SCALAR' ? : 1 3 4
99 : ref($what) eq 'SCALAR' ? '(evaluated block)'
100 : $what
101 ;
102  
103 5 local $depth = $depth + 1;
104 5 my $spacing = ' ' x $depth;
105  
106 5 my $start = Time::HiRes::time();
107 5 ▶ local $epoch = $epoch ? $epoch : $start;
Branch: 1 branch
BranchTrueFalseTotal
$Template::Timer::epoch ? : 4 1 5
108 5 my $epoch_elapsed_start = _diff_disp($epoch, $start);
109 5 my $start_stats = "L$depth $epoch_elapsed_start $spacing$ip $template";
110 5 push @totals, $start_stats;
111  
112 5 my $processed_data = $super->($self, $what, @_);
113  
114 5 my $end = Time::HiRes::time();
115 5 my $epoch_elapsed_end = _diff_disp($epoch, $end);
116 5 my $level_elapsed = _diff_disp($start, $end);
117 5 my $end_stats = "L$depth $epoch_elapsed_end $level_elapsed $spacing$ip $template";
118 5 push @totals, $end_stats;
119  
120 5 ▶ if ( $depth > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Template::Timer::depth > 1) 4 1 5
121 4 return $processed_data;
122 }
123  
124 1 my $summary = join( "\n",
125 '<!-- SUMMARY',
126 @totals,
127 '-->',
128 '',
129 );
130 1 @totals = ();
131 1 return "$processed_data\n$summary\n";
132 4 }; # sub
133 } # for
134  
135  
136 15 ▶sub _diff_disp {
Subroutine _diff_disp: called CC=1 SCAR=0.0
137 15 my $starting_point = shift;
138 15 my $ending_point = shift;
139  
140 15 return sprintf( '%10.3f', ($ending_point - $starting_point) * 1000 );
141 }
142  
143  
144 =head1 AUTHOR
145  
146 Andy Lester, C<< <andy at petdance.com> >>
147  
148 =head1 BUGS
149  
150 Please report any bugs or feature requests to
151 C<bug-template-timer at rt.cpan.org>, or through the web interface at
152 L<http://rt.cpan.org>. 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 2 1; # End of Template::Timer
↑ Summary