How to measure time of a request with requests_futures even an exception occurs?

515 Views Asked by At

I would to measure the time of an HTTP request with python requests_futures even an exception occurs.

Although the elapsed inherited from the original requests module can be used to measure a successful request. But it can not be used to measure a failed request.

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

Is there a good way to measure the time even when an exception occurs? Thanks.

1

There are 1 best solutions below

0
On

It is interesting question. You can add timer into an send function which in HTTPAdapter. But there is a disadvantage that you will get all time records(include redirect). Btw, i try to add timer into session.send but it return an unexpected time record.

import requests
from requests.adapters import HTTPAdapter
import time
from datetime import timedelta

def new_adapter():
    adapter =  HTTPAdapter()
    def new_send(request, **kwargs):
        start = time.perf_counter()
        try:
            resp = HTTPAdapter().send(request,**kwargs)
        except Exception as e:
            raise e
        finally:
            elapsed = time.perf_counter() - start
            elapsed = timedelta(seconds=elapsed)
            print("From new_adapter {}".format(elapsed.total_seconds()))
        return resp
    adapter.send = new_send
    return adapter

with requests.Session() as s:
    s.mount('https://', new_adapter())
    s.mount('http://', new_adapter())
    for url in ["http://127.0.0.1","http://github.com"]:
        try:
            starts = time.perf_counter()
            resp = s.get(url)
            elapsed = resp.elapsed.total_seconds()
        except Exception as e:
            elapsed = None
            #raise e
        print("From rough {}".format(timedelta(seconds=time.perf_counter() - starts).total_seconds()))
        print("From elapsed {}".format(elapsed))
        print()