Skip to content

Commit 4806815

Browse files
committed
ready to merge
1 parent 9ad40e2 commit 4806815

File tree

6 files changed

+637
-87
lines changed

6 files changed

+637
-87
lines changed

codeflash/code_utils/edit_generated_tests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import libcst as cst
66

77
from codeflash.cli_cmds.console import logger
8-
from codeflash.code_utils.time_utils import format_time
8+
from codeflash.code_utils.time_utils import format_perf, format_time
99
from codeflash.models.models import GeneratedTests, GeneratedTestsList, InvocationId
1010
from codeflash.result.critic import performance_gain
1111
from codeflash.verification.verification_utils import TestConfig
@@ -131,11 +131,11 @@ def leave_SimpleStatementLine(
131131
if matching_original_times and matching_optimized_times:
132132
original_time = min(matching_original_times)
133133
optimized_time = min(matching_optimized_times)
134-
perf_gain = (
134+
perf_gain = format_perf(
135135
performance_gain(original_runtime_ns=original_time, optimized_runtime_ns=optimized_time) * 100
136136
)
137137
# Create the runtime comment
138-
comment_text = f"# {format_time(original_time)} -> {format_time(optimized_time)} ({perf_gain:.2f}%)"
138+
comment_text = f"# {format_time(original_time)} -> {format_time(optimized_time)} ({perf_gain}%)"
139139

140140
# Add comment to the trailing whitespace
141141
new_trailing_whitespace = cst.TrailingWhitespace(

codeflash/code_utils/time_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,15 @@ def format_time(nanoseconds: int) -> str:
8585

8686
# This should never be reached, but included for completeness
8787
return f"{nanoseconds}ns"
88+
89+
90+
def format_perf(percentage: float) -> str:
91+
"""Format percentage into a human-readable string with 3 significant digits when needed."""
92+
percentage_abs = abs(percentage)
93+
if percentage_abs >= 100:
94+
return f"{percentage:.0f}"
95+
if percentage_abs >= 10:
96+
return f"{percentage:.1f}"
97+
if percentage_abs >= 1:
98+
return f"{percentage:.2f}"
99+
return f"{percentage:.3f}"

codeflash/result/create_pr.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
)
1919
from codeflash.code_utils.github_utils import github_pr_url
2020
from codeflash.code_utils.tabulate import tabulate
21-
from codeflash.code_utils.time_utils import format_time
21+
from codeflash.code_utils.time_utils import format_perf, format_time
2222
from codeflash.github.PrComment import FileDiffContent, PrComment
2323
from codeflash.result.critic import performance_gain
2424

@@ -40,7 +40,7 @@ def existing_tests_source_for(
4040
return ""
4141
output: str = ""
4242
rows = []
43-
headers = ["Test File::Test Function", "Original ⏱️", "Optimized ⏱️", "Improvement"]
43+
headers = ["Test File::Test Function", "Original ⏱️", "Optimized ⏱️", "Speedup"]
4444
tests_root = test_cfg.tests_root
4545
module_root = test_cfg.project_root_path
4646
rel_tests_root = tests_root.relative_to(module_root)
@@ -84,23 +84,17 @@ def existing_tests_source_for(
8484
].keys() # both will have the same keys as some default values are assigned in the previous loop
8585
for qualified_name in sorted(all_qualified_names):
8686
# if not present in optimized output nan
87-
if optimized_tests_to_runtimes[filename][qualified_name] == 0:
88-
print_optimized_runtime = "NaN"
89-
else:
90-
print_optimized_runtime = format_time(optimized_tests_to_runtimes[filename][qualified_name])
91-
if original_tests_to_runtimes[filename][qualified_name] == 0:
92-
print_original_runtime = "NaN"
93-
else:
94-
print_original_runtime = format_time(original_tests_to_runtimes[filename][qualified_name])
9587
if (
9688
original_tests_to_runtimes[filename][qualified_name] != 0
9789
and optimized_tests_to_runtimes[filename][qualified_name] != 0
9890
):
91+
print_optimized_runtime = format_time(optimized_tests_to_runtimes[filename][qualified_name])
92+
print_original_runtime = format_time(original_tests_to_runtimes[filename][qualified_name])
9993
greater = (
10094
optimized_tests_to_runtimes[filename][qualified_name]
10195
> original_tests_to_runtimes[filename][qualified_name]
10296
)
103-
perf_gain = (
97+
perf_gain = format_perf(
10498
performance_gain(
10599
original_runtime_ns=original_tests_to_runtimes[filename][qualified_name],
106100
optimized_runtime_ns=optimized_tests_to_runtimes[filename][qualified_name],
@@ -113,7 +107,7 @@ def existing_tests_source_for(
113107
f"`{filename}::{qualified_name}`",
114108
f"{print_original_runtime}",
115109
f"{print_optimized_runtime}",
116-
f"⚠️{perf_gain:.2f}%",
110+
f"⚠️{perf_gain}%",
117111
]
118112
)
119113
else:
@@ -122,14 +116,9 @@ def existing_tests_source_for(
122116
f"`{filename}::{qualified_name}`",
123117
f"{print_original_runtime}",
124118
f"{print_optimized_runtime}",
125-
f"✅{perf_gain:.2f}%",
119+
f"✅{perf_gain}%",
126120
]
127121
)
128-
else:
129-
# one of them is NaN
130-
rows.append(
131-
[f"`{filename}::{qualified_name}`", f"{print_original_runtime}", f"{print_optimized_runtime}", "❌"]
132-
)
133122
output += tabulate( # type: ignore[no-untyped-call]
134123
headers=headers, tabular_data=rows, tablefmt="pipe", colglobalalign=None, preserve_whitespace=True
135124
)

0 commit comments

Comments
 (0)