Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to calculate a webpage load time using perl? 1

Status
Not open for further replies.

rcsen

Programmer
Jan 11, 2001
51
US
Hi ,
I am new to perl. Could you please suggest me,how to find the the total time for a particular webpage to load?
Any help is appreciated. Thanks.

Sincerely,
Senthil
 
Hello rcsen,
do you mean from the browser's perspective or from the server's perspective? Is it a CGI generated page? ????

This is easily done if it is a CGI generated page. Just grab the time, using the time function, at the beginning of the code and again at the end and substract. That will give you the number of seconds the program took to run.

'hope this helps....

#!/usr/local/bin/perl
$started = time;

# program stuff here

$ended = time;
$seconds = $ended - started;


keep the rudder amid ship and beware the odd typo
 
put probably not the time it took the browser to download/render the page. adam@aauser.com
 
Hi goBoating,
What I intended to do was, write a perl script such that if I give any url as a parameter, the code should be able to calculate the time to load. I should not add anything to the existing code. Could give me some idea for this?

Thanks
RCSEN
 
hi senthil!

I think you need to try using some modules(LWP)
thendal :)
 
well, you could get the filesize and size of all dependencies with perl from a url... then calculate the average time it would take at different speeds based on the sizes...

or you could actually have your perl program download these things, but, it would give the speed of the server that your site is on.... and, it might be a little slower depending on processor speeds and what not... adam@aauser.com
 
Thanks thendal and luciddream. Let me continue to try about this. Please pass on any information you come across regarding this to me.


Senthil

 
I think thendal might be barking up the right tree. You can use LWP to retrieve a page, but, you have to get the graphics separately. I have not run this, but, it might be close to what you are looking for......


#-- START CODE --#
#!/usr/local/bin/perl
use LWP::Simple;
$url = '$started = time;

# this will get the HTML text
$content = get($url);

# parse the IMG SRC tags from the HTML
while ($content =~ /<IMG SRC=&quot;(.*?)&quot;/gis)
{
# retrieve each IMG SRC
get($1); # I have not tried this, but, I don't see
# why it would not work
}
$stopped = time;
$elapsed = $stopped - $started;
#--- END CODE --#

'hope this helps....


keep the rudder amid ship and beware the odd typo
 
One possibility: If the images' locations are written in relative instead of absolute directory listings, you'll have to strip the prefix directory out of the original url and add that onto it. you'll also have to check first to see whether it's an absolute or relative listing. not too much trouble, though.
&quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Hi goBoating,
Thanks for the code but still I have a problem.
The elapsed time calculated displays &quot;0&quot;. How could I proceed with it?
 
This works just fine on my box....

#---- code ----#
#!/usr/local/bin/perl
use LWP::Simple;
$url = '$started = time;
print &quot;Started: $started\n&quot;;

# this will get the HTML text
$content = get($url);
print &quot;Got content\n&quot;;

# parse the IMG SRC tags from the HTML
while ($content =~ /<IMG.*?SRC=&quot;(.*?)&quot;/gis)
{
# retrieve each IMG SRC
print &quot;Getting $1\n&quot;;
get($1); # I have not tried this, but, I don't see
# why it would not work
}
$stopped = time;
print &quot;Stopped: $stopped\n&quot;;
$elapsed = $stopped - $started;
print &quot;Elapsed: $elapsed\n&quot;;

#---- END CODE ----#

That code produces this output....

#---- OUTPUT ----#
Started: 980879788
Got content
Getting Getting Getting Getting Getting Getting Getting Getting Getting Getting Getting Getting Getting Getting Getting Getting Getting Getting Getting Stopped: 980879798
Elapsed: 10
#---- End OUTPUT ----#


That is a funny site, by the way. Take a look at the John West Bear commercial, if you haven't already seen it.

' hope this helps..



keep the rudder amid ship and beware the odd typo
 
Hi goBoating,
Thankyou verymuch. It works fine. Could you say me what is nonleap seconds mean? For the &quot;time&quot; function, they had given as nonleap seconds from Jan 1,1970.

Thanks again.
Sincerely,
RCSEN.
 
In leap years, there are 366 days (February 30 exists).
I believe the time function has neglected to add a day for leap years. It assumes 365 days in all years.


keep the rudder amid ship and beware the odd typo
 
Sorry for disturbing again.
The elapsed time is returned in seconds. Is there any function to find the same in milliseconds? Because, all my pages loads in almost 1 or 2 seconds.
Can I convert the &quot;time&quot; function to calculate upto milliseconds? The time function in JAVA gives the output in milliseconds.

Thanks
Sincerely,
RCSEN
 
Sorry, the time function returns seconds. I guess you could run a series of loops on each page. Maybe get each one 10 or 20 or 100 times and use the aggregate value. Doing this to your server is reasonable, but, I would [red]not[/red] do this to someone else's server. It would be a real annoyance.

good luck...


keep the rudder amid ship and beware the odd typo
 
If you're interested in time in increments less than a second, the Perl Cookbook recipe 3.9 High-Resolution Timers suggests downloading the Time::HiRes module from CPAN - that is, if your system supports both the &quot;syscall&quot; in Perl as well as a system call like gettimeofday(2). I just did &quot;man gettimeofday&quot; and a manpage popped up - I guess I have gettimeofday - I'm on a Redhat 6.1 Linux system.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
oops.... X-) I'v been missing appointments for years..... finally I know why :)


keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top