Skip to content

Commit fec0627

Browse files
committed
first version that does not work correctly
1 parent 55a48eb commit fec0627

File tree

3 files changed

+711
-8
lines changed

3 files changed

+711
-8
lines changed

codeflash/optimization/function_optimizer.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,6 @@ def optimize_function(self) -> Result[BestOptimization, str]: # noqa: PLR0911
341341
optimized_function=best_optimization.candidate.source_code,
342342
)
343343

344-
existing_tests = existing_tests_source_for(
345-
self.function_to_optimize.qualified_name_with_modules_from_root(self.project_root),
346-
function_to_all_tests,
347-
tests_root=self.test_cfg.tests_root,
348-
)
349-
350344
original_code_combined = original_helper_code.copy()
351345
original_code_combined[explanation.file_path] = self.function_to_optimize_source_code
352346
new_code_combined = new_helper_code.copy()
@@ -369,6 +363,13 @@ def optimize_function(self) -> Result[BestOptimization, str]: # noqa: PLR0911
369363
generated_tests_str = "\n\n".join(
370364
[test.generated_original_test_source for test in generated_tests.generated_tests]
371365
)
366+
existing_tests = existing_tests_source_for(
367+
self.function_to_optimize.qualified_name_with_modules_from_root(self.project_root),
368+
function_to_all_tests,
369+
tests_root=self.test_cfg.tests_root,
370+
original_test_results=original_code_baseline.benchmarking_test_results,
371+
optimized_test_results=best_optimization.winning_benchmarking_test_results,
372+
)
372373
if concolic_test_str:
373374
generated_tests_str += "\n\n" + concolic_test_str
374375

codeflash/result/create_pr.py

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,77 @@
1919
from codeflash.github.PrComment import FileDiffContent, PrComment
2020

2121
if TYPE_CHECKING:
22-
from codeflash.models.models import FunctionCalledInTest
22+
from codeflash.models.models import FunctionCalledInTest, TestResults
2323
from codeflash.result.explanation import Explanation
2424

2525

2626
def existing_tests_source_for(
2727
function_qualified_name_with_modules_from_root: str,
2828
function_to_tests: dict[str, set[FunctionCalledInTest]],
2929
tests_root: Path,
30+
original_test_results: Optional[TestResults] = None,
31+
optimized_test_results: Optional[TestResults] = None,
3032
) -> str:
3133
test_files = function_to_tests.get(function_qualified_name_with_modules_from_root)
3234
existing_tests_unique = set()
35+
3336
if test_files:
37+
# Group test cases by test file
38+
test_files_grouped = {}
3439
for test_file in test_files:
35-
existing_tests_unique.add("- " + str(Path(test_file.tests_in_file.test_file).relative_to(tests_root)))
40+
file_path = Path(test_file.tests_in_file.test_file)
41+
relative_path = str(file_path.relative_to(tests_root))
42+
43+
if relative_path not in test_files_grouped:
44+
test_files_grouped[relative_path] = []
45+
test_files_grouped[relative_path].append(test_file)
46+
47+
# Create detailed report for each test file
48+
for relative_path, tests_in_file in sorted(test_files_grouped.items()):
49+
file_line = f"- {relative_path}"
50+
51+
# Add test case details with timing information if available
52+
if original_test_results and optimized_test_results:
53+
test_case_details = []
54+
55+
# Use the same pattern as add_runtime_comments_to_generated_tests
56+
original_runtime_by_test = original_test_results.usable_runtime_data_by_test_case()
57+
optimized_runtime_by_test = optimized_test_results.usable_runtime_data_by_test_case()
58+
59+
# Collect test function names for this file
60+
test_functions_in_file = {test_file.tests_in_file.test_function for test_file in tests_in_file}
61+
62+
# Create timing report for each test function
63+
for test_function_name in sorted(test_functions_in_file):
64+
# Find matching runtime data
65+
original_runtimes = []
66+
optimized_runtimes = []
67+
68+
for invocation_id, runtimes in original_runtime_by_test.items():
69+
if invocation_id.test_function_name == test_function_name:
70+
original_runtimes.extend(runtimes)
71+
72+
for invocation_id, runtimes in optimized_runtime_by_test.items():
73+
if invocation_id.test_function_name == test_function_name:
74+
optimized_runtimes.extend(runtimes)
75+
76+
if original_runtimes and optimized_runtimes:
77+
# Use minimum timing like the generated tests function does
78+
original_time = min(original_runtimes)
79+
optimized_time = min(optimized_runtimes)
80+
81+
from codeflash.code_utils.time_utils import format_time
82+
83+
original_str = format_time(original_time)
84+
optimized_str = format_time(optimized_time)
85+
86+
test_case_details.append(f" - {test_function_name}: {original_str} -> {optimized_str}")
87+
88+
if test_case_details:
89+
file_line += "\n" + "\n".join(test_case_details)
90+
91+
existing_tests_unique.add(file_line)
92+
3693
return "\n".join(sorted(existing_tests_unique))
3794

3895

0 commit comments

Comments
 (0)