|
Development -
General
|
|
Written by Rick
|
|
Sunday, 04 May 2008 00:00 |
|
I'm not exceedingly fond of perl scripting, but this one is useful for testing the time it takes to download a web page from a site. As there is no cache system employed it provides a raw time to download the specified page and produces the results in terms of seconds.
---
#App sends http request for index page, downloads page and measures response time. #Contributors: Script is a compilation of bits and pieces gathered from cpan.org and forums.cacti.net
#use Net::HTTP; #use Getopt::Std; use Time::HiRes qw(gettimeofday); use LWP::UserAgent;
#URL to test print "\n", "Enter the URL to check (include http://): "; $uri = <>;
print "\n", "Starting HTTP Response Test.", "\n", "Downloading web page from ", $uri, "\n", "Please wait...", "\n", "\n";
# Create a user agent object $ua = LWP::UserAgent->new; $ua->agent("HTTP-Res-Test/0.1 ");
#Create time vairables my ($t0, $startTime);
#Record time prior to request $startTime = gettimeofday();
# Create a request my $req = HTTP::Request->new(GET => $uri); $req->content_type('application/x-www-form-urlencoded'); $req->content('query=libwww-perl&mode=dist');
# Pass request to the user agent and get a response back my $res = $ua->request($req);
# Check the outcome of the response if ($res->is_success) { $t0 = gettimeofday() - $startTime; printf ("Response Time: %.4f sec.", $t0), "\n"; print "\n"; if ($t0>="15.00") { print "That seems a little slow for a web page!!", "\n" ; } } else { print "Error: Server did not respond!", "\n"; }
print "\n";
---
The above code was written to execute on a Linux machine. I'm sure it could be modified for use in any perl environment.
|