| 1 | package LWPx::Profile; | ||||||||||
| 2 | 2 | $LWPx::Profile::VERSION = '0.2'; | |||||||||
| 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.2 | ||||||||||
| 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; | |||||||||
| 58 | 2 | our %timings; | |||||||||
| 59 | |||||||||||
| 60 | 3 | ▶ | sub start_profiling { | ||||||||
|
Subroutine
start_profiling: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 61 | 3 | _wrap_request_sub(); | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 3 | ▶ | sub stop_profiling { | ||||||||
|
Subroutine
stop_profiling: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 65 | 3 | *LWP::UserAgent::request = $original_lwp_ua_request; | |||||||||
| 66 | 3 | my %copy = %timings; | |||||||||
| 67 | 3 | %timings = (); | |||||||||
| 68 | |||||||||||
| 69 | 3 | return \%copy; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 3 | ▶ | sub _wrap_request_sub { | ||||||||
|
Subroutine
_wrap_request_sub: called CC=1 SCAR=0.0
| |||||||||||
| 73 | 3 | $original_lwp_ua_request = \&LWP::UserAgent::request; | |||||||||
| 74 | |||||||||||
| 75 | 6 | ▶ | *LWP::UserAgent::request = sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||
| 76 | 6 | my ($ua, $req, @args) = @_; | |||||||||
| 77 | |||||||||||
| 78 | 6 | my $start = Time::HiRes::time(); | |||||||||
| 79 | 6 | my $resp = $original_lwp_ua_request->($ua, $req, @args); | |||||||||
| 80 | 6 | my $end = Time::HiRes::time(); | |||||||||
| 81 | |||||||||||
| 82 | 6 | my $duration = $end - $start; | |||||||||
| 83 | 6 | ▶ | if (my $data = $timings{$req->as_string}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 2 | $data->{count}++; | |||||||||
| 85 | 2 | $data->{total_duration} += $duration; | |||||||||
| 86 | 2 | $data->{time_of_last_sample} = $end; | |||||||||
| 87 | |||||||||||
| 88 | 2 | ▶ | if ($duration < $data->{shortest_duration}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 2 | $data->{shortest_duration} = $duration; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 2 | ▶ | if ($duration > $data->{longest_duration}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 0 | $data->{longest_duration} = $duration | |||||||||
| 94 | } | ||||||||||
| 95 | } | ||||||||||
| 96 | else { | ||||||||||
| 97 | 4 | $timings{$req->as_string} = { | |||||||||
| 98 | count => 1, | ||||||||||
| 99 | total_duration => $duration, | ||||||||||
| 100 | first_duration => $duration, | ||||||||||
| 101 | shortest_duration => $duration, | ||||||||||
| 102 | longest_duration => $duration, | ||||||||||
| 103 | time_of_first_sample => $end, | ||||||||||
| 104 | time_of_last_sample => $end, | ||||||||||
| 105 | }; | ||||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 6 | return $resp; | |||||||||
| 109 | 3 | }; | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | =head1 TODO | ||||||||||
| 113 | |||||||||||
| 114 | =over 2 | ||||||||||
| 115 | |||||||||||
| 116 | =item * | ||||||||||
| 117 | |||||||||||
| 118 | The docs are pretty middling at the moment. | ||||||||||
| 119 | |||||||||||
| 120 | =back | ||||||||||
| 121 | |||||||||||
| 122 | =head1 AUTHORS | ||||||||||
| 123 | |||||||||||
| 124 | Chris Reinhardt | ||||||||||
| 125 | crein@cpan.org | ||||||||||
| 126 | |||||||||||
| 127 | =head1 COPYRIGHT | ||||||||||
| 128 | |||||||||||
| 129 | This program is free software; you can redistribute | ||||||||||
| 130 | it and/or modify it under the same terms as Perl itself. | ||||||||||
| 131 | |||||||||||
| 132 | The full text of the license can be found in the | ||||||||||
| 133 | LICENSE file included with this module. | ||||||||||
| 134 | |||||||||||
| 135 | =head1 SEE ALSO | ||||||||||
| 136 | |||||||||||
| 137 | L<LWP::UserAgent>, perl(1) | ||||||||||
| 138 | |||||||||||
| 139 | =cut | ||||||||||
| 140 | |||||||||||
| 141 | 2 | 1; | |||||||||
| 142 | __END__ | ||||||||||