-
Notifications
You must be signed in to change notification settings - Fork 26
[Enhancement] Stop looping when runtime is stable (CF-934) #967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
e4cbf95
exp
mohammedahmed18 01ad626
still experimenting
mohammedahmed18 2b01faf
Merge branch 'main' of github.com:codeflash-ai/codeflash into exp/con…
mohammedahmed18 ce89905
reset
mohammedahmed18 1f367be
dynamic tolerance
mohammedahmed18 0d819a8
Merge branch 'main' of github.com:codeflash-ai/codeflash into exp/con…
mohammedahmed18 5c4a6d9
get the duration from the pytest overriden methods
mohammedahmed18 ecd21d5
remove debug log
mohammedahmed18 30c89ce
respect the min loop count -just in case-
mohammedahmed18 ce2c05b
Merge branch 'main' of github.com:codeflash-ai/codeflash into exp/con…
mohammedahmed18 89fc939
more closer method
mohammedahmed18 cc94694
Merge branch 'main' of github.com:codeflash-ai/codeflash into exp/con…
mohammedahmed18 a67dad3
working version
mohammedahmed18 d52aae4
even better
mohammedahmed18 244f9ca
better stability algorithm
mohammedahmed18 a890d4f
should stop metrics
mohammedahmed18 3159eb6
Merge branch 'main' of github.com:codeflash-ai/codeflash into exp/con…
mohammedahmed18 95f22ee
better stability with sum the min of all prev loops
mohammedahmed18 9f311cd
Optimize should_stop
codeflash-ai[bot] 83dff02
best summed runtime helper
mohammedahmed18 a8e93c7
Merge branch 'main' of github.com:codeflash-ai/codeflash into codefla…
mohammedahmed18 e49ba13
linting
mohammedahmed18 91cbc74
Merge pull request #984 from codeflash-ai/codeflash/optimize-pr967-20…
mohammedahmed18 0b3be3f
some enhancements from claude pr review
mohammedahmed18 b57fa1a
Merge branch 'exp/consistent-loop-break' of github.com:codeflash-ai/c…
mohammedahmed18 46701c7
window percentage
mohammedahmed18 74520f6
cleanup
mohammedahmed18 9ab06d3
revert comment
mohammedahmed18 f1058ea
for unit tests
mohammedahmed18 56cce15
Merge branch 'main' of github.com:codeflash-ai/codeflash into exp/con…
mohammedahmed18 8ea9231
toggle stability by arg and zero tolerance
mohammedahmed18 9079590
configs and cleaner impl
mohammedahmed18 70b7627
typo
mohammedahmed18 dd3707a
Merge branch 'main' of github.com:codeflash-ai/codeflash into exp/con…
mohammedahmed18 9cae2a1
refactor
mohammedahmed18 7f1818b
refactoring
mohammedahmed18 270af89
Merge branch 'main' into exp/consistent-loop-break
KRRT7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,15 +2,17 @@ | |
|
|
||
| import contextlib | ||
| import inspect | ||
|
|
||
| # System Imports | ||
| import logging | ||
| import os | ||
| import platform | ||
| import re | ||
| import statistics | ||
| import sys | ||
| import time as _time_module | ||
| import warnings | ||
| from collections import deque | ||
|
|
||
| # System Imports | ||
| from pathlib import Path | ||
| from typing import TYPE_CHECKING, Any, Callable | ||
| from unittest import TestCase | ||
|
|
@@ -19,6 +21,8 @@ | |
| import pytest | ||
| from pluggy import HookspecMarker | ||
|
|
||
| from codeflash.code_utils.config_consts import CONSISTENT_LOOP_COUNT | ||
|
|
||
| if TYPE_CHECKING: | ||
| from _pytest.config import Config, Parser | ||
| from _pytest.main import Session | ||
|
|
@@ -268,9 +272,30 @@ def __init__(self, config: Config) -> None: | |
| level = logging.DEBUG if config.option.verbose > 1 else logging.INFO | ||
| logging.basicConfig(level=level) | ||
| self.logger = logging.getLogger(self.name) | ||
| self.current_loop_durations_in_seconds: list[float] = [] | ||
|
|
||
| def dynamic_tolerance(self, avg: float) -> float: | ||
| if avg < 0.0001: # < 100 µs | ||
| return 0.7 | ||
| if avg < 0.0005: # < 500 µs | ||
| return 0.5 | ||
| if avg < 0.001: # < 1 ms | ||
| return 0.4 | ||
| if avg < 0.01: # < 10 ms | ||
| return 0.2 | ||
| if avg < 0.1: # < 100 ms | ||
| return 0.1 | ||
| return 0.03 # > 0.1 s | ||
|
|
||
| @pytest.hookimpl | ||
| def pytest_runtest_logreport(self, report: pytest.TestReport) -> None: | ||
| if report.when == "call" and report.outcome == "passed": | ||
| self.current_loop_durations_in_seconds.append(report.duration) | ||
|
|
||
| @hookspec(firstresult=True) | ||
| def pytest_runtestloop(self, session: Session) -> bool: | ||
| durations = deque(maxlen=CONSISTENT_LOOP_COUNT) | ||
|
|
||
| """Reimplement the test loop but loop for the user defined amount of time.""" | ||
| if session.testsfailed and not session.config.option.continue_on_collection_errors: | ||
| msg = "{} error{} during collection".format(session.testsfailed, "s" if session.testsfailed != 1 else "") | ||
|
|
@@ -284,9 +309,9 @@ def pytest_runtestloop(self, session: Session) -> bool: | |
|
|
||
| count: int = 0 | ||
|
|
||
| while total_time >= SHORTEST_AMOUNT_OF_TIME: # need to run at least one for normal tests | ||
| while total_time >= SHORTEST_AMOUNT_OF_TIME: | ||
| count += 1 | ||
| total_time = self._get_total_time(session) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see if total_time changes inside this loop
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it doesn't, I checked |
||
| self.current_loop_durations_in_seconds.clear() | ||
|
|
||
| for index, item in enumerate(session.items): | ||
| item: pytest.Item = item # noqa: PLW0127, PLW2901 | ||
|
|
@@ -304,8 +329,27 @@ def pytest_runtestloop(self, session: Session) -> bool: | |
| raise session.Failed(session.shouldfail) | ||
| if session.shouldstop: | ||
| raise session.Interrupted(session.shouldstop) | ||
|
|
||
| total_duration_in_seconds = sum(self.current_loop_durations_in_seconds) | ||
|
|
||
| if total_duration_in_seconds > 0: | ||
| durations.append(total_duration_in_seconds) | ||
| else: | ||
| durations.clear() | ||
|
|
||
| # Consistency check | ||
| if len(durations) == CONSISTENT_LOOP_COUNT: | ||
| avg = statistics.median(durations) | ||
| if avg == 0: | ||
| consistent = all(d == 0 for d in durations) | ||
| else: | ||
| consistent = all(abs(d - avg) / avg <= self.dynamic_tolerance(avg) for d in durations) | ||
| if consistent: | ||
| break | ||
|
|
||
| if self._timed_out(session, start_time, count): | ||
| break # exit loop | ||
| break | ||
|
|
||
| _ORIGINAL_TIME_SLEEP(self._get_delay_time(session)) | ||
| return True | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.