57

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?

3 Answers 3

194

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()
4
  • 30
    to get the response time in seconds: requests.get("http://127.0.0.1").elapsed.total_seconds()
    – Moe
    Commented Apr 4, 2014 at 13:51
  • 3
    To add to Micael Osl's comment: total_seconds() is a decimal number which seems to have microsecond precision.
    – Luc
    Commented May 13, 2016 at 21:51
  • 10
    Do not use this for profiling and optimizing your code on the client side. It only measures the server's response time (which was the OPs question). The amount of time elapsed between sending the request and the arrival of the response (as a timedelta). This property specifically measures the time taken between sending the first byte of the request and finishing parsing the headers. It is therefore unaffected by consuming the response content or the value of the stream keyword argument. - the docs.
    – ChaimG
    Commented Jun 29, 2016 at 5:36
  • 3
    @GvS what about Python 3 and urllib3?
    – Heinz
    Commented Oct 16, 2017 at 19:10
35

As for your question, it should be the total time for

  1. time to create the request object
  2. Send request
  3. Receive response
  4. Parse response (See comment from Thomas Orozco )

Other ways to measure a single request load time is to use urllib:

nf = urllib.urlopen(url)
start = time.time()
page = nf.read()
end = time.time()
nf.close()
# end - start gives you the page load time
11
  • 7
    + 4. parse the HTTP response Commented Jun 22, 2012 at 16:06
  • 1
    That looks really nice, but looking at one of the examples i see 1. start_timer = time.time() 2. Open Browser + Read Response 3. latency = time.time() - start_timer Would that be kind of the same problem?
    – cookM
    Commented Jun 22, 2012 at 16:19
  • @cookM: I did not see it as problem but a real time experience of what the request latency will be. In fact it averages over many requests which will be closer to a realistic time.
    – pyfunc
    Commented Jun 22, 2012 at 16:23
  • 1
    #cookM: The wiki has more details on profiling load limes: code.google.com/p/multi-mechanize/wiki/AdvancedScripts
    – pyfunc
    Commented Jun 22, 2012 at 16:24
  • @pyfunc Just saw your edit, I think that snippet is just what I was looking for. I'm not that familiar with urllib but I'm guessing that when I issue nf.read() what I'm doing is sending the request and getting it back right?
    – cookM
    Commented Jun 22, 2012 at 16:25
2

response.elapsed returns a timedelta object with the time elapsed from sending the request to the arrival of the response. It is often used to stop the connection after a certain point of time is elapsed.

# import requests module 
import requests 
  
# Making a get request 
response = requests.get('http://stackoverflow.com/') 
  
# print response 
print(response) 
  
# print elapsed time 
print(response.elapsed)

output:

<Response [200]>
0:00:00.343720

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.