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

后端 未结 2 1262
遥遥无期
遥遥无期 2020-12-29 01:18

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

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

2条回答
  •  灰色年华
    2020-12-29 01:58

    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

提交回复
热议问题