diff --git a/codeflash/verification/parse_line_profile_test_output.py b/codeflash/verification/parse_line_profile_test_output.py index 85e6a7460..37af52458 100644 --- a/codeflash/verification/parse_line_profile_test_output.py +++ b/codeflash/verification/parse_line_profile_test_output.py @@ -5,9 +5,15 @@ from __future__ import annotations import inspect import linecache import os +from typing import TYPE_CHECKING, Optional + +import dill as pickle from codeflash.code_utils.tabulate import tabulate +if TYPE_CHECKING: + from pathlib import Path + def show_func( filename: str, start_lineno: int, func_name: str, timings: list[tuple[int, int, float]], unit: float @@ -112,3 +118,17 @@ def show_text_non_python(stats: dict, line_contents: dict[tuple[str, int], str]) ) out_table += "\n" return out_table + + +def parse_line_profile_results(line_profiler_output_file: Optional[Path]) -> tuple[dict, None]: + line_profiler_output_file = line_profiler_output_file.with_suffix(".lprof") + stats_dict: dict = {} + if not line_profiler_output_file.exists(): + return {"timings": {}, "unit": 0, "str_out": ""}, None + with line_profiler_output_file.open("rb") as f: + stats = pickle.load(f) + stats_dict["timings"] = stats.timings + stats_dict["unit"] = stats.unit + str_out = show_text(stats_dict) + stats_dict["str_out"] = str_out + return stats_dict, None diff --git a/tests/test_async_run_and_parse_tests.py b/tests/test_async_run_and_parse_tests.py index e9d85bf68..1777a1c73 100644 --- a/tests/test_async_run_and_parse_tests.py +++ b/tests/test_async_run_and_parse_tests.py @@ -805,7 +805,6 @@ def test_sync_sort(): os.chdir(run_cwd) success, instrumented_test = inject_profiling_into_existing_test( - test_code, test_path, [CodePosition(6, 13), CodePosition(10, 13)], # Lines where sync_sorter is called func, diff --git a/tests/test_inject_profiling_used_frameworks.py b/tests/test_inject_profiling_used_frameworks.py index cde35b62d..826be09c8 100644 --- a/tests/test_inject_profiling_used_frameworks.py +++ b/tests/test_inject_profiling_used_frameworks.py @@ -1105,7 +1105,6 @@ def test_my_function(): func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py")) success, instrumented_code = inject_profiling_into_existing_test( - test_string=code, test_path=test_file, call_positions=[CodePosition(4, 13)], function_to_optimize=func, @@ -1132,7 +1131,6 @@ def test_my_function(): func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py")) success, instrumented_code = inject_profiling_into_existing_test( - test_string=code, test_path=test_file, call_positions=[CodePosition(5, 13)], function_to_optimize=func, @@ -1159,7 +1157,6 @@ def test_my_function(): func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py")) success, instrumented_code = inject_profiling_into_existing_test( - test_string=code, test_path=test_file, call_positions=[CodePosition(5, 13)], function_to_optimize=func, @@ -1186,7 +1183,6 @@ def test_my_function(): func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py")) success, instrumented_code = inject_profiling_into_existing_test( - test_string=code, test_path=test_file, call_positions=[CodePosition(5, 13)], function_to_optimize=func, @@ -1213,7 +1209,6 @@ def test_my_function(): func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py")) success, instrumented_code = inject_profiling_into_existing_test( - test_string=code, test_path=test_file, call_positions=[CodePosition(5, 13)], function_to_optimize=func, @@ -1240,7 +1235,6 @@ def test_my_function(): func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py")) success, instrumented_code = inject_profiling_into_existing_test( - test_string=code, test_path=test_file, call_positions=[CodePosition(5, 13)], function_to_optimize=func, @@ -1267,7 +1261,6 @@ def test_my_function(): func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py")) success, instrumented_code = inject_profiling_into_existing_test( - test_string=code, test_path=test_file, call_positions=[CodePosition(5, 13)], function_to_optimize=func, @@ -1294,7 +1287,6 @@ def test_my_function(): func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py")) success, instrumented_code = inject_profiling_into_existing_test( - test_string=code, test_path=test_file, call_positions=[CodePosition(5, 13)], function_to_optimize=func, @@ -1322,7 +1314,6 @@ def test_my_function(): func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py")) success, instrumented_code = inject_profiling_into_existing_test( - test_string=code, test_path=test_file, call_positions=[CodePosition(6, 13)], function_to_optimize=func, @@ -1351,7 +1342,6 @@ def test_my_function(): func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py")) success, instrumented_code = inject_profiling_into_existing_test( - test_string=code, test_path=test_file, call_positions=[CodePosition(7, 13)], function_to_optimize=func, @@ -1386,7 +1376,6 @@ def test_my_function(): func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py")) success, instrumented_code = inject_profiling_into_existing_test( - test_string=code, test_path=test_file, call_positions=[CodePosition(4, 13)], function_to_optimize=func, @@ -1413,7 +1402,6 @@ def test_my_function(): func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py")) success, instrumented_code = inject_profiling_into_existing_test( - test_string=code, test_path=test_file, call_positions=[CodePosition(5, 13)], function_to_optimize=func, @@ -1440,7 +1428,6 @@ def test_my_function(): func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py")) success, instrumented_code = inject_profiling_into_existing_test( - test_string=code, test_path=test_file, call_positions=[CodePosition(5, 13)], function_to_optimize=func, @@ -1467,7 +1454,6 @@ def test_my_function(): func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py")) success, instrumented_code = inject_profiling_into_existing_test( - test_string=code, test_path=test_file, call_positions=[CodePosition(5, 13)], function_to_optimize=func, @@ -1496,7 +1482,6 @@ def test_my_function(): func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py")) success, instrumented_code = inject_profiling_into_existing_test( - test_string=code, test_path=test_file, call_positions=[CodePosition(7, 13)], function_to_optimize=func, diff --git a/tests/test_pickle_patcher.py b/tests/test_pickle_patcher.py index 9d05da9d8..804ff137b 100644 --- a/tests/test_pickle_patcher.py +++ b/tests/test_pickle_patcher.py @@ -349,7 +349,7 @@ def test_run_and_parse_picklepatch() -> None: run_cwd = project_root os.chdir(run_cwd) success, new_test = inject_profiling_into_existing_test( - original_replay_test_code, replay_test_path, [CodePosition(17, 15)], func, project_root, mode=TestingMode.BEHAVIOR + replay_test_path, [CodePosition(17, 15)], func, project_root, mode=TestingMode.BEHAVIOR ) os.chdir(original_cwd) assert success @@ -443,7 +443,7 @@ def bubble_sort_with_unused_socket(data_container): function_name="bubble_sort_with_used_socket", parents=[], file_path=Path(fto_used_socket_path) ) success, new_test = inject_profiling_into_existing_test( - original_replay_test_code, replay_test_path, [CodePosition(23, 15)], func, project_root, mode=TestingMode.BEHAVIOR + replay_test_path, [CodePosition(23, 15)], func, project_root, mode=TestingMode.BEHAVIOR ) os.chdir(original_cwd) assert success