Skip to content

Conversation

@KRRT7
Copy link
Contributor

@KRRT7 KRRT7 commented Sep 23, 2025

PR Type

Enhancement, Tests


Description

  • Add async retry with backoff utility

  • Report async throughput gains in outputs

  • Propagate throughput metrics across pipeline

  • Update async end-to-end test target


Diagram Walkthrough

flowchart LR
  A["Async function under test"] -- "retry_with_backoff" --> B["E2E async test"]
  B["E2E async test"] -- "benchmark + measure" --> C["Baselines (runtime, throughput)"]
  C["Baselines (runtime, throughput)"] -- "compare" --> D["determine_best_candidate()"]
  D["determine_best_candidate()"] -- "include throughput gain" --> E["Console tree output"]
  C["Baselines (runtime, throughput)"] -- "persist" --> F["Explanation model"]
  F["Explanation model"] -- "choose best metric" --> G["Explanation text & console"]
Loading

File Walkthrough

Relevant files
Enhancement
main.py
Add async retry_with_backoff helper                                           

code_to_optimize/code_directories/async_e2e/main.py

  • Replace fake API with async retry helper
  • Implement retry_with_backoff(func, max_retries)
  • Validate max_retries and handle exceptions
  • Add minimal backoff using time.sleep
+15/-3   
function_optimizer.py
Surface and propagate async throughput metrics                     

codeflash/optimization/function_optimizer.py

  • Print throughput gain in candidate summary
  • Pass async throughput to explanations
  • Include throughput in review regeneration
  • Adjust logging for original throughput
+17/-3   
explanation.py
Prefer throughput in explanation and output                           

codeflash/result/explanation.py

  • Add async throughput fields to Explanation
  • Prefer throughput metric when more indicative
  • Update console string to show throughput
  • Import throughput helpers
+48/-1   
Miscellaneous
critic.py
Tidy critic logging for throughput gains                                 

codeflash/result/critic.py

  • Remove noisy throughput gain info logging
  • Keep throughput-based selection logic
+0/-3     
Tests
end_to_end_test_async.py
Update async E2E test to new target                                           

tests/scripts/end_to_end_test_async.py

  • Point test to main.py
  • Expect zero unit tests generated
  • Update coverage target to retry_with_backoff
  • Adjust expected covered line numbers
+4/-4     

@github-actions
Copy link

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Async Bug

Using time.sleep inside async retry blocks the event loop; replace with await asyncio.sleep for non-blocking backoff.

async def retry_with_backoff(func, max_retries=3):
    if max_retries < 1:
        raise ValueError("max_retries must be at least 1")
    last_exception = None
    for attempt in range(max_retries):
        try:
            return await func()
        except Exception as e:
            last_exception = e
            if attempt < max_retries - 1:
                time.sleep(0.0001 * attempt)
Fragile Coverage

Hard-coded expected_lines for coverage look brittle; line shifts will break the test. Consider matching by statements/branches or a looser range.

config = TestConfig(
    file_path="main.py",
    expected_unit_tests=0,
    min_improvement_x=0.1,
    coverage_expectations=[
        CoverageExpectation(
            function_name="retry_with_backoff",
            expected_coverage=100.0,
            expected_lines=[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
        )
    ],
)
cwd = (
Metric Choice

Logic prefers throughput when runtime improvement is <= 0; verify that negative runtime change with small throughput gain should still override and that messaging stays consistent across outputs.

@property
def perf_improvement_line(self) -> str:
    runtime_improvement = self.speedup

    if (
        self.original_async_throughput is not None 
        and self.best_async_throughput is not None 
        and self.original_async_throughput > 0
    ):
        throughput_improvement = throughput_gain(
            original_throughput=self.original_async_throughput,
            optimized_throughput=self.best_async_throughput,
        )

        # Use throughput metrics if throughput improvement is better or runtime got worse
        if throughput_improvement > runtime_improvement or runtime_improvement <= 0:
            throughput_pct = f"{throughput_improvement * 100:,.0f}%"
            throughput_x = f"{throughput_improvement + 1:,.2f}x"
            return f"{throughput_pct} improvement ({throughput_x} faster)."

    return f"{self.speedup_pct} improvement ({self.speedup_x} faster)."

@github-actions
Copy link

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Avoid blocking event loop

Replace the blocking sleep with asyncio.sleep to avoid blocking the event loop in an
async function. Also make the initial delay non-zero to ensure a minimal backoff
delay.

code_to_optimize/code_directories/async_e2e/main.py [5-16]

 async def retry_with_backoff(func, max_retries=3):
     if max_retries < 1:
         raise ValueError("max_retries must be at least 1")
     last_exception = None
     for attempt in range(max_retries):
         try:
             return await func()
         except Exception as e:
             last_exception = e
             if attempt < max_retries - 1:
-                time.sleep(0.0001 * attempt)
+                await asyncio.sleep(0.0001 * max(1, attempt))
     raise last_exception
Suggestion importance[1-10]: 8

__

Why: The function retry_with_backoff is async but uses blocking time.sleep, which can stall the event loop; replacing with await asyncio.sleep(...) is correct and impactful for async correctness. The improved code aligns with the PR context and imports asyncio are present.

Medium

@github-actions github-actions bot added the workflow-modified This PR modifies GitHub Actions workflows label Sep 23, 2025
@KRRT7 KRRT7 merged commit 9aa34d9 into get-throughput-from-output Sep 23, 2025
19 of 20 checks passed
@KRRT7 KRRT7 deleted the end-to-end-test branch September 23, 2025 07:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review effort 2/5 workflow-modified This PR modifies GitHub Actions workflows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant