Skip to content

Commit 5c4a6d9

Browse files
get the duration from the pytest overriden methods
1 parent 0d819a8 commit 5c4a6d9

3 files changed

Lines changed: 29 additions & 23 deletions

File tree

codeflash/code_utils/config_consts.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
MAX_CUMULATIVE_TEST_RUNTIME_NANOSECONDS = 100e6 # 100ms
99
N_TESTS_TO_GENERATE = 2
1010
TOTAL_LOOPING_TIME = 10.0 # 10 second candidate benchmarking budget
11-
CONSISTENT_LOOP_COUNT = 5
12-
CONSISTENT_DURATION_TOLERANCE = 0.05
11+
CONSISTENT_LOOP_COUNT = 3
1312
COVERAGE_THRESHOLD = 60.0
1413
MIN_TESTCASE_PASSED_THRESHOLD = 6
1514
REPEAT_OPTIMIZATION_PROBABILITY = 0.1

codeflash/code_utils/env_utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
def check_formatter_installed(formatter_cmds: list[str], exit_on_failure: bool = True) -> bool: # noqa
2020
if not formatter_cmds or formatter_cmds[0] == "disabled":
2121
return True
22-
2322
first_cmd = formatter_cmds[0]
2423
cmd_tokens = shlex.split(first_cmd) if isinstance(first_cmd, str) else [first_cmd]
2524

codeflash/verification/pytest_plugin.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import contextlib
44
import inspect
5-
6-
# System Imports
75
import logging
86
import os
97
import platform
@@ -13,6 +11,8 @@
1311
import time as _time_module
1412
import warnings
1513
from collections import deque
14+
15+
# System Imports
1616
from pathlib import Path
1717
from typing import TYPE_CHECKING, Any, Callable
1818
from unittest import TestCase
@@ -21,7 +21,7 @@
2121
import pytest
2222
from pluggy import HookspecMarker
2323

24-
from codeflash.code_utils.config_consts import CONSISTENT_DURATION_TOLERANCE, CONSISTENT_LOOP_COUNT
24+
from codeflash.code_utils.config_consts import CONSISTENT_LOOP_COUNT
2525

2626
if TYPE_CHECKING:
2727
from _pytest.config import Config, Parser
@@ -272,21 +272,25 @@ def __init__(self, config: Config) -> None:
272272
level = logging.DEBUG if config.option.verbose > 1 else logging.INFO
273273
logging.basicConfig(level=level)
274274
self.logger = logging.getLogger(self.name)
275+
self.current_loop_durations_in_seconds: list[float] = []
275276

276277
def dynamic_tolerance(self, avg: float) -> float:
277-
# (< 0.1 ms)
278-
if avg < 0.0001:
279-
return 0.7 # 70%
280-
281-
# (< 0.5 ms)
282-
if avg < 0.0005:
283-
return 0.4 # 40%
284-
285-
# (< 1 ms)
286-
if avg < 0.001:
287-
return 0.2 # 20%
288-
289-
return CONSISTENT_DURATION_TOLERANCE
278+
if avg < 0.0001: # < 100 µs
279+
return 0.7
280+
if avg < 0.0005: # < 500 µs
281+
return 0.5
282+
if avg < 0.001: # < 1 ms
283+
return 0.4
284+
if avg < 0.01: # < 10 ms
285+
return 0.2
286+
if avg < 0.1: # < 100 ms
287+
return 0.1
288+
return 0.03 # > 0.1 s
289+
290+
@pytest.hookimpl
291+
def pytest_runtest_logreport(self, report: pytest.TestReport) -> None:
292+
if report.when == "call" and report.outcome == "passed":
293+
self.current_loop_durations_in_seconds.append(report.duration)
290294

291295
@hookspec(firstresult=True)
292296
def pytest_runtestloop(self, session: Session) -> bool:
@@ -307,7 +311,7 @@ def pytest_runtestloop(self, session: Session) -> bool:
307311

308312
while total_time >= SHORTEST_AMOUNT_OF_TIME:
309313
count += 1
310-
loop_start = _ORIGINAL_TIME_TIME()
314+
self.current_loop_durations_in_seconds.clear()
311315

312316
for index, item in enumerate(session.items):
313317
item: pytest.Item = item # noqa: PLW0127, PLW2901
@@ -326,9 +330,12 @@ def pytest_runtestloop(self, session: Session) -> bool:
326330
if session.shouldstop:
327331
raise session.Interrupted(session.shouldstop)
328332

329-
loop_end = _ORIGINAL_TIME_TIME()
330-
loop_duration = loop_end - loop_start
331-
durations.append(loop_duration)
333+
total_duration_in_seconds = sum(self.current_loop_durations_in_seconds)
334+
335+
if total_duration_in_seconds > 0:
336+
durations.append(total_duration_in_seconds)
337+
else:
338+
durations.clear()
332339

333340
# Consistency check
334341
if len(durations) == CONSISTENT_LOOP_COUNT:
@@ -338,6 +345,7 @@ def pytest_runtestloop(self, session: Session) -> bool:
338345
else:
339346
consistent = all(abs(d - avg) / avg <= self.dynamic_tolerance(avg) for d in durations)
340347
if consistent:
348+
Path(f"/home/mohammed/Documents/test-results/break-{session.name}.txt").write_text(str(count))
341349
break
342350

343351
if self._timed_out(session, start_time, count):

0 commit comments

Comments
 (0)