Profiling¶
-
class
wax_toolbox.profiling.
Timer
(label, at_enter=False, report=<built-in function print>)[source]¶ Simple timer focused on practical use.
Parameters: - label (str) – label of the timer
- at_enter (bool) – whether it should be also displayed when entering the context. Defaults to False.
- report (func) – function to use for reporting. Defaults to logger.info
Example usage:
In [1]: import asyncio
In [2]: import time
In [3]: from concurrent.futures import ThreadPoolExecutor
In [4]: from wax_toolbox import profiling
In [5]: async def waiter():
...: await asyncio.sleep(5)
...: return
...:
In [6]: def secondwaiter():
...: time.sleep(5)
...: return
...:
In [7]: N = 5
In [8]: with profiling.Timer("Asyncio", at_enter=True, report=print):
...: loop = asyncio.get_event_loop()
...: asyncio.set_event_loop(loop)
...: tasks = []
...: for i in range(N):
...: tasks.append(asyncio.ensure_future(waiter()))
...: loop.run_until_complete(asyncio.wait(tasks))
...: loop.close()
...:
Asyncio in progress...
Asyncio took 5.003 sec
In [9]: with profiling.Timer("ThreadPoolExecutor", at_enter=True, report=print):
...: futures = []
...: with ThreadPoolExecutor(max_workers=4) as e: # have you spotted it ?
...: for i in range(N):
...: futures.append(e.submit(secondwaiter))
...: for i in range(len(futures)):
...: futures[i].result()
...: