cybertextron cybertextron - 1 month ago 7
Python Question

measuring the time to load a page - Python

I'm using webbrowser, so I can open a html to an performance test I'm currently doing.
This small piece of code is the begin of the automation. The goal of the function

perf_measure
is to return how long took to load the page in
url
entirely.

import webbrowser

def perf_measure(url=""):

try:
webbrowser.open(url)

except webbrowser.Error, e:
print "It couldn't open the url: ", url

url = "www.google.com"
open_browser(url)


How can I accomplish that? I just need the value, in seconds, like:

www.google.com Total time to load page in (secs): 2.641

Answer Source

Do you need to use the web browser? As in do you need to view the result?

Otherwise you could do this.

import urllib2
from time import time

stream = urllib2.urlopen('http://www.rarlab.com/rar/winrar-x64-420.exe')
start_time = time()
output = stream.read()
end_time = time()
stream.close()
print(end_time-start_time)

If you want a more human-readable result you can use round.

print(round(end_time-start_time, 3))

Output

0.865000009537 # Without Round
0.865          # With Round