Cache code overhead measurements throughout the session for all tests
@@ -83,8 +83,16 @@ class CodeOverhead:
max: float
+@pytest.fixture(scope='session')
+def _code_overhead_cache():
+ return []
+
+
@pytest.fixture()
-async def code_overhead(resource, stream, aresponses, watcher_limited, timer) -> CodeOverhead:
+async def code_overhead(
+ resource, stream, aresponses, watcher_limited, timer,
+ _code_overhead_cache,
+) -> CodeOverhead:
"""
Estimate the overhead of synchronous code in the watching routines.
@@ -118,7 +126,11 @@ async def code_overhead(resource, stream, aresponses, watcher_limited, timer) ->
Empirically, the overhead usually remains within the range of 50-150 ms.
It does not depend on the number of events or unique uids in the stream.
It does depend on the hardware used, or containers in the CI systems.
+
+ Several dummy runs are used to average the values, to avoid fluctuation.
+ The estimation happens only once per session, and is reused for all tests.
"""
+ if not _code_overhead_cache:
# Collect a few data samples to make the estimation realistic.
overheads: List[float] = []
@@ -152,11 +164,11 @@ async def code_overhead(resource, stream, aresponses, watcher_limited, timer) ->
overheads.append(timer.seconds)
# Reserve extra 10-30% from both sides for occasional variations.
- overhead = CodeOverhead(
+ _code_overhead_cache.append(CodeOverhead(
min=min(overheads) * 0.9,
avg=sum(overheads) / len(overheads),
max=max(overheads) * 1.1,
- )
+ ))
# Cleanup our own endpoints, if something is left.
aresponses._responses[:] = []
@@ -164,4 +176,4 @@ async def code_overhead(resource, stream, aresponses, watcher_limited, timer) ->
# Uncomment for debugging of the actual timing: visible only with -s pytest option.
# print(f"The estimated code overhead is {overhead}.")
- return overhead
+ return _code_overhead_cache[0]
