cybertextron cybertextron - 6 months ago 30
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

is to return how long took to load the page in

import webbrowser

def perf_measure(url=""):


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

url = ""

How can I accomplish that? I just need the value, in seconds, like: 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('')
start_time = time()
output =
end_time = time()

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

print(round(end_time-start_time, 3))


0.865000009537 # Without Round
0.865          # With Round
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download