File Coverage

blib/lib/MasonX/Profiler.pm
Criterion Covered Total %
statement 6 20 30.0
branch 0 6 0.0
condition 0 2 0.0
subroutine 2 5 40.0
pod 0 2 0.0
total 8 35 22.8


line stmt bran cond sub pod time code
1             package MasonX::Profiler;
2             $MasonX::Profiler::VERSION = '0.07';
3              
4 1     1   4655 use strict;
  1         5  
  1         23  
5 1     1   437 use Time::HiRes ();
  1         1110  
  1         375  
6              
7             =head1 NAME
8              
9             MasonX::Profiler - Mason per-component profiler
10              
11             =head1 VERSION
12              
13             This document describes version 0.07 of MasonX::Profiler.
14              
15             =head1 SYNOPSIS
16              
17             In the Mason handler:
18              
19             use MasonX::Profiler;
20             my $ah = HTML::Mason::ApacheHandler->new(
21             preamble => 'my $p = MasonX::Profiler->new($m, $r);',
22             # ...
23             );
24              
25             Note that B and B works, too.
26              
27             Alternatively, in F, before loading your C:
28              
29             PerlModule MasonX::Profiler
30             PerlSetVar MasonPreamble "my $p = MasonX::Profiler->new($m, $r);"
31              
32             Note that if you are using virtual hosts, the two lines above must be
33             inside the CVirtualHostE> block, not outside it.
34              
35             =head1 INSTALLATION
36              
37             MasonX::Profiler uses the standard perl module install process:
38              
39             cpansign -v # optional; see SIGNATURE for details
40             perl Makefile.PL
41             make # or 'nmake' on Win32
42             make test
43             make install
44              
45             =head1 DESCRIPTION
46              
47             This module prints per-component profiling information to C
48             (usually directed to the Apache error log). Its output looks like this:
49              
50             =Mason= 127.0.0.1 - /NoAuth/webrt.css BEGINS {{{
51             =Mason= 127.0.0.1 - /NoAuth/webrt.css {{{
52             =Mason= 127.0.0.1 - /Elements/Callback {{{
53             =Mason= 127.0.0.1 - /Elements/Callback }}} 0.0008
54             =Mason= 127.0.0.1 - /NoAuth/webrt.css }}} 0.0072
55             =Mason= 127.0.0.1 - /NoAuth/webrt.css }}} ENDS
56              
57             Each row contains five whitespace-separated fields: C<=Mason=>, remote IP
58             address, C<->, indented component name, and how many seconds did it take to
59             process that component, including all subcomponents called by it.
60              
61             The beginning and end of the initial request is represented by the special
62             time fields C and C.
63              
64             =cut
65              
66             my %Depth;
67              
68             sub init {
69 0     0 0   my ($class, $p, $m, $r) = @_;
70 0           $_[1] = $class->new($m, $r);
71             }
72              
73             sub new {
74 0     0 0   my ($class, $m, $r) = @_;
75              
76             my $self = {
77             start => Time::HiRes::time(),
78             uri => $r->uri,
79             tag => $m->current_comp->path,
80             ip => (
81             eval { $r->connection->get_remote_host(
82             Apache::REMOTE_NAME(), $r->per_dir_config,
83             ) } ||
84             eval { $r->get_remote_host } ||
85             eval { CGI->remote_host } ||
86             eval { $ENV{REMOTE_HOST} } ||
87 0   0       eval { $ENV{REMOTE_ADDR} } ||
88             '*'
89             ),
90             };
91              
92 0 0         return if $self->{tag} eq '/l';
93              
94             print STDERR "=Mason= $self->{ip} - $self->{uri} BEGINS {{{\n"
95 0 0         unless $Depth{$self->{ip}}{$self->{uri}}++;
96              
97 0           my $indent = ' ' x (4 * $Depth{$self->{ip}}{$self->{uri}});
98 0           printf STDERR "=Mason= $self->{ip} - $indent".
99             "$self->{tag} {{{\n";
100              
101 0           bless($self, $class);
102             }
103              
104             sub DESTROY {
105 0     0     my $self = shift;
106 0           my $indent = ' ' x (4 + 4 * --$Depth{$self->{ip}}{$self->{uri}});
107              
108             printf STDERR "=Mason= $self->{ip} - $indent".
109 0           "$self->{tag} }}} %.4f\n", (Time::HiRes::time() - $self->{start});
110              
111 0 0         return if $Depth{$self->{ip}}{$self->{uri}};
112 0           print STDERR "=Mason= $self->{ip} - $self->{uri} }}} ENDS\n";
113             }
114              
115             1;
116              
117             =head1 AUTHORS
118              
119             Best Practical Solutions, LLC
120              
121             Autrijus Tang Eautrijus@autrijus.orgE
122              
123             =head1 COPYRIGHT
124              
125             Copyright 2002, 2003, 2004 by Autrijus Tang Eautrijus@autrijus.orgE.
126              
127             This program is free software; you can redistribute it and/or
128             modify it under the same terms as Perl itself.
129              
130             See L
131              
132             =cut