88 COVERAGE_THRESHOLD ,
99 MIN_IMPROVEMENT_THRESHOLD ,
1010 MIN_TESTCASE_PASSED_THRESHOLD ,
11+ MIN_THROUGHPUT_IMPROVEMENT_THRESHOLD ,
1112)
12- from codeflash .models .test_type import TestType
13+ from codeflash .models .models import TestType
1314
1415if TYPE_CHECKING :
1516 from codeflash .models .models import CoverageData , OptimizedCandidateResult , OriginalCodeBaseline
@@ -25,31 +26,73 @@ def performance_gain(*, original_runtime_ns: int, optimized_runtime_ns: int) ->
2526 return (original_runtime_ns - optimized_runtime_ns ) / optimized_runtime_ns
2627
2728
29+ def throughput_gain (* , original_throughput : int , optimized_throughput : int ) -> float :
30+ """Calculate the throughput gain of an optimized code over the original code.
31+
32+ This value multiplied by 100 gives the percentage improvement in throughput.
33+ For throughput, higher values are better (more executions per time period).
34+ """
35+ if original_throughput == 0 :
36+ return 0.0
37+ return (optimized_throughput - original_throughput ) / original_throughput
38+
39+
2840def speedup_critic (
2941 candidate_result : OptimizedCandidateResult ,
3042 original_code_runtime : int ,
3143 best_runtime_until_now : int | None ,
3244 * ,
3345 disable_gh_action_noise : bool = False ,
46+ original_async_throughput : int | None = None ,
47+ best_throughput_until_now : int | None = None ,
3448) -> bool :
3549 """Take in a correct optimized Test Result and decide if the optimization should actually be surfaced to the user.
3650
37- Ensure that the optimization is actually faster than the original code, above the noise floor.
38- The noise floor is a function of the original code runtime. Currently, the noise floor is 2xMIN_IMPROVEMENT_THRESHOLD
39- when the original runtime is less than 10 microseconds, and becomes MIN_IMPROVEMENT_THRESHOLD for any higher runtime.
40- The noise floor is doubled when benchmarking on a (noisy) GitHub Action virtual instance, also we want to be more confident there.
51+ Evaluates both runtime performance and async throughput improvements.
52+
53+ For runtime performance:
54+ - Ensures the optimization is actually faster than the original code, above the noise floor.
55+ - The noise floor is a function of the original code runtime. Currently, the noise floor is 2xMIN_IMPROVEMENT_THRESHOLD
56+ when the original runtime is less than 10 microseconds, and becomes MIN_IMPROVEMENT_THRESHOLD for any higher runtime.
57+ - The noise floor is doubled when benchmarking on a (noisy) GitHub Action virtual instance.
58+
59+ For async throughput (when available):
60+ - Evaluates throughput improvements using MIN_THROUGHPUT_IMPROVEMENT_THRESHOLD
61+ - Throughput improvements complement runtime improvements for async functions
4162 """
63+ # Runtime performance evaluation
4264 noise_floor = 3 * MIN_IMPROVEMENT_THRESHOLD if original_code_runtime < 10000 else MIN_IMPROVEMENT_THRESHOLD
4365 if not disable_gh_action_noise and env_utils .is_ci ():
4466 noise_floor = noise_floor * 2 # Increase the noise floor in GitHub Actions mode
4567
4668 perf_gain = performance_gain (
4769 original_runtime_ns = original_code_runtime , optimized_runtime_ns = candidate_result .best_test_runtime
4870 )
49- if best_runtime_until_now is None :
50- # collect all optimizations with this
51- return bool (perf_gain > noise_floor )
52- return bool (perf_gain > noise_floor and candidate_result .best_test_runtime < best_runtime_until_now )
71+ runtime_improved = perf_gain > noise_floor
72+
73+ # Check runtime comparison with best so far
74+ runtime_is_best = best_runtime_until_now is None or candidate_result .best_test_runtime < best_runtime_until_now
75+
76+ throughput_improved = True # Default to True if no throughput data
77+ throughput_is_best = True # Default to True if no throughput data
78+
79+ if original_async_throughput is not None and candidate_result .async_throughput is not None :
80+ if original_async_throughput > 0 :
81+ throughput_gain_value = throughput_gain (
82+ original_throughput = original_async_throughput , optimized_throughput = candidate_result .async_throughput
83+ )
84+ throughput_improved = throughput_gain_value > MIN_THROUGHPUT_IMPROVEMENT_THRESHOLD
85+
86+ throughput_is_best = (
87+ best_throughput_until_now is None or candidate_result .async_throughput > best_throughput_until_now
88+ )
89+
90+ if original_async_throughput is not None and candidate_result .async_throughput is not None :
91+ # When throughput data is available, accept if EITHER throughput OR runtime improves significantly
92+ throughput_acceptance = throughput_improved and throughput_is_best
93+ runtime_acceptance = runtime_improved and runtime_is_best
94+ return throughput_acceptance or runtime_acceptance
95+ return runtime_improved and runtime_is_best
5396
5497
5598def quantity_of_tests_critic (candidate_result : OptimizedCandidateResult | OriginalCodeBaseline ) -> bool :
0 commit comments