Search code examples
pythonpython-3.xpython-asyncioaiohttp

how to get response_time and response_size while using aiohttp


Is it possible to get response time and response size for each request made using aiohttp?

The documentation seems not to have those properties anywhere.

Thanks


Solution

  • One possibility might be:

    • measure point in time before request
    • measure point in time after request
    • the difference is the response time
    • with 'response.text()' you get the response and can determine the length with 'len()'

    A small self-contained example could look like this:

    import time
    import asyncio
    from aiohttp import ClientSession
    
    
    async def fetch(session, url):
        start = time.time()
        async with session.get(url) as response:
            result = await response.text()
            end = time.time()
            print(url, ": ", end - start, "response length:", len(result))
            return result
    
    
    async def crawl(urls: set):
        async with ClientSession() as session:
            tasks = []
            for url in urls:
                tasks.append(
                    fetch(session, url)
                )
            await asyncio.gather(*tasks)
    
    
    if __name__ == "__main__":
        urlSet = {"https://www.software7.biz/tst/number.php",
                  "https://www.software7.biz/tst/number1.php",
                  "https://www.software7.biz"}
        asyncio.run(crawl(urlSet))
    

    Test

    The two endpoints number.php and number1.php have a delay on server side of 3 respective 1 second and are returning a two digit number each.

    The output in the debug console looks like this then:

    https://www.software7.biz :  0.16438698768615723 response length: 4431
    https://www.software7.biz/tst/number1.php :  1.249755859375 response length: 2
    https://www.software7.biz/tst/number.php :  3.214473009109497 response length: 2