Subject: | Adding an overall timing + convenience function |
Hi Leon!
I was confused by the perldoc synopsis which gave the example:
print "Total time: " . $ua->client_response_receive_time . "\n"
Looking into it further, it appears that each of the client_request_connect_time,
client_request_transmit_time, client_response_server_time and client_response_receive_time
states are independently timed, but the example suggests it is a cumulative time.
So I've added in two new functions:
client_total_time - this is the sum of the 4 values above
get_elapsed_time - this is the total time taken for a $ua->get call. This subtract
client_total_time would be the processing overhead of using LWP
I've updated the documentation and added in some tests too. I hope you will add this to your
useful module.
Ton
Subject: | www-mechanize-timed.patch |
diff -ur WWW-Mechanize-Timed-0.42.original/lib/WWW/Mechanize/Timed.pm WWW-Mechanize-Timed-0.42/lib/WWW/Mechanize/Timed.pm
--- WWW-Mechanize-Timed-0.42.original/lib/WWW/Mechanize/Timed.pm 2006-05-05 13:52:22.000000000 +0100
+++ WWW-Mechanize-Timed-0.42/lib/WWW/Mechanize/Timed.pm 2006-05-05 13:54:14.000000000 +0100
@@ -6,6 +6,9 @@
use base qw( WWW::Mechanize );
use LWPx::TimedHTTP qw(:autoinstall);
+use Time::HiRes;
+
+my $get_elapsed_time = 0;
sub new {
my $class = shift;
@@ -14,6 +17,16 @@
return $self;
}
+sub get {
+ my $self = shift;
+ my $start = Time::HiRes::gettimeofday();
+ $_ = $self->SUPER::get(@_);
+ $get_elapsed_time = Time::HiRes::gettimeofday()-$start;
+ return $_;
+}
+
+sub get_elapsed_time { return $get_elapsed_time }
+
sub client_request_connect_time {
my $self = shift;
return $self->response->header('Client-Request-Connect-Time');
@@ -34,6 +47,13 @@
return $self->response->header('Client-Response-Receive-Time');
}
+sub client_total_time {
+ my $self = shift;
+ return $self->client_request_connect_time+$self->client_request_transmit_time+$self->client_response_server_time+$self->client_response_receive_time;
+}
+
+1;
+
__END__
=head1 NAME
@@ -45,7 +65,8 @@
use WWW::Mechanize::Timed;
my $ua = WWW::Mechanize::Timed->new();
$ua->get($url);
- print "Total time: " . $ua->client_response_receive_time . "\n";
+ print "Total time: " . $ua->client_total_time . "\n";
+ print "Get elapsed time: " . $ua->get_elapsed_time . "\n";
=head1 DESCRIPTION
@@ -84,6 +105,15 @@
Time it took to get the data back.
+=head2 client_total_time
+
+Total time taken for each of the 4 stages above.
+
+=head2 get_elapsed_time
+
+Total time taken to make the WWW::Mechanize::get request, as
+perceived by the calling program.
+
=head1 THANKS
Andy Lester for L<WWW::Mechanize>. Simon Wistow for L<LWPx::TimedHTTP>.
diff -ur WWW-Mechanize-Timed-0.42.original/t/simple.t WWW-Mechanize-Timed-0.42/t/simple.t
--- WWW-Mechanize-Timed-0.42.original/t/simple.t 2006-05-05 13:52:22.000000000 +0100
+++ WWW-Mechanize-Timed-0.42/t/simple.t 2006-05-05 13:59:43.000000000 +0100
@@ -1,16 +1,18 @@
use strict;
-use Test::More tests => 6;
+use Test::More tests => 9;
use_ok('WWW::Mechanize::Timed');
my $ua = WWW::Mechanize::Timed->new();
isa_ok($ua, 'WWW::Mechanize::Timed');
+cmp_ok($ua->get_elapsed_time, '==', 0, "Elapsed timer not started");
$ua->get("http://www.astray.com/");
-ok($ua->client_request_connect_time);
-ok($ua->client_request_transmit_time);
-ok($ua->client_response_server_time);
-ok($ua->client_response_receive_time);
-
-
+my ($a, $b, $c, $d);
+ok($a = $ua->client_request_connect_time);
+ok($b = $ua->client_request_transmit_time);
+ok($c = $ua->client_response_server_time);
+ok($d = $ua->client_response_receive_time);
+cmp_ok($ua->client_total_time, '==', $a+$b+$c+$d, "client_total_time correct");
+cmp_ok($ua->get_elapsed_time, '>', $ua->client_total_time, "get_elapsed_time > client_total_time");