1616from typing import TYPE_CHECKING , Any , Callable , Optional
1717from unittest import TestCase
1818
19- # PyTest Imports
2019import pytest
2120from pluggy import HookspecMarker
2221
2625 STABILITY_WARMUP_LOOPS ,
2726 STABILITY_WINDOW_SIZE ,
2827)
29- from codeflash .code_utils .time_utils import humanize_runtime
28+
29+ # PyTest Imports
3030from codeflash .result .best_summed_runtime import calculate_best_summed_runtime
3131
3232if TYPE_CHECKING :
@@ -338,9 +338,12 @@ def __init__(self, config: Config) -> None:
338338 self .logger = logging .getLogger (self .name )
339339 self .usable_runtime_data_by_test_case : dict [str , list [int ]] = {}
340340 self .total_loop_runtimes : list [int ] = []
341+ self .is_perf_test : bool = config .option .codeflash_max_loops > 1
341342
342343 @pytest .hookimpl
343344 def pytest_runtest_logreport (self , report : pytest .TestReport ) -> None :
345+ if not self .is_perf_test :
346+ return
344347 if report .when == "call" and report .passed :
345348 duration_ns = get_runtime_from_stdout (report .capstdout )
346349 if duration_ns :
@@ -353,7 +356,6 @@ def pytest_runtestloop(self, session: Session) -> bool:
353356 if session .testsfailed and not session .config .option .continue_on_collection_errors :
354357 msg = "{} error{} during collection" .format (session .testsfailed , "s" if session .testsfailed != 1 else "" )
355358 raise session .Interrupted (msg )
356- is_perf_test = bool (session .config .option .codeflash_max_loops > 1 )
357359
358360 if session .config .option .collectonly :
359361 return True
@@ -363,7 +365,6 @@ def pytest_runtestloop(self, session: Session) -> bool:
363365
364366 count : int = 0
365367 runtimes = []
366- break_at = - 1
367368 elapsed = 0.0
368369
369370 while total_time >= SHORTEST_AMOUNT_OF_TIME :
@@ -386,7 +387,7 @@ def pytest_runtestloop(self, session: Session) -> bool:
386387 if session .shouldstop :
387388 raise session .Interrupted (session .shouldstop )
388389
389- if is_perf_test :
390+ if self . is_perf_test :
390391 loop_end = _ORIGINAL_PERF_COUNTER_NS ()
391392 dt = loop_end - loop_start # nano-seconds
392393
@@ -404,40 +405,15 @@ def pytest_runtestloop(self, session: Session) -> bool:
404405
405406 warmup_loops = math .floor (STABILITY_WARMUP_LOOPS * estimated_total_loops )
406407 window_size = math .floor (STABILITY_WINDOW_SIZE * estimated_total_loops )
407- if ( # noqa: SIM102
408- warmup_loops > 1 and window_size > 1 and should_stop (runtimes , warmup_loops , window_size )
408+ if (
409+ count >= session .config .option .codeflash_min_loops
410+ and warmup_loops > 1
411+ and window_size > 1
412+ and should_stop (runtimes , warmup_loops , window_size )
409413 ):
410- if break_at == - 1 :
411- break_at = count
412- # break
414+ break
413415
414416 if self ._timed_out (session , start_time , count ):
415- if is_perf_test :
416- did_break = "true" if break_at != - 1 else "false"
417- best_of_all = min (runtimes )
418- best_before_break = min (runtimes [:break_at ])
419-
420- runtimes_after_break = self .total_loop_runtimes [break_at :]
421- total_after_break = str (sum (runtimes_after_break )) if did_break == "true" else "NA"
422- total_runtime = str (sum (self .total_loop_runtimes ))
423-
424- accuracy_str = "NA"
425- if did_break == "true" :
426- accuracy = best_of_all / best_before_break * 100
427- accuracy_str = f"{ accuracy :.2f} "
428- Path (
429- f"/home/mohammed/Documents/test-results/optimize-me-exp/exp-{ int (_ORIGINAL_TIME_TIME ())} .json"
430- ).write_text (f"""{{
431- "runtimes": { runtimes } ,
432- "did_break": { did_break } ,
433- "accuracy": "{ accuracy_str } %",
434- "time_saved": "{ total_after_break } ",
435- "total_runtime": "{ total_runtime } ",
436- "total_loops": { count } ,
437- "breaked_at": { break_at } ,
438- "best_of_all": "{ humanize_runtime (best_of_all )} ",
439- "best_before_break": "{ humanize_runtime (best_before_break )} "
440- }}""" )
441417 break
442418
443419 _ORIGINAL_TIME_SLEEP (self ._get_delay_time (session ))
0 commit comments