File Coverage

blib/lib/Plack/Middleware/Debug/LWP.pm
Criterion Covered Total %
statement 12 31 38.7
branch n/a
condition n/a
subroutine 4 6 66.6
pod 1 1 100.0
total 17 38 44.7


line stmt bran cond sub pod time code
1             package Plack::Middleware::Debug::LWP;
2             $Plack::Middleware::Debug::LWP::VERSION = '0.3';
3 1     1   47235 use strict;
  1         2  
  1         26  
4 1     1   5 use warnings;
  1         1  
  1         21  
5              
6 1     1   286 use LWPx::Profile;
  1         35706  
  1         28  
7 1     1   7 use parent qw(Plack::Middleware::Debug::Base);
  1         2  
  1         5  
8              
9             =head1 NAME
10              
11             Plack::Middleware::Debug::LWP - LWP Profiling Panel
12              
13             =head1 VERSION
14              
15             version 0.3
16              
17             =head1 SYNOPSIS
18              
19             enable 'Debug::LWP';
20              
21             =head1 DESCRIPTION
22              
23             This module provides a panel for the L that gives
24             profiling information for L requests.
25              
26             =cut
27              
28              
29            
30             my $lwp_template = __PACKAGE__->build_template(<<'ENDOFIT');
31            
32            
33            
34             Request
35             Timing
36            
37            
38            
39             % my $i;
40             % while (@{$_[0]->{list}}) {
41             % my($key, $value) = splice(@{$_[0]->{list}}, 0, 2);
42            
43            
<%= $key %>
44             <%= $value %>
45            
46             % }
47            
48            
49             ENDOFIT
50              
51            
52             sub run {
53 0     0 1   my($self, $env, $panel) = @_;
54            
55 0           LWPx::Profile::start_profiling();
56            
57             return sub {
58 0     0     my $res = shift;
59            
60 0           my $profile = LWPx::Profile::stop_profiling();
61            
62 0           my @lines;
63 0           my ($time, $requests);
64            
65 0           for my $req (sort {
66             $profile->{$a}->{time_of_first_sample} <=> $profile->{$b}->{time_of_first_sample}
67 0           } keys %$profile) {
68 0           my $stats = $profile->{$req};
69 0           my $summary = sprintf("%.5f/%d (%.5f avg)", $stats->{total_duration}, $stats->{count}, $stats->{total_duration} / $stats->{count});
70 0           push(@lines, $req, $summary);
71 0           $requests += $stats->{count};
72 0           $time += $stats->{total_duration};
73             }
74            
75 0           my $summary = sprintf("%d requests / %.2f seconds", $requests, $time);
76            
77 0           $panel->nav_title("LWP Requests");
78 0           $panel->title("LWP Requests ($summary)");
79 0           $panel->nav_subtitle($summary);
80              
81 0           $panel->content(
82             $self->render($lwp_template, { list => \@lines })
83             );
84 0           };
85             }
86              
87              
88              
89             =head1 TODO
90              
91             =over 2
92              
93             =item *
94              
95             The docs are pretty middling at the moment.
96              
97             =back
98              
99             =head1 AUTHORS
100              
101             Chris Reinhardt
102             crein@cpan.org
103              
104             Mark Ng
105             cpan@markng.co.uk
106            
107             =head1 COPYRIGHT
108              
109             This program is free software; you can redistribute
110             it and/or modify it under the same terms as Perl itself.
111              
112             The full text of the license can be found in the
113             LICENSE file included with this module.
114              
115             =head1 SEE ALSO
116              
117             L, L, L, perl(1)
118              
119             =cut
120              
121             1;
122             __END__