Skip to content

Commit 95f22ee

Browse files
better stability with sum the min of all prev loops
1 parent 3159eb6 commit 95f22ee

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

codeflash/verification/pytest_plugin.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ def get_runtime_from_stdout(stdout: str) -> Optional[int]:
287287

288288
def should_stop(
289289
runtimes: list[int],
290-
warmup: int = 3,
291-
window: int = 3,
290+
warmup: int = 4,
291+
window: int = 6,
292292
center_rel_tol: float = 0.01, # ±1% around median
293293
spread_rel_tol: float = 0.02, # 2% window spread
294294
slope_rel_tol: float = 0.01, # 1% improvement allowed
@@ -324,7 +324,7 @@ def __init__(self, config: Config) -> None:
324324
level = logging.DEBUG if config.option.verbose > 1 else logging.INFO
325325
logging.basicConfig(level=level)
326326
self.logger = logging.getLogger(self.name)
327-
self.current_loop_durations_in_nano: list[int] = []
327+
self.usable_runtime_data_by_test_case: dict[str, list[int]] = {}
328328

329329
def dynamic_tolerance(self, avg_ns: float) -> float: # noqa: PLR0911
330330
if avg_ns < 200_000: # < 0.2 ms
@@ -345,8 +345,10 @@ def dynamic_tolerance(self, avg_ns: float) -> float: # noqa: PLR0911
345345

346346
@pytest.hookimpl
347347
def pytest_runtest_logreport(self, report: pytest.TestReport) -> None:
348-
if report.when == "call" and (duration_ns := get_runtime_from_stdout(report.capstdout)):
349-
self.current_loop_durations_in_nano.append(duration_ns)
348+
if report.when == "call" and report.passed and (duration_ns := get_runtime_from_stdout(report.capstdout)): # noqa: SIM102
349+
if duration_ns:
350+
clean_id = re.sub(r"\s*\[\s*\d+\s*\]\s*$", "", report.nodeid)
351+
self.usable_runtime_data_by_test_case.setdefault(clean_id, []).append(duration_ns)
350352

351353
@hookspec(firstresult=True)
352354
def pytest_runtestloop(self, session: Session) -> bool:
@@ -365,8 +367,6 @@ def pytest_runtestloop(self, session: Session) -> bool:
365367
runtimes = []
366368
while total_time >= SHORTEST_AMOUNT_OF_TIME:
367369
count += 1
368-
self.current_loop_durations_in_nano.clear()
369-
370370
for index, item in enumerate(session.items):
371371
item: pytest.Item = item # noqa: PLW0127, PLW2901
372372
item._report_sections.clear() # clear reports for new test # noqa: SLF001
@@ -384,8 +384,10 @@ def pytest_runtestloop(self, session: Session) -> bool:
384384
if session.shouldstop:
385385
raise session.Interrupted(session.shouldstop)
386386

387-
total_duration_in_nano = sum(self.current_loop_durations_in_nano)
388-
runtimes.append(total_duration_in_nano)
387+
best_runtime_until_now = sum(
388+
[min(usable_runtime_data) for _, usable_runtime_data in self.usable_runtime_data_by_test_case.items()]
389+
)
390+
runtimes.append(best_runtime_until_now)
389391

390392
if should_stop(runtimes):
391393
break

0 commit comments

Comments
 (0)