| 1 | package LWPx::Profile; | ||||||||||
| 2 | 2 | $LWPx::Profile::VERSION = '0.1'; | |||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | no warnings 'redefine'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Time::HiRes; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | LWPx::Profile - Basic Timing of HTTP Requests | ||||||||||
| 13 | |||||||||||
| 14 | =head1 VERSION | ||||||||||
| 15 | |||||||||||
| 16 | version 0.1 | ||||||||||
| 17 | |||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||
| 19 | |||||||||||
| 20 | use LWP::UserAgent; | ||||||||||
| 21 | use LWPx::Profile; | ||||||||||
| 22 | |||||||||||
| 23 | my $ua = LWP::UserAgent; | ||||||||||
| 24 | |||||||||||
| 25 | LWPx::Profile::start_profile(); | ||||||||||
| 26 | foreach my $url (@sites) { | ||||||||||
| 27 | $ua->get($url); | ||||||||||
| 28 | } | ||||||||||
| 29 | my $results = LWPx::Profile::stop_profile; | ||||||||||
| 30 | |||||||||||
| 31 | |||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||
| 33 | |||||||||||
| 34 | This module provides a basic profiling framework for looking at how long | ||||||||||
| 35 | HTTP requests with LWP took to complete. The data structure returned by | ||||||||||
| 36 | C<stop_profile> is a hashref of request-string => stats pairs. For example: | ||||||||||
| 37 | |||||||||||
| 38 | |||||||||||
| 39 | 'GET http://www.google.com/ | ||||||||||
| 40 | User-Agent: libwww-perl/6.08 | ||||||||||
| 41 | |||||||||||
| 42 | ' => { | ||||||||||
| 43 | 'shortest_duration' => '0.111438989639282', | ||||||||||
| 44 | 'time_of_first_sample' => '1424211134.8376', | ||||||||||
| 45 | 'longest_duration' => '0.202037811279297', | ||||||||||
| 46 | 'count' => 3, | ||||||||||
| 47 | 'total_duration' => '0.436195850372314', | ||||||||||
| 48 | 'time_of_last_sample' => '1424211135.07221', | ||||||||||
| 49 | 'first_duration' => '0.202037811279297' | ||||||||||
| 50 | }; | ||||||||||
| 51 | |||||||||||
| 52 | |||||||||||
| 53 | In this example, there have been three requests for http://www.google.com/. | ||||||||||
| 54 | |||||||||||
| 55 | =cut | ||||||||||
| 56 | |||||||||||
| 57 | 2 | our $original_lwp_ua_request = \&LWP::UserAgent::request; | |||||||||
| 58 | 2 | our %timings; | |||||||||
| 59 | |||||||||||
| 60 | sub start_profiling { | ||||||||||
| 61 | 3 | ▶ | _wrap_request_sub(); | ||||||||
|
Subroutine
start_profiling: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | sub stop_profiling { | ||||||||||
| 65 | 3 | ▶ | *LWP::UserAgent::request = $original_lwp_ua_request; | ||||||||
|
Subroutine
stop_profiling: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 66 | 3 | my %copy = %timings; | |||||||||
| 67 | 3 | %timings = (); | |||||||||
| 68 | |||||||||||
| 69 | 3 | return \%copy; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | sub _wrap_request_sub { | ||||||||||
| 73 | *LWP::UserAgent::request = sub { | ||||||||||
| 74 | 7 | ▶ | my ($ua, $req, @args) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||
| 75 | |||||||||||
| 76 | 7 | my $start = Time::HiRes::time(); | |||||||||
| 77 | 7 | my $resp = $original_lwp_ua_request->($ua, $req, @args); | |||||||||
| 78 | 7 | my $end = Time::HiRes::time(); | |||||||||
| 79 | |||||||||||
| 80 | 7 | my $duration = $end - $start; | |||||||||
| 81 | 7 | ▶ | if (my $data = $timings{$req->as_string}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 2 | $data->{count}++; | |||||||||
| 83 | 2 | $data->{total_duration} += $duration; | |||||||||
| 84 | 2 | $data->{time_of_last_sample} = $end; | |||||||||
| 85 | |||||||||||
| 86 | 2 | ▶ | if ($duration < $data->{shortest_duration}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 1 | $data->{shortest_duration} = $duration; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 2 | ▶ | if ($duration > $data->{longest_duration}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 1 | $data->{longest_duration} = $duration | |||||||||
| 92 | } | ||||||||||
| 93 | } | ||||||||||
| 94 | else { | ||||||||||
| 95 | 5 | $timings{$req->as_string} = { | |||||||||
| 96 | count => 1, | ||||||||||
| 97 | total_duration => $duration, | ||||||||||
| 98 | first_duration => $duration, | ||||||||||
| 99 | shortest_duration => $duration, | ||||||||||
| 100 | longest_duration => $duration, | ||||||||||
| 101 | time_of_first_sample => $end, | ||||||||||
| 102 | time_of_last_sample => $end, | ||||||||||
| 103 | }; | ||||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 7 | return $resp; | |||||||||
| 107 | 3 | ▶ | }; | ||||||||
|
Subroutine
_wrap_request_sub: called CC=1 SCAR=0.0
| |||||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | =head1 TODO | ||||||||||
| 111 | |||||||||||
| 112 | =over 2 | ||||||||||
| 113 | |||||||||||
| 114 | =item * | ||||||||||
| 115 | |||||||||||
| 116 | The docs are pretty middling at the moment. | ||||||||||
| 117 | |||||||||||
| 118 | =back | ||||||||||
| 119 | |||||||||||
| 120 | =head1 AUTHORS | ||||||||||
| 121 | |||||||||||
| 122 | Chris Reinhardt | ||||||||||
| 123 | crein@cpan.org | ||||||||||
| 124 | |||||||||||
| 125 | =head1 COPYRIGHT | ||||||||||
| 126 | |||||||||||
| 127 | This program is free software; you can redistribute | ||||||||||
| 128 | it and/or modify it under the same terms as Perl itself. | ||||||||||
| 129 | |||||||||||
| 130 | The full text of the license can be found in the | ||||||||||
| 131 | LICENSE file included with this module. | ||||||||||
| 132 | |||||||||||
| 133 | =head1 SEE ALSO | ||||||||||
| 134 | |||||||||||
| 135 | L<LWP::UserAgent>, perl(1) | ||||||||||
| 136 | |||||||||||
| 137 | =cut | ||||||||||
| 138 | |||||||||||
| 139 | 2 | 1; | |||||||||
| 140 | __END__ | ||||||||||