Skip to content

Commit 0cf2ceb

Browse files
authored
Merge branch 'main' into chore-send-email
2 parents 352a176 + c288419 commit 0cf2ceb

16 files changed

+1815
-1397
lines changed

codeflash/cli_cmds/cmd_init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ def install_github_app() -> None:
981981
)
982982
click.launch("https://github.com/apps/codeflash-ai/installations/select_target")
983983
click.prompt(
984-
f"Press Enter once you've finished installing the github app from https://github.com/apps/codeflash-ai/installations/select_target{LF}",
984+
f"Press Enter once you've finished installing the github app from https://github.com/apps/codeflash-ai/installations/select_target{LF}",
985985
default="",
986986
type=click.STRING,
987987
prompt_suffix="",

codeflash/code_utils/edit_generated_tests.py

Lines changed: 152 additions & 189 deletions
Large diffs are not rendered by default.

codeflash/code_utils/time_utils.py

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import datetime as dt
24
import re
35

@@ -58,42 +60,27 @@ def format_time(nanoseconds: int) -> str:
5860
raise TypeError("Input must be an integer.")
5961
if nanoseconds < 0:
6062
raise ValueError("Input must be a positive integer.")
61-
conversions = [(1_000_000_000, "s"), (1_000_000, "ms"), (1_000, "μs"), (1, "ns")]
6263

63-
# Handle nanoseconds case directly (no decimal formatting needed)
6464
if nanoseconds < 1_000:
6565
return f"{nanoseconds}ns"
66-
67-
# Find appropriate unit
68-
for divisor, unit in conversions:
69-
if nanoseconds >= divisor:
70-
value = nanoseconds / divisor
71-
int_value = nanoseconds // divisor
72-
73-
# Use integer formatting for values >= 100
74-
if int_value >= 100:
75-
formatted_value = f"{int_value:.0f}"
76-
# Format with precision for 3 significant digits
77-
elif value >= 100:
78-
formatted_value = f"{value:.0f}"
79-
elif value >= 10:
80-
formatted_value = f"{value:.1f}"
81-
else:
82-
formatted_value = f"{value:.2f}"
83-
84-
return f"{formatted_value}{unit}"
85-
86-
# This should never be reached, but included for completeness
87-
return f"{nanoseconds}ns"
66+
if nanoseconds < 1_000_000:
67+
value = nanoseconds / 1_000
68+
return f"{value:.2f}μs" if value < 10 else (f"{value:.1f}μs" if value < 100 else f"{int(value)}μs")
69+
if nanoseconds < 1_000_000_000:
70+
value = nanoseconds / 1_000_000
71+
return f"{value:.2f}ms" if value < 10 else (f"{value:.1f}ms" if value < 100 else f"{int(value)}ms")
72+
value = nanoseconds / 1_000_000_000
73+
return f"{value:.2f}s" if value < 10 else (f"{value:.1f}s" if value < 100 else f"{int(value)}s")
8874

8975

9076
def format_perf(percentage: float) -> str:
9177
"""Format percentage into a human-readable string with 3 significant digits when needed."""
92-
percentage_abs = abs(percentage)
93-
if percentage_abs >= 100:
78+
# Branch order optimized
79+
abs_perc = abs(percentage)
80+
if abs_perc >= 100:
9481
return f"{percentage:.0f}"
95-
if percentage_abs >= 10:
82+
if abs_perc >= 10:
9683
return f"{percentage:.1f}"
97-
if percentage_abs >= 1:
84+
if abs_perc >= 1:
9885
return f"{percentage:.2f}"
9986
return f"{percentage:.3f}"

codeflash/discovery/discover_unit_tests.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ def discover_unit_tests(
341341
cfg: TestConfig,
342342
discover_only_these_tests: list[Path] | None = None,
343343
file_to_funcs_to_optimize: dict[Path, list[FunctionToOptimize]] | None = None,
344-
) -> tuple[dict[str, set[FunctionCalledInTest]], int]:
344+
) -> tuple[dict[str, set[FunctionCalledInTest]], int, int]:
345345
framework_strategies: dict[str, Callable] = {"pytest": discover_tests_pytest, "unittest": discover_tests_unittest}
346346
strategy = framework_strategies.get(cfg.test_framework, None)
347347
if not strategy:
@@ -352,8 +352,10 @@ def discover_unit_tests(
352352
functions_to_optimize = None
353353
if file_to_funcs_to_optimize:
354354
functions_to_optimize = [func for funcs_list in file_to_funcs_to_optimize.values() for func in funcs_list]
355-
function_to_tests, num_discovered_tests = strategy(cfg, discover_only_these_tests, functions_to_optimize)
356-
return function_to_tests, num_discovered_tests
355+
function_to_tests, num_discovered_tests, num_discovered_replay_tests = strategy(
356+
cfg, discover_only_these_tests, functions_to_optimize
357+
)
358+
return function_to_tests, num_discovered_tests, num_discovered_replay_tests
357359

358360

359361
def discover_tests_pytest(
@@ -515,6 +517,7 @@ def process_test_files(
515517

516518
function_to_test_map = defaultdict(set)
517519
num_discovered_tests = 0
520+
num_discovered_replay_tests = 0
518521
jedi_project = jedi.Project(path=project_root_path)
519522

520523
with test_files_progress_bar(total=len(file_to_test_map), description="Processing test files") as (
@@ -661,11 +664,14 @@ def process_test_files(
661664
position=CodePosition(line_no=name.line, col_no=name.column),
662665
)
663666
)
667+
if test_func.test_type == TestType.REPLAY_TEST:
668+
num_discovered_replay_tests += 1
669+
664670
num_discovered_tests += 1
665671
except Exception as e:
666672
logger.debug(str(e))
667673
continue
668674

669675
progress.advance(task_id)
670676

671-
return dict(function_to_test_map), num_discovered_tests
677+
return dict(function_to_test_map), num_discovered_tests, num_discovered_replay_tests

codeflash/discovery/functions_to_optimize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def get_all_replay_test_functions(
304304
logger.error("Could not find trace_file_path in replay test files.")
305305
exit_with_message("Could not find trace_file_path in replay test files.")
306306

307-
function_tests, _ = discover_unit_tests(test_cfg, discover_only_these_tests=replay_test)
307+
function_tests, _, _ = discover_unit_tests(test_cfg, discover_only_these_tests=replay_test)
308308
# Get the absolute file paths for each function, excluding class name if present
309309
filtered_valid_functions = defaultdict(list)
310310
file_to_functions_map = defaultdict(list)

codeflash/optimization/function_optimizer.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ def setup_and_establish_baseline(
905905
if self.args.override_fixtures:
906906
restore_conftest(original_conftest_content)
907907
cleanup_paths(paths_to_cleanup)
908-
return Failure("The threshold for test coverage was not met.")
908+
return Failure("The threshold for test confidence was not met.")
909909

910910
return Success(
911911
(
@@ -1016,11 +1016,7 @@ def find_and_process_best_optimization(
10161016
qualified_name = self.function_to_optimize.qualified_name_with_modules_from_root(self.project_root)
10171017
# Add runtime comments to generated tests before creating the PR
10181018
generated_tests = add_runtime_comments_to_generated_tests(
1019-
qualified_name,
1020-
self.test_cfg,
1021-
generated_tests,
1022-
original_runtime_by_test,
1023-
optimized_runtime_by_test,
1019+
generated_tests, original_runtime_by_test, optimized_runtime_by_test
10241020
)
10251021
generated_tests_str = "\n\n".join(
10261022
[test.generated_original_test_source for test in generated_tests.generated_tests]

codeflash/optimization/optimizer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,12 @@ def discover_tests(
227227

228228
console.rule()
229229
start_time = time.time()
230-
function_to_tests, num_discovered_tests = discover_unit_tests(
230+
function_to_tests, num_discovered_tests, num_discovered_replay_tests = discover_unit_tests(
231231
self.test_cfg, file_to_funcs_to_optimize=file_to_funcs_to_optimize
232232
)
233233
console.rule()
234234
logger.info(
235-
f"Discovered {num_discovered_tests} existing unit tests in {(time.time() - start_time):.1f}s at {self.test_cfg.tests_root}"
235+
f"Discovered {num_discovered_tests} existing unit tests and {num_discovered_replay_tests} replay tests in {(time.time() - start_time):.1f}s at {self.test_cfg.tests_root}"
236236
)
237237
console.rule()
238238
ph("cli-optimize-discovered-tests", {"num_tests": num_discovered_tests})

0 commit comments

Comments
 (0)