<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Plack::Middleware::Debug::LWP;
$Plack::Middleware::Debug::LWP::VERSION = '0.3';
use strict;
use warnings;

use LWPx::Profile;
use parent qw(Plack::Middleware::Debug::Base);

=head1 NAME

Plack::Middleware::Debug::LWP - LWP Profiling Panel

=head1 VERSION

version 0.3

=head1 SYNOPSIS

	enable 'Debug::LWP';

=head1 DESCRIPTION

This module provides a panel for the L&lt;Plack::Middleware::Debug&gt; that gives
profiling information for L&lt;LWP::UserAgent&gt; requests.

=cut


 
my $lwp_template = __PACKAGE__-&gt;build_template(&lt;&lt;'ENDOFIT');
&lt;table&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;Request&lt;/th&gt;
            &lt;th&gt;Timing&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
% my $i;
% while (@{$_[0]-&gt;{list}}) {
% my($key, $value) = splice(@{$_[0]-&gt;{list}}, 0, 2);
            &lt;tr class="&lt;%= ++$i % 2 ? 'plDebugOdd' : 'plDebugEven' %&gt;"&gt;
                &lt;td&gt;&lt;pre&gt;&lt;%= $key %&gt;&lt;/pre&gt;&lt;/td&gt;
                &lt;td&gt;&lt;%= $value %&gt;&lt;/td&gt;
            &lt;/tr&gt;
% }
    &lt;/tbody&gt;
&lt;/table&gt;
ENDOFIT

 
sub run {
	my($self, $env, $panel) = @_;
	
	LWPx::Profile::start_profiling();
	
	return sub {
		my $res = shift;
		
		my $profile = LWPx::Profile::stop_profiling();
		
		my @lines;
		my ($time, $requests) = (0, 0);
		
		for my $req (sort {
			$profile-&gt;{$a}-&gt;{time_of_first_sample} &lt;=&gt; $profile-&gt;{$b}-&gt;{time_of_first_sample}
		} keys %$profile) {
			my $stats = $profile-&gt;{$req};
			my $summary = sprintf("%.5f/%d (%.5f avg)", $stats-&gt;{total_duration}, $stats-&gt;{count}, $stats-&gt;{total_duration} / $stats-&gt;{count});
			push(@lines, $req, $summary);
			$requests += $stats-&gt;{count};
			$time     += $stats-&gt;{total_duration};
		}
		
		my $summary = sprintf("%d requests / %.2f seconds", $requests, $time);
		
		$panel-&gt;nav_title("LWP Requests");
		$panel-&gt;title("LWP Requests ($summary)");
		$panel-&gt;nav_subtitle($summary);

		$panel-&gt;content(
			$self-&gt;render($lwp_template, { list =&gt; \@lines })
		);
	};
}



=head1 TODO

=over 2

=item *

The docs are pretty middling at the moment.

=back

=head1 AUTHORS

    Chris Reinhardt
    crein@cpan.org

    Mark Ng
    cpan@markng.co.uk   
    
=head1 COPYRIGHT

This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the
LICENSE file included with this module.

=head1 SEE ALSO

L&lt;Plack::Middleware::Debug&gt;, L&lt;LWP::UserAgent&gt;, L&lt;LWPx::Profile&gt;, perl(1)

=cut

1;
__END__
</pre></body></html>