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

折月煮酒 提交于 2020-01-21 03:56:04

问题


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?


回答1:


The Response object as returned by requests.post() has a property called elapsed, which give 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())

It should be mentioned that requests.post() is a synchronous operation, which means that it "blocks" until the response is received.




回答2:


It depends on whether you can hit the server with a lot of test requests, or whether you need to wait for real requests to occur.

If you need real request data, then you'd need to wrap the call to determine the time of each request:

start = time.clock()
response = requests.post(url, data=post_fields, timeout=timeout)
request_time = time.clock() - start
self.logger.info("Request completed in {0:.0f}ms".format(request_time)
#store request_time in persistent data store

You'd need somewhere to store the results of each request over a period of time (file, database, etc). Then you can just calculate the stats of the response times.

If you have a test server available, you could benchmark the response without python using something like apachebench and sending test data for each request:

https://gist.github.com/kelvinn/6a1c51b8976acf25bd78



来源:https://stackoverflow.com/questions/43252542/how-to-measure-server-response-time-for-python-requests-post-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!