Skip to content

Commit 29f4c1a

Browse files
committed
wrote some tests for instrumentation+testrun+parsing, todo, write some tests for each component
1 parent 82f4138 commit 29f4c1a

File tree

1 file changed

+104
-3
lines changed

1 file changed

+104
-3
lines changed

tests/test_instrument_tests.py

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
FunctionImportedAsVisitor,
1313
inject_profiling_into_existing_test,
1414
)
15+
from codeflash.code_utils.line_profile_utils import add_decorator_imports
1516
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
16-
from codeflash.models.models import CodePosition, FunctionParent, TestFile, TestFiles, TestingMode, TestsInFile
17+
from codeflash.either import is_successful, Failure
18+
from codeflash.models.models import CodePosition, FunctionParent, TestFile, TestFiles, TestingMode, TestsInFile, \
19+
CodeOptimizationContext
1720
from codeflash.optimization.function_optimizer import FunctionOptimizer
1821
from codeflash.verification.test_results import TestType
1922
from codeflash.verification.verification_utils import TestConfig
@@ -488,7 +491,31 @@ def test_sort():
488491
codeflash stdout: Sorting list
489492
result: [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]"""
490493
assert out_str == test_results_perf[1].stdout
491-
494+
ctx_result = func_optimizer.get_code_optimization_context()
495+
code_context: CodeOptimizationContext = ctx_result.unwrap()
496+
original_helper_code: dict[Path, str] = {}
497+
helper_function_paths = {hf.file_path for hf in code_context.helper_functions}
498+
for helper_function_path in helper_function_paths:
499+
with helper_function_path.open(encoding="utf8") as f:
500+
helper_code = f.read()
501+
original_helper_code[helper_function_path] = helper_code
502+
line_profiler_output_file = add_decorator_imports(
503+
func_optimizer.function_to_optimize, code_context)
504+
line_profile_results, _ = func_optimizer.run_and_parse_tests(
505+
testing_type=TestingMode.LINE_PROFILE,
506+
test_env=test_env,
507+
test_files=test_files,
508+
optimization_iteration=0,
509+
pytest_min_loops=1,
510+
pytest_max_loops=1,
511+
testing_time=0.1,
512+
line_profiler_output_file = line_profiler_output_file
513+
)
514+
func_optimizer.write_code_and_helpers(
515+
func_optimizer.function_to_optimize_source_code, original_helper_code, func_optimizer.function_to_optimize.file_path
516+
)
517+
tmp_lpr = list(line_profile_results['timings'].keys())
518+
assert len(tmp_lpr) == 1 and line_profile_results['timings'][tmp_lpr[0]][0][1]==2
492519
finally:
493520
test_path.unlink(missing_ok=True)
494521
test_path_perf.unlink(missing_ok=True)
@@ -714,7 +741,31 @@ def test_sort_parametrized(input, expected_output):
714741
)
715742
assert test_results_perf[2].runtime > 0
716743
assert test_results_perf[2].did_pass
717-
744+
ctx_result = func_optimizer.get_code_optimization_context()
745+
code_context: CodeOptimizationContext = ctx_result.unwrap()
746+
original_helper_code: dict[Path, str] = {}
747+
helper_function_paths = {hf.file_path for hf in code_context.helper_functions}
748+
for helper_function_path in helper_function_paths:
749+
with helper_function_path.open(encoding="utf8") as f:
750+
helper_code = f.read()
751+
original_helper_code[helper_function_path] = helper_code
752+
line_profiler_output_file = add_decorator_imports(
753+
func_optimizer.function_to_optimize, code_context)
754+
line_profile_results, _ = func_optimizer.run_and_parse_tests(
755+
testing_type=TestingMode.LINE_PROFILE,
756+
test_env=test_env,
757+
test_files=test_files,
758+
optimization_iteration=0,
759+
pytest_min_loops=1,
760+
pytest_max_loops=1,
761+
testing_time=0.1,
762+
line_profiler_output_file = line_profiler_output_file
763+
)
764+
func_optimizer.write_code_and_helpers(
765+
func_optimizer.function_to_optimize_source_code, original_helper_code, func_optimizer.function_to_optimize.file_path
766+
)
767+
tmp_lpr = list(line_profile_results['timings'].keys())
768+
assert len(tmp_lpr) == 1 and line_profile_results['timings'][tmp_lpr[0]][0][1]==3
718769
finally:
719770
test_path.unlink(missing_ok=True)
720771
test_path_perf.unlink(missing_ok=True)
@@ -1026,6 +1077,31 @@ def test_sort_parametrized_loop(input, expected_output):
10261077
)
10271078
assert test_results[5].runtime > 0
10281079
assert test_results[5].did_pass
1080+
ctx_result = func_optimizer.get_code_optimization_context()
1081+
code_context: CodeOptimizationContext = ctx_result.unwrap()
1082+
original_helper_code: dict[Path, str] = {}
1083+
helper_function_paths = {hf.file_path for hf in code_context.helper_functions}
1084+
for helper_function_path in helper_function_paths:
1085+
with helper_function_path.open(encoding="utf8") as f:
1086+
helper_code = f.read()
1087+
original_helper_code[helper_function_path] = helper_code
1088+
line_profiler_output_file = add_decorator_imports(
1089+
func_optimizer.function_to_optimize, code_context)
1090+
line_profile_results, _ = func_optimizer.run_and_parse_tests(
1091+
testing_type=TestingMode.LINE_PROFILE,
1092+
test_env=test_env,
1093+
test_files=test_files,
1094+
optimization_iteration=0,
1095+
pytest_min_loops=1,
1096+
pytest_max_loops=1,
1097+
testing_time=0.1,
1098+
line_profiler_output_file = line_profiler_output_file
1099+
)
1100+
func_optimizer.write_code_and_helpers(
1101+
func_optimizer.function_to_optimize_source_code, original_helper_code, func_optimizer.function_to_optimize.file_path
1102+
)
1103+
tmp_lpr = list(line_profile_results['timings'].keys())
1104+
assert len(tmp_lpr) == 1 and line_profile_results['timings'][tmp_lpr[0]][0][1]==6
10291105
finally:
10301106
test_path.unlink(missing_ok=True)
10311107
test_path_behavior.unlink(missing_ok=True)
@@ -1271,6 +1347,31 @@ def test_sort():
12711347
)
12721348
assert test_results[2].runtime > 0
12731349
assert test_results[2].did_pass
1350+
ctx_result = func_optimizer.get_code_optimization_context()
1351+
code_context: CodeOptimizationContext = ctx_result.unwrap()
1352+
original_helper_code: dict[Path, str] = {}
1353+
helper_function_paths = {hf.file_path for hf in code_context.helper_functions}
1354+
for helper_function_path in helper_function_paths:
1355+
with helper_function_path.open(encoding="utf8") as f:
1356+
helper_code = f.read()
1357+
original_helper_code[helper_function_path] = helper_code
1358+
line_profiler_output_file = add_decorator_imports(
1359+
func_optimizer.function_to_optimize, code_context)
1360+
line_profile_results, _ = func_optimizer.run_and_parse_tests(
1361+
testing_type=TestingMode.LINE_PROFILE,
1362+
test_env=test_env,
1363+
test_files=test_files,
1364+
optimization_iteration=0,
1365+
pytest_min_loops=1,
1366+
pytest_max_loops=1,
1367+
testing_time=0.1,
1368+
line_profiler_output_file = line_profiler_output_file
1369+
)
1370+
func_optimizer.write_code_and_helpers(
1371+
func_optimizer.function_to_optimize_source_code, original_helper_code, func_optimizer.function_to_optimize.file_path
1372+
)
1373+
tmp_lpr = list(line_profile_results['timings'].keys())
1374+
assert len(tmp_lpr) == 1 and line_profile_results['timings'][tmp_lpr[0]][0][1]==3
12741375
finally:
12751376
test_path.unlink(missing_ok=True)
12761377
test_path_perf.unlink(missing_ok=True)

0 commit comments

Comments
 (0)