22
33import contextlib
44import inspect
5-
6- # System Imports
75import logging
86import os
97import platform
1311import time as _time_module
1412import warnings
1513from collections import deque
14+
15+ # System Imports
1616from pathlib import Path
1717from typing import TYPE_CHECKING , Any , Callable
1818from unittest import TestCase
2121import pytest
2222from 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
2626if 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