[aiohttp] What is the right way to measure request latency?

1,626 views
Skip to first unread message

ma...@influxdb.com

unread,
May 17, 2016, 1:08:51 PM5/17/16
to aio-libs
I'm using the aiohttp HTTP client, and I want to measure latency of my outgoing requests, in order to gauge whether a particular request has different latency under different conditions.

Is it sufficient to do something like the following?

start = time.perf_counter()
async with client.post(url, params=params, data=data) as resp:
  delta= time.perf_counter() - start
  print("Status: {}, Latency: {}".format(resp.status, delta))

I think the above approach could give misleading results depending on other active coroutines when the request is active.

Would it be better to subclass the ClientRequest or ClientResponse classes for this? I've looked through that code a bit and it isn't very clear to me what I would need to change to track request latency.

Pierre-Alexandre Gagné

unread,
Jul 29, 2016, 1:15:12 PM7/29/16
to aio-libs, ma...@influxdb.com
I am also looking for the same thing.

Andrew Svetlov

unread,
Jul 29, 2016, 1:20:28 PM7/29/16
to Pierre-Alexandre Gagné, aio-libs, ma...@influxdb.com
The proposed approach is perfectly fine.

If client is overloaded by other activities time measure shows misleading results regardless actual point for insertion `time.perf_counter()` code.

`ClientRequest`/`ClientResponse` overloading is really tricky thing, please avoid the technique if possible.

--
You received this message because you are subscribed to the Google Groups "aio-libs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aio-libs+u...@googlegroups.com.
To post to this group, send email to aio-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/aio-libs/5d59e138-89b5-45e6-a2c3-170683fb6efc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Thanks,
Andrew Svetlov

roberto...@gmail.com

unread,
Aug 25, 2017, 4:11:54 AM8/25/17
to aio-libs, pierrealex...@hotmail.com, ma...@influxdb.com
Hi Andrew,
I tried this approach and it works fine with single requests, but I have problems with many requests.

I published a file with the thing I am trying to achieve in a GitHub Gist, here: https://gist.github.com/RobertoPrevato/0af45aa686b256eb3cf11525a477d8f5#file-aioshot-py

Performance counters seem to start all at once, before requests are going to actually start. I need the semaphore to avoid the classic error about too many open files.

Could you please help me with this? I'm a big fan of aiohttp!

Kind Regards,
Roberto Prevato
Reply all
Reply to author
Forward
0 new messages