Skip to content

Commit a5dc011

Browse files
authored
Merge pull request #1031 from codeflash-ai/add-limit-to-codeflash-optimize--m-pytest
2 parents 210f3c3 + 58cd8e4 commit a5dc011

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

codeflash/tracer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ def main(args: Namespace | None = None) -> ArgumentParser:
5757
default=None,
5858
)
5959
parser.add_argument("--trace-only", action="store_true", help="Trace and create replay tests only, don't optimize")
60+
parser.add_argument(
61+
"--limit", type=int, default=None, help="Limit the number of test files to process (for -m pytest mode)"
62+
)
6063

6164
if args is not None:
6265
parsed_args = args
@@ -106,7 +109,7 @@ def main(args: Namespace | None = None) -> ArgumentParser:
106109
test_paths = []
107110
replay_test_paths = []
108111
if parsed_args.module and unknown_args[0] == "pytest":
109-
pytest_splits, test_paths = pytest_split(unknown_args[1:])
112+
pytest_splits, test_paths = pytest_split(unknown_args[1:], limit=parsed_args.limit)
110113
if pytest_splits is None or test_paths is None:
111114
console.print(f"❌ Could not find test files in the specified paths: {unknown_args[1:]}")
112115
console.print(f"Current working directory: {Path.cwd()}")

codeflash/tracing/pytest_parallelization.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88

99
def pytest_split(
10-
arguments: list[str], num_splits: int | None = None
10+
arguments: list[str], num_splits: int | None = None, limit: int | None = None
1111
) -> tuple[list[list[str]] | None, list[str] | None]:
1212
"""Split pytest test files from a directory into N roughly equal groups for parallel execution.
1313
1414
Args:
1515
arguments: List of arguments passed to pytest
16-
test_directory: Path to directory containing test files
1716
num_splits: Number of groups to split tests into. If None, uses CPU count.
17+
limit: Maximum number of test files to process. If None, processes all files.
1818
1919
Returns:
2020
List of lists, where each inner list contains test file paths for one group.
@@ -58,6 +58,10 @@ def pytest_split(
5858
test_files = list(test_files)
5959
shuffle(test_files)
6060

61+
# Apply limit if specified
62+
if limit is not None and limit > 0:
63+
test_files = test_files[:limit]
64+
6165
# Ensure each split has at least 4 test files
6266
# If we have fewer test files than 4 * num_splits, reduce num_splits
6367
max_possible_splits = len(test_files) // 4

0 commit comments

Comments
 (0)