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