Measure website load time with Python requests

cookM picture cookM · Jun 22, 2012 · Viewed 65.5k times · Source

I'm trying to build a tool for testing the delay of my internet connection, more specifically web site load times. I thought of using the python requests module for the loading part.

Problem is, it's got no built-in functionality to measure the time it took to get the full response. For this I thought I would use the timeit module.

What I'm not sure about is that if I run timeit like so:

t = timeit.Timer("requests.get('http://www.google.com')", "import requests")

I'm I really measuring the time it took the response to arrive or is it the time it takes for the request to be built, sent, received, etc? I'm guessing I could maybe disregard that excecution time since I'm testing networks with very long delays (~700ms)?

Is there a better way to do this programatically?

Answer

TJL picture TJL · Jan 9, 2014

There is such functionality in latest version of requests:

https://requests.readthedocs.io/en/latest/api/?highlight=elapsed#requests.Response.elapsed

For example:

requests.get("http://127.0.0.1").elapsed.total_seconds()