Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
You can time the subroutine that does the request, in this case LWP::UserAgent::request (or LWP::UserAgent::simple_request, one level further down) using Hook::LexWrap and Time::HiRes. This can be handy when there are a number of layers between the subroutine/method you call and the one that you want to time (e.g. when you're using some higher level module like WWW::Mechanize to fetch the pages.

Please note that no attempt was made to adjust the time reported to take into account the time spent in the timing code itself.

use strict; use warnings; use Time::HiRes qw(tv_interval gettimeofday); use Hook::LexWrap; use LWP::UserAgent; use Data::Dumper; my $start; my @timer_list; my $timer = {}; wrap LWP::UserAgent::request, pre => \&start_timer, post => \&stop_timer, ; ### Now any LWP calls that use the subroutine ### LWP::UserAgent::request will be timed my $agent = LWP::UserAgent->new(timeout => 30); my $site = 'http://www.perlmonks.org/'; my $response = HTTP::Request->new(GET => $site); my $page = $agent->request($response); $page = $agent->request($response); $page = $agent->request($response); $page = $agent->request($response); $page = $agent->request($response); ### After doing some LWP work, dump the results: print Dumper(\@timer_list); sub start_timer { $start = [gettimeofday]; } sub stop_timer { $timer->{interval} = tv_interval ($start, [gettimeofday]); push @timer_list, $timer; $timer = {}; };
And the output:
$VAR1 = [ { 'interval' => '8.521706' }, { 'interval' => '3.794971' }, { 'interval' => '20.934479' }, { 'interval' => '12.792284' }, { 'interval' => '4.147622' } ];

In reply to Re: Transaction time for LWP::UserAgent GET by mp
in thread Transaction time for LWP::UserAgent GET by c

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2022-05-19 05:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?