|
12 | 12 | FunctionImportedAsVisitor, |
13 | 13 | inject_profiling_into_existing_test, |
14 | 14 | ) |
| 15 | +from codeflash.code_utils.line_profile_utils import add_decorator_imports |
15 | 16 | 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 |
17 | 20 | from codeflash.optimization.function_optimizer import FunctionOptimizer |
18 | 21 | from codeflash.verification.test_results import TestType |
19 | 22 | from codeflash.verification.verification_utils import TestConfig |
@@ -488,7 +491,31 @@ def test_sort(): |
488 | 491 | codeflash stdout: Sorting list |
489 | 492 | result: [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]""" |
490 | 493 | 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 |
492 | 519 | finally: |
493 | 520 | test_path.unlink(missing_ok=True) |
494 | 521 | test_path_perf.unlink(missing_ok=True) |
@@ -714,7 +741,31 @@ def test_sort_parametrized(input, expected_output): |
714 | 741 | ) |
715 | 742 | assert test_results_perf[2].runtime > 0 |
716 | 743 | 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 |
718 | 769 | finally: |
719 | 770 | test_path.unlink(missing_ok=True) |
720 | 771 | test_path_perf.unlink(missing_ok=True) |
@@ -1026,6 +1077,31 @@ def test_sort_parametrized_loop(input, expected_output): |
1026 | 1077 | ) |
1027 | 1078 | assert test_results[5].runtime > 0 |
1028 | 1079 | 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 |
1029 | 1105 | finally: |
1030 | 1106 | test_path.unlink(missing_ok=True) |
1031 | 1107 | test_path_behavior.unlink(missing_ok=True) |
@@ -1271,6 +1347,31 @@ def test_sort(): |
1271 | 1347 | ) |
1272 | 1348 | assert test_results[2].runtime > 0 |
1273 | 1349 | 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 |
1274 | 1375 | finally: |
1275 | 1376 | test_path.unlink(missing_ok=True) |
1276 | 1377 | test_path_perf.unlink(missing_ok=True) |
|
0 commit comments