For testing purposes, I would like to measure the time that is spent on blocking execution of a coroutine (i.e, excluding the time for which it is suspended).
For example:
import trio
import time
async def under_test():
await trio.sleep(2)
time.sleep(3)
async def measure():
with measure_blocking_time() as ctx: # or something like that
await under_test()
assert ctx.elapsed == 3
trio.run(measure)
How do I do that?
(There seems to be a somewhat hacky way to do this when using asyncio - hopefully it can be done more nicer in Trio?)
belm0/perf-timer solves exactly this problem for threads and Trio, but not asyncio.
The relevant usage snippet from the readme is:
from perf_timer import TrioPerfTimer
@TrioPerfTimer('get thumbnail')
async def get_thumbnail_image(path):
img = cache.get_thumbnail(path)
if not thumbnail:
img = await read_image(path)
img.decode()
img.resize(THUMBNAIL_SIZE)
cache.set_thumbnail(img)
return img