Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure server response time for Python requests POST-request

I create requests POST-requests like this, where I specify timeout threshold:

response = requests.post(url, data=post_fields, timeout=timeout)

However, to determine a "good" threshold value, I would like to benchmark the server response time in advance.

How do I compute the minimum and maximum response times for the server?

like image 621
Shuzheng Avatar asked Apr 06 '17 10:04

Shuzheng


People also ask

How is server response time calculated?

To measure server response time, you can use a few potential metrics. Most often, admins measure server response time with a term called Time to First Byte (TTFB), which represents the time it takes in milliseconds for a browser to receive the first byte of the response from a server.

Does Python requests wait for response?

It will wait until the response arrives before the rest of your program will execute. If you want to be able to do other things, you will probably want to look at the asyncio or multiprocessing modules. Chad S. Chad S.

What is the default timeout for Python requests?

By default, requests do not have a timeout unless you explicitly specify one.


1 Answers

The Response object returned by requests.post() (and requests.get() etc.) has a property called elapsed, which provides the time delta between the Request was sent and the Response was received. To get the delta in seconds, use the total_seconds() method:

response = requests.post(url, data=post_fields, timeout=timeout) print(response.elapsed.total_seconds()) 

Note that requests.post() is a synchronous operation, which means that it blocks until the Response is received.

like image 190
Nicolas Lykke Iversen Avatar answered Oct 02 '22 11:10

Nicolas Lykke Iversen