Skip to content

Commit a9f6196

Browse files
committed
boilerplate ready, need to fill in the gaps
1 parent 0e75144 commit a9f6196

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

codeflash/optimization/function_optimizer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,7 @@ def establish_original_code_baseline(
828828
test_env["PYTHONPATH"] += os.pathsep + str(self.args.project_root)
829829

830830
coverage_results = None
831+
lprofiler_results = None
831832
# Instrument codeflash capture
832833
try:
833834
instrument_codeflash_capture(
@@ -840,6 +841,7 @@ def establish_original_code_baseline(
840841
optimization_iteration=0,
841842
testing_time=TOTAL_LOOPING_TIME,
842843
enable_coverage=test_framework == "pytest",
844+
enable_profiler=test_framework == "pytest",
843845
code_context=code_context,
844846
)
845847
finally:
@@ -1058,6 +1060,7 @@ def run_and_parse_tests(
10581060
testing_time: float = TOTAL_LOOPING_TIME,
10591061
*,
10601062
enable_coverage: bool = False,
1063+
enable_profiler: bool = False,
10611064
pytest_min_loops: int = 5,
10621065
pytest_max_loops: int = 100_000,
10631066
code_context: CodeOptimizationContext | None = None,
@@ -1074,6 +1077,7 @@ def run_and_parse_tests(
10741077
pytest_timeout=INDIVIDUAL_TESTCASE_TIMEOUT,
10751078
verbose=True,
10761079
enable_coverage=enable_coverage,
1080+
enable_profiler=enable_profiler,
10771081
)
10781082
elif testing_type == TestingMode.PERFORMANCE:
10791083
result_file_path, run_result = run_benchmarking_tests(

codeflash/optimization/optimizer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def run(self) -> None:
8989
tempfile.mkdtemp(dir=self.args.tests_root, prefix="codeflash_concolic_")
9090
)
9191
try:
92-
ph("cli-optimize-functions-to-optimize", {"num_functions": num_optimizable_functions})
92+
ph('cli-optimize-functions-to-optimize", {"num_functions": num_optimizable_functions})
9393
if num_optimizable_functions == 0:
9494
logger.info("No functions found to optimize. Exiting…")
9595
return
@@ -102,7 +102,7 @@ def run(self) -> None:
102102
console.rule()
103103
logger.info(f"Discovered {num_discovered_tests} existing unit tests in {self.test_cfg.tests_root}")
104104
console.rule()
105-
ph("cli-optimize-discovered-tests", {"num_tests": num_discovered_tests})
105+
ph('cli-optimize-discovered-tests", {"num_tests": num_discovered_tests})
106106

107107
for original_module_path in file_to_funcs_to_optimize:
108108
logger.info(f"Examining file {original_module_path!s}…")

codeflash/verification/test_runner.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def run_behavioral_tests(
3636
verbose: bool = False,
3737
pytest_target_runtime_seconds: int = TOTAL_LOOPING_TIME,
3838
enable_coverage: bool = False,
39+
enable_profiler: bool = False,
3940
) -> tuple[Path, subprocess.CompletedProcess, Path | None]:
4041
if test_framework == "pytest":
4142
test_files: list[str] = []
@@ -109,6 +110,28 @@ def run_behavioral_tests(
109110
logger.debug(
110111
f"""Result return code: {results.returncode}, {"Result stderr:" + str(results.stderr) if results.stderr else ""}"""
111112
)
113+
if enable_profiler:
114+
coverage_database_file, coveragercfile = prepare_coverage_files()
115+
116+
cov_erase = execute_test_subprocess(
117+
shlex.split(f"{SAFE_SYS_EXECUTABLE} -m coverage erase"), cwd=cwd, env=pytest_test_env
118+
) # this cleanup is necessary to avoid coverage data from previous runs, if there are any,
119+
# then the current run will be appended to the previous data, which skews the results
120+
logger.debug(cov_erase)
121+
coverage_cmd = [SAFE_SYS_EXECUTABLE, "-m", "coverage", "run", f"--rcfile={coveragercfile.as_posix()}", "-m"]
122+
123+
if pytest_cmd == "pytest":
124+
coverage_cmd.extend(["pytest"])
125+
else:
126+
coverage_cmd.extend(shlex.split(pytest_cmd, posix=IS_POSIX)[1:])
127+
128+
results = execute_test_subprocess(
129+
coverage_cmd + common_pytest_args + result_args + test_files, cwd=cwd, env=pytest_test_env, timeout=600
130+
)
131+
logger.debug(
132+
f"Result return code: {results.returncode}, "
133+
f"{'Result stderr:' + str(results.stderr) if results.stderr else ''}"
134+
)
112135
elif test_framework == "unittest":
113136
if enable_coverage:
114137
msg = "Coverage is not supported yet for unittest framework"
@@ -127,7 +150,6 @@ def run_behavioral_tests(
127150

128151
return result_file_path, results, coverage_database_file if enable_coverage else None
129152

130-
131153
def run_benchmarking_tests(
132154
test_paths: TestFiles,
133155
pytest_cmd: str,

0 commit comments

Comments
 (0)