File: Timer.pm

package info (click to toggle)
libtemplate-timer-perl 1.00-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch, wheezy
  • size: 84 kB
  • ctags: 3
  • sloc: perl: 94; makefile: 2
file content (154 lines) | stat: -rw-r--r-- 4,144 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package Template::Timer;

use warnings;
use strict;

=head1 NAME

Template::Timer - Rudimentary profiling for Template Toolkit

=head1 VERSION

Version 1.00

=cut

our $VERSION = '1.00';

=head1 SYNOPSIS

Template::Timer provides inline timings of the template processing
througout your code.  It's an overridden version of L<Template::Context>
that wraps the C<process()> and C<include()> methods.

Using Template::Timer is simple.

    use Template::Timer;

    my %config = ( # Whatever your config is
        INCLUDE_PATH    => '/my/template/path',
        COMPILE_EXT     => '.ttc',
        COMPILE_DIR     => '/tmp/tt',
    );

    if ( $development_mode ) {
        $config{ CONTEXT } = Template::Timer->new( %config );
    }

    my $template = Template->new( \%config );

Now when you process templates, HTML comments will get embedded in your
output, which you can easily grep for.  The nesting level is also shown.

    <!-- TIMER START: L1 process mainmenu/mainmenu.ttml -->
    <!-- TIMER START: L2 include mainmenu/cssindex.tt -->
    <!-- TIMER START: L3 process mainmenu/cssindex.tt -->
    <!-- TIMER END:   L3 process mainmenu/cssindex.tt (17.279 ms) -->
    <!-- TIMER END:   L2 include mainmenu/cssindex.tt (17.401 ms) -->

    ....

    <!-- TIMER END:   L3 process mainmenu/footer.tt (3.016 ms) -->
    <!-- TIMER END:   L2 include mainmenu/footer.tt (3.104 ms) -->
    <!-- TIMER END:   L1 process mainmenu/mainmenu.ttml (400.409 ms) -->

Note that since INCLUDE is a wrapper around PROCESS, calls to INCLUDEs
will be doubled up, and slightly longer than the PROCESS call.

=cut

use base qw( Template::Context );
use Time::HiRes ();

our $depth = 0;
our $epoch = undef;
our @totals;

foreach my $sub ( qw( process include ) ) {
    no strict;
    my $super = __PACKAGE__->can("SUPER::$sub") or die;
    *{$sub} = sub {
        my $self = shift;
        my $what = shift;

        my $template =
            ref($what) eq 'ARRAY'
                ? join( ' + ', @{$what} )
                : ref($what)
                    ? $what->name
                    : $what;

        my $level;
        my $processed_data;
        my $epoch_elapsed_start;
        my $epoch_elapsed_end;
        my $now   = [Time::HiRes::gettimeofday];
        my $start = [@{$now}];
        DOIT: {
            local $epoch = $epoch ? $epoch : [@{$now}];
            local $depth = $depth + 1;
            $level = $depth;
            $epoch_elapsed_start = _diff_disp($epoch);
            $processed_data = $super->($self, $what, @_);
            $epoch_elapsed_end = _diff_disp($epoch);
        }
        my $spacing = ' ' x $level;
        my $level_elapsed = _diff_disp($start);
        my $ip = uc substr( $sub, 0, 1 );
        my $start_stats = "L$level $epoch_elapsed_start         $spacing$ip $template";
        my $end_stats =   "L$level $epoch_elapsed_end $level_elapsed $spacing$ip $template";
        @totals = ( $start_stats, @totals, $end_stats );
        if ( $level > 1 ) {
            return $processed_data;
        }

        my $summary = join( "\n",
            '<!-- SUMMARY',
            @totals,
            '-->',
            '',
        );
        @totals = ();
        return "$processed_data\n$summary\n";
    }; # sub
} # for


sub _diff_disp {
    my $starting_point = shift;

    return sprintf( '%7.3f', Time::HiRes::tv_interval($starting_point) * 1000 );
}


=head1 AUTHOR

Andy Lester, C<< <andy at petdance.com> >>

=head1 BUGS

Please report any bugs or feature requests to
C<bug-template-timer at rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.  I will be notified, and then you'll automatically
be notified of progress on your bug as I make changes.

=head1 ACKNOWLEDGEMENTS

Thanks to
Randal Schwartz,
Bill Moseley,
and to Gavin Estey for the original code.

=head1 COPYRIGHT & LICENSE

This library is free software; you can redistribute it and/or modify
it under the terms of either the GNU Public License v3, or the Artistic
License 2.0.

    * http://www.gnu.org/copyleft/gpl.html

    * http://www.opensource.org/licenses/artistic-license-2.0.php

=cut

1; # End of Template::Timer