|
9 | 9 |
|
10 | 10 | @dataclass
|
11 | 11 | class IoLoopMonitorState:
|
12 |
| - # The time it took to execute the loop in wall time (For example asyncio.sleep will also be recorded). |
13 |
| - wall_loop_duration: float |
14 |
| - # The time it took to execute the loop in cpu time (For example asyncio.sleep will not be recorded). |
15 |
| - cpu_loop_duration: float |
16 |
| - # The amount of handles in the loop - https://docs.python.org/3/library/asyncio-eventloop.html#callback-handles |
17 |
| - handles_count: int |
| 12 | + """ |
| 13 | + A dataclass containing the state of the loop when the callback was executed. |
| 14 | + This class is the interface that the monitor callback will receive. |
| 15 | +
|
| 16 | + A basic Lexicon: |
| 17 | + * Handle - A wrapper for a callback that is scheduled to be executed by the loop. |
| 18 | + * Callback - The function that is executed by the loop. |
| 19 | + * Loop - The event loop that is executing the callbacks. |
| 20 | + """ |
| 21 | + |
| 22 | + """ |
| 23 | + Wall executing time of the callback |
| 24 | + It can be the whole coroutine or parts of it, depending on if the executing control |
| 25 | + was delegated back the loop or not. |
| 26 | +
|
| 27 | + Wall Time explanation - https://en.wikipedia.org/wiki/Wall-clock_time |
| 28 | + """ |
| 29 | + callback_wall_time: float |
| 30 | + |
| 31 | + """ |
| 32 | + The amount of handles in the loop, excluding the current one. |
| 33 | + """ |
| 34 | + loop_handles_count: int |
| 35 | + |
| 36 | + """ |
| 37 | + The amount of time it took from the moment the coroutine was added to the loop until it was executed. |
| 38 | + """ |
| 39 | + loop_lag: float |
18 | 40 |
|
19 | 41 |
|
20 | 42 | @dataclass
|
@@ -48,21 +70,21 @@ def wrap_callback_with_monitoring(
|
48 | 70 | back to the monitor_callback.
|
49 | 71 | """
|
50 | 72 | ioloop_state.increase_handles_count_thread_safe(1)
|
| 73 | + added_to_loop_time = time.perf_counter() |
51 | 74 |
|
52 | 75 | def wrapper(*inner_args: typing.Any, **inner_kwargs: typing.Any) -> typing.Any:
|
53 |
| - start_wall_time = time.time() |
54 |
| - start_cpu_time = time.process_time() |
| 76 | + loop_lag = time.perf_counter() - added_to_loop_time |
| 77 | + start_wall_time = time.perf_counter() |
55 | 78 | response = callback(*inner_args, **inner_kwargs)
|
56 | 79 | ioloop_state.decrease_handles_count_thread_safe(1)
|
57 |
| - wall_duration = time.time() - start_wall_time |
58 |
| - cpu_duration = time.process_time() - start_cpu_time |
| 80 | + wall_duration = time.perf_counter() - start_wall_time |
59 | 81 |
|
60 | 82 | try:
|
61 | 83 | monitor_callback(
|
62 | 84 | IoLoopMonitorState(
|
63 |
| - wall_loop_duration=wall_duration, |
64 |
| - cpu_loop_duration=cpu_duration, |
65 |
| - handles_count=ioloop_state.handles_count, |
| 85 | + callback_wall_time=wall_duration, |
| 86 | + loop_handles_count=ioloop_state.handles_count, |
| 87 | + loop_lag=loop_lag, |
66 | 88 | )
|
67 | 89 | )
|
68 | 90 | except Exception:
|
|
0 commit comments