Skip to content

Commit 2abbfba

Browse files
committed
moving line profiler instrument tests in a separate file
1 parent a628dae commit 2abbfba

File tree

2 files changed

+165
-159
lines changed

2 files changed

+165
-159
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import os
2+
from pathlib import Path
3+
4+
from codeflash.code_utils.line_profile_utils import add_decorator_imports
5+
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
6+
from codeflash.models.models import CodeOptimizationContext
7+
from codeflash.optimization.function_optimizer import FunctionOptimizer
8+
from codeflash.verification.verification_utils import TestConfig
9+
10+
11+
def test_add_decorator_imports_helper_in_class():
12+
code_path = (Path(__file__).parent.resolve() / "../code_to_optimize/bubble_sort_classmethod.py").resolve()
13+
tests_root = Path(__file__).parent.resolve() / "../code_to_optimize/tests/pytest/"
14+
project_root_path = (Path(__file__).parent / "..").resolve()
15+
original_cwd = Path.cwd()
16+
run_cwd = Path(__file__).parent.parent.resolve()
17+
test_config = TestConfig(
18+
tests_root=tests_root,
19+
tests_project_rootdir=project_root_path,
20+
project_root_path=project_root_path,
21+
test_framework="pytest",
22+
pytest_cmd="pytest",
23+
)
24+
func = FunctionToOptimize(function_name="sort_classmethod", parents=[], file_path=code_path)
25+
func_optimizer = FunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
26+
os.chdir(run_cwd)
27+
#func_optimizer = pass
28+
try:
29+
ctx_result = func_optimizer.get_code_optimization_context()
30+
code_context: CodeOptimizationContext = ctx_result.unwrap()
31+
original_helper_code: dict[Path, str] = {}
32+
helper_function_paths = {hf.file_path for hf in code_context.helper_functions}
33+
for helper_function_path in helper_function_paths:
34+
with helper_function_path.open(encoding="utf8") as f:
35+
helper_code = f.read()
36+
original_helper_code[helper_function_path] = helper_code
37+
line_profiler_output_file = add_decorator_imports(
38+
func_optimizer.function_to_optimize, code_context)
39+
expected_code_main = f"""from line_profiler import profile as codeflash_line_profile
40+
codeflash_line_profile.enable(output_prefix='{line_profiler_output_file}')
41+
42+
from code_to_optimize.bubble_sort_in_class import BubbleSortClass
43+
44+
45+
@codeflash_line_profile
46+
def sort_classmethod(x):
47+
y = BubbleSortClass()
48+
return y.sorter(x)
49+
"""
50+
expected_code_helper = """from line_profiler import profile as codeflash_line_profile
51+
52+
53+
def hi():
54+
pass
55+
56+
57+
class BubbleSortClass:
58+
def __init__(self):
59+
pass
60+
61+
@codeflash_line_profile
62+
def sorter(self, arr):
63+
n = len(arr)
64+
for i in range(n):
65+
for j in range(0, n - i - 1):
66+
if arr[j] > arr[j + 1]:
67+
arr[j], arr[j + 1] = arr[j + 1], arr[j]
68+
return arr
69+
70+
def helper(self, arr, j):
71+
return arr[j] > arr[j + 1]
72+
"""
73+
assert code_path.read_text("utf-8") == expected_code_main
74+
assert code_context.helper_functions[0].file_path.read_text("utf-8") == expected_code_helper
75+
finally:
76+
func_optimizer.write_code_and_helpers(
77+
func_optimizer.function_to_optimize_source_code, original_helper_code, func_optimizer.function_to_optimize.file_path
78+
)
79+
80+
def test_add_decorator_imports_helper_in_nested_class():
81+
code_path = (Path(__file__).parent.resolve() / "../code_to_optimize/bubble_sort_nested_classmethod.py").resolve()
82+
tests_root = Path(__file__).parent.resolve() / "../code_to_optimize/tests/pytest/"
83+
project_root_path = (Path(__file__).parent / "..").resolve()
84+
original_cwd = Path.cwd()
85+
run_cwd = Path(__file__).parent.parent.resolve()
86+
test_config = TestConfig(
87+
tests_root=tests_root,
88+
tests_project_rootdir=project_root_path,
89+
project_root_path=project_root_path,
90+
test_framework="pytest",
91+
pytest_cmd="pytest",
92+
)
93+
func = FunctionToOptimize(function_name="sort_classmethod", parents=[], file_path=code_path)
94+
func_optimizer = FunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
95+
os.chdir(run_cwd)
96+
#func_optimizer = pass
97+
try:
98+
ctx_result = func_optimizer.get_code_optimization_context()
99+
code_context: CodeOptimizationContext = ctx_result.unwrap()
100+
original_helper_code: dict[Path, str] = {}
101+
helper_function_paths = {hf.file_path for hf in code_context.helper_functions}
102+
for helper_function_path in helper_function_paths:
103+
with helper_function_path.open(encoding="utf8") as f:
104+
helper_code = f.read()
105+
original_helper_code[helper_function_path] = helper_code
106+
line_profiler_output_file = add_decorator_imports(
107+
func_optimizer.function_to_optimize, code_context)
108+
expected_code_main = f"""from line_profiler import profile as codeflash_line_profile
109+
codeflash_line_profile.enable(output_prefix='{line_profiler_output_file}')
110+
111+
from code_to_optimize.bubble_sort_in_nested_class import WrapperClass
112+
113+
114+
@codeflash_line_profile
115+
def sort_classmethod(x):
116+
y = WrapperClass.BubbleSortClass()
117+
return y.sorter(x)
118+
"""
119+
expected_code_helper = """from line_profiler import profile as codeflash_line_profile
120+
121+
122+
def hi():
123+
pass
124+
125+
126+
class WrapperClass:
127+
def __init__(self):
128+
pass
129+
130+
class BubbleSortClass:
131+
def __init__(self):
132+
pass
133+
134+
@codeflash_line_profile
135+
def sorter(self, arr):
136+
def inner_helper(arr, j):
137+
return arr[j] > arr[j + 1]
138+
139+
for i in range(len(arr)):
140+
for j in range(len(arr) - 1):
141+
if arr[j] > arr[j + 1]:
142+
temp = arr[j]
143+
arr[j] = arr[j + 1]
144+
arr[j + 1] = temp
145+
return arr
146+
147+
def helper(self, arr, j):
148+
return arr[j] > arr[j + 1]
149+
"""
150+
assert code_path.read_text("utf-8") == expected_code_main
151+
assert code_context.helper_functions[0].file_path.read_text("utf-8") == expected_code_helper
152+
finally:
153+
func_optimizer.write_code_and_helpers(
154+
func_optimizer.function_to_optimize_source_code, original_helper_code, func_optimizer.function_to_optimize.file_path
155+
)

tests/test_instrument_tests.py

Lines changed: 10 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
from codeflash.code_utils.line_profile_utils import add_decorator_imports
1616
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
1717
from codeflash.models.models import (
18+
CodeOptimizationContext,
1819
CodePosition,
1920
FunctionParent,
2021
TestFile,
2122
TestFiles,
2223
TestingMode,
2324
TestsInFile,
24-
TestType, CodeOptimizationContext,
25+
TestType,
2526
)
2627
from codeflash.optimization.function_optimizer import FunctionOptimizer
2728
from codeflash.verification.verification_utils import TestConfig
@@ -518,8 +519,8 @@ def test_sort():
518519
testing_time=0.1,
519520
line_profiler_output_file = line_profiler_output_file
520521
)
521-
tmp_lpr = list(line_profile_results['timings'].keys())
522-
assert len(tmp_lpr) == 1 and line_profile_results['timings'][tmp_lpr[0]][0][1]==2
522+
tmp_lpr = list(line_profile_results["timings"].keys())
523+
assert len(tmp_lpr) == 1 and line_profile_results["timings"][tmp_lpr[0]][0][1]==2
523524
finally:
524525
if computed_fn_opt:
525526
func_optimizer.write_code_and_helpers(
@@ -771,8 +772,8 @@ def test_sort_parametrized(input, expected_output):
771772
testing_time=0.1,
772773
line_profiler_output_file = line_profiler_output_file
773774
)
774-
tmp_lpr = list(line_profile_results['timings'].keys())
775-
assert len(tmp_lpr) == 1 and line_profile_results['timings'][tmp_lpr[0]][0][1]==3
775+
tmp_lpr = list(line_profile_results["timings"].keys())
776+
assert len(tmp_lpr) == 1 and line_profile_results["timings"][tmp_lpr[0]][0][1]==3
776777
finally:
777778
if computed_fn_opt:
778779
func_optimizer.write_code_and_helpers(
@@ -1110,8 +1111,8 @@ def test_sort_parametrized_loop(input, expected_output):
11101111
testing_time=0.1,
11111112
line_profiler_output_file = line_profiler_output_file
11121113
)
1113-
tmp_lpr = list(line_profile_results['timings'].keys())
1114-
assert len(tmp_lpr) == 1 and line_profile_results['timings'][tmp_lpr[0]][0][1]==6
1114+
tmp_lpr = list(line_profile_results["timings"].keys())
1115+
assert len(tmp_lpr) == 1 and line_profile_results["timings"][tmp_lpr[0]][0][1]==6
11151116
finally:
11161117
if computed_fn_opt:
11171118
func_optimizer.write_code_and_helpers(
@@ -1383,8 +1384,8 @@ def test_sort():
13831384
testing_time=0.1,
13841385
line_profiler_output_file = line_profiler_output_file
13851386
)
1386-
tmp_lpr = list(line_profile_results['timings'].keys())
1387-
assert len(tmp_lpr) == 1 and line_profile_results['timings'][tmp_lpr[0]][0][1]==3
1387+
tmp_lpr = list(line_profile_results["timings"].keys())
1388+
assert len(tmp_lpr) == 1 and line_profile_results["timings"][tmp_lpr[0]][0][1]==3
13881389
finally:
13891390
if computed_fn_opt is True:
13901391
func_optimizer.write_code_and_helpers(
@@ -3139,153 +3140,3 @@ def test_sleepfunc_sequence_short(self, n, expected_total_sleep_time):
31393140

31403141
finally:
31413142
test_path.unlink(missing_ok=True)
3142-
3143-
def test_add_decorator_imports_helper_in_class():
3144-
code_path = (Path(__file__).parent.resolve() / "../code_to_optimize/bubble_sort_classmethod.py").resolve()
3145-
tests_root = Path(__file__).parent.resolve() / "../code_to_optimize/tests/pytest/"
3146-
project_root_path = (Path(__file__).parent / "..").resolve()
3147-
original_cwd = Path.cwd()
3148-
run_cwd = Path(__file__).parent.parent.resolve()
3149-
test_config = TestConfig(
3150-
tests_root=tests_root,
3151-
tests_project_rootdir=project_root_path,
3152-
project_root_path=project_root_path,
3153-
test_framework="pytest",
3154-
pytest_cmd="pytest",
3155-
)
3156-
func = FunctionToOptimize(function_name="sort_classmethod", parents=[], file_path=code_path)
3157-
func_optimizer = FunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
3158-
os.chdir(run_cwd)
3159-
#func_optimizer = pass
3160-
try:
3161-
ctx_result = func_optimizer.get_code_optimization_context()
3162-
code_context: CodeOptimizationContext = ctx_result.unwrap()
3163-
original_helper_code: dict[Path, str] = {}
3164-
helper_function_paths = {hf.file_path for hf in code_context.helper_functions}
3165-
for helper_function_path in helper_function_paths:
3166-
with helper_function_path.open(encoding="utf8") as f:
3167-
helper_code = f.read()
3168-
original_helper_code[helper_function_path] = helper_code
3169-
computed_fn_opt = True
3170-
line_profiler_output_file = add_decorator_imports(
3171-
func_optimizer.function_to_optimize, code_context)
3172-
expected_code_main = f"""from line_profiler import profile as codeflash_line_profile
3173-
codeflash_line_profile.enable(output_prefix='{line_profiler_output_file}')
3174-
3175-
from code_to_optimize.bubble_sort_in_class import BubbleSortClass
3176-
3177-
3178-
@codeflash_line_profile
3179-
def sort_classmethod(x):
3180-
y = BubbleSortClass()
3181-
return y.sorter(x)
3182-
"""
3183-
expected_code_helper = """from line_profiler import profile as codeflash_line_profile
3184-
3185-
3186-
def hi():
3187-
pass
3188-
3189-
3190-
class BubbleSortClass:
3191-
def __init__(self):
3192-
pass
3193-
3194-
@codeflash_line_profile
3195-
def sorter(self, arr):
3196-
n = len(arr)
3197-
for i in range(n):
3198-
for j in range(0, n - i - 1):
3199-
if arr[j] > arr[j + 1]:
3200-
arr[j], arr[j + 1] = arr[j + 1], arr[j]
3201-
return arr
3202-
3203-
def helper(self, arr, j):
3204-
return arr[j] > arr[j + 1]
3205-
"""
3206-
assert code_path.read_text("utf-8") == expected_code_main
3207-
assert code_context.helper_functions[0].file_path.read_text("utf-8") == expected_code_helper
3208-
finally:
3209-
#if computed_fn_opt:
3210-
func_optimizer.write_code_and_helpers(
3211-
func_optimizer.function_to_optimize_source_code, original_helper_code, func_optimizer.function_to_optimize.file_path
3212-
)
3213-
3214-
def test_add_decorator_imports_helper_in_nested_class():
3215-
code_path = (Path(__file__).parent.resolve() / "../code_to_optimize/bubble_sort_nested_classmethod.py").resolve()
3216-
tests_root = Path(__file__).parent.resolve() / "../code_to_optimize/tests/pytest/"
3217-
project_root_path = (Path(__file__).parent / "..").resolve()
3218-
original_cwd = Path.cwd()
3219-
run_cwd = Path(__file__).parent.parent.resolve()
3220-
test_config = TestConfig(
3221-
tests_root=tests_root,
3222-
tests_project_rootdir=project_root_path,
3223-
project_root_path=project_root_path,
3224-
test_framework="pytest",
3225-
pytest_cmd="pytest",
3226-
)
3227-
func = FunctionToOptimize(function_name="sort_classmethod", parents=[], file_path=code_path)
3228-
func_optimizer = FunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
3229-
os.chdir(run_cwd)
3230-
#func_optimizer = pass
3231-
try:
3232-
ctx_result = func_optimizer.get_code_optimization_context()
3233-
code_context: CodeOptimizationContext = ctx_result.unwrap()
3234-
original_helper_code: dict[Path, str] = {}
3235-
helper_function_paths = {hf.file_path for hf in code_context.helper_functions}
3236-
for helper_function_path in helper_function_paths:
3237-
with helper_function_path.open(encoding="utf8") as f:
3238-
helper_code = f.read()
3239-
original_helper_code[helper_function_path] = helper_code
3240-
computed_fn_opt = True
3241-
line_profiler_output_file = add_decorator_imports(
3242-
func_optimizer.function_to_optimize, code_context)
3243-
expected_code_main = f"""from line_profiler import profile as codeflash_line_profile
3244-
codeflash_line_profile.enable(output_prefix='{line_profiler_output_file}')
3245-
3246-
from code_to_optimize.bubble_sort_in_nested_class import WrapperClass
3247-
3248-
3249-
@codeflash_line_profile
3250-
def sort_classmethod(x):
3251-
y = WrapperClass.BubbleSortClass()
3252-
return y.sorter(x)
3253-
"""
3254-
expected_code_helper = """from line_profiler import profile as codeflash_line_profile
3255-
3256-
3257-
def hi():
3258-
pass
3259-
3260-
3261-
class WrapperClass:
3262-
def __init__(self):
3263-
pass
3264-
3265-
class BubbleSortClass:
3266-
def __init__(self):
3267-
pass
3268-
3269-
@codeflash_line_profile
3270-
def sorter(self, arr):
3271-
def inner_helper(arr, j):
3272-
return arr[j] > arr[j + 1]
3273-
3274-
for i in range(len(arr)):
3275-
for j in range(len(arr) - 1):
3276-
if arr[j] > arr[j + 1]:
3277-
temp = arr[j]
3278-
arr[j] = arr[j + 1]
3279-
arr[j + 1] = temp
3280-
return arr
3281-
3282-
def helper(self, arr, j):
3283-
return arr[j] > arr[j + 1]
3284-
"""
3285-
assert code_path.read_text("utf-8") == expected_code_main
3286-
assert code_context.helper_functions[0].file_path.read_text("utf-8") == expected_code_helper
3287-
finally:
3288-
#if computed_fn_opt:
3289-
func_optimizer.write_code_and_helpers(
3290-
func_optimizer.function_to_optimize_source_code, original_helper_code, func_optimizer.function_to_optimize.file_path
3291-
)

0 commit comments

Comments
 (0)