Average the code overhead from 10 data samples, to make it more stable
import asyncio
import dataclasses
-from typing import Tuple, NamedTuple
+from typing import List
import pytest
from asynctest import CoroutineMock
@@ -120,6 +120,10 @@ async def code_overhead(resource, stream, aresponses, watcher_limited, timer) ->
It does depend on the hardware used, or containers in the CI systems.
"""
+ # Collect a few data samples to make the estimation realistic.
+ overheads: List[float] = []
+ for _ in range(10):
+
# We feed the stream and consume the stream before we go into the tests,
# which can feed the stream with their own events.
stream.feed([
@@ -145,14 +149,19 @@ async def code_overhead(resource, stream, aresponses, watcher_limited, timer) ->
# Ensure that everything worked as expected, i.e. the worker is not mocked,
# and the whole code is actually executed down to the processor callback.
assert processor.awaited, "The processor is not called for code overhead measurement."
+ overheads.append(timer.seconds)
+
+ # Reserve extra 10-30% from both sides for occasional variations.
+ overhead = 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[:] = []
# Uncomment for debugging of the actual timing: visible only with -s pytest option.
- # print(f"The estimated code overhead is {timer.seconds:.3f} seconds (unadjusted).")
+ # print(f"The estimated code overhead is {overhead}.")
- # Reserve extra 10-30% from both sides for occasional variations.
- return CodeOverhead(
- min=timer.seconds * 0.8,
- avg=timer.seconds * 1.0,
- max=timer.seconds * 1.2,
- )
+ return overhead
