Skip to content

Commit 3f1cbef

Browse files
committed
wip need to do in a single loop
1 parent 61094f7 commit 3f1cbef

File tree

2 files changed

+56
-57
lines changed

2 files changed

+56
-57
lines changed

codeflash/models/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ def report_to_tree(report: dict[TestType, dict[str, int]], title: str) -> Tree:
557557

558558
def usable_runtime_data_by_test_case(self) -> dict[InvocationId, list[int]]:
559559
# Efficient single traversal, directly accumulating into a dict.
560+
# can track mins here and only sums can be return in total_passed_runtime
560561
by_id: dict[InvocationId, list[int]] = {}
561562
for result in self.test_results:
562563
if result.did_pass:

codeflash/result/create_pr.py

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -31,64 +31,62 @@ def existing_tests_source_for(
3131
optimized_test_results: Optional[TestResults] = None,
3232
) -> str:
3333
test_files = function_to_tests.get(function_qualified_name_with_modules_from_root)
34+
if not test_files:
35+
return ""
3436
existing_tests_unique = set()
35-
36-
if test_files:
37-
# Group test cases by test file
38-
test_files_grouped = {}
39-
for test_file in test_files:
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)
37+
# a lot of loops, need to do in a single loop
38+
#original_runtime_by_test = original_test_results.usable_runtime_data_by_test_case()
39+
#optimized_runtime_by_test = optimized_test_results.usable_runtime_data_by_test_case()
40+
# Group test cases by test file
41+
test_files_grouped = {}
42+
for test_file in test_files:
43+
file_path = Path(test_file.tests_in_file.test_file)
44+
relative_path = str(file_path.relative_to(tests_root))
45+
46+
if relative_path not in test_files_grouped:
47+
test_files_grouped[relative_path] = []
48+
test_files_grouped.setdefault(relative_path,[]).append(test_file)
49+
50+
# Create detailed report for each test file
51+
# for relative_path, tests_in_file in sorted(test_files_grouped.items()):
52+
file_line = f"- {relative_path}"
53+
54+
# Add test case details with timing information if available
55+
#if original_test_results and optimized_test_results:
56+
test_case_details = []
57+
# Collect test function names for this file
58+
test_functions_in_file = {test_file.tests_in_file.test_function for test_file in tests_in_file}
59+
60+
# Create timing report for each test function
61+
for test_function_name in sorted(test_functions_in_file):
62+
# Find matching runtime data
63+
original_runtimes = []
64+
optimized_runtimes = []
65+
66+
for invocation_id, runtimes in original_runtime_by_test.items():
67+
if invocation_id.test_function_name == test_function_name:
68+
original_runtimes.extend(runtimes)
69+
70+
for invocation_id, runtimes in optimized_runtime_by_test.items():
71+
if invocation_id.test_function_name == test_function_name:
72+
optimized_runtimes.extend(runtimes)
73+
74+
if original_runtimes and optimized_runtimes:
75+
# Use minimum timing like the generated tests function does
76+
original_time = min(original_runtimes)
77+
optimized_time = min(optimized_runtimes)
78+
79+
from codeflash.code_utils.time_utils import format_time
80+
81+
original_str = format_time(original_time)
82+
optimized_str = format_time(optimized_time)
83+
84+
test_case_details.append(f" - {test_function_name}: {original_str} -> {optimized_str}")
85+
86+
if test_case_details:
87+
file_line += "\n" + "\n".join(test_case_details)
88+
89+
existing_tests_unique.add(file_line)
9290

9391
return "\n".join(sorted(existing_tests_unique))
9492

0 commit comments

Comments
 (0)