Skip to content

Commit b221be4

Browse files
authored
Merge pull request #739 from codeflash-ai/get-throughput-from-output
Get throughput from output for async functions
2 parents 324f607 + 9aa34d9 commit b221be4

22 files changed

+921
-452
lines changed

.github/workflows/e2e-async.yaml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: E2E - Async
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- '**' # Trigger for all paths
7+
8+
workflow_dispatch:
9+
10+
jobs:
11+
async-optimization:
12+
# Dynamically determine if environment is needed only when workflow files change and contributor is external
13+
environment: ${{ (github.event_name == 'workflow_dispatch' || (contains(toJSON(github.event.pull_request.files.*.filename), '.github/workflows/') && github.event.pull_request.user.login != 'misrasaurabh1' && github.event.pull_request.user.login != 'KRRT7')) && 'external-trusted-contributors' || '' }}
14+
15+
runs-on: ubuntu-latest
16+
env:
17+
CODEFLASH_AIS_SERVER: prod
18+
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }}
19+
CODEFLASH_API_KEY: ${{ secrets.CODEFLASH_API_KEY }}
20+
COLUMNS: 110
21+
MAX_RETRIES: 3
22+
RETRY_DELAY: 5
23+
EXPECTED_IMPROVEMENT_PCT: 10
24+
CODEFLASH_END_TO_END: 1
25+
steps:
26+
- name: 🛎️ Checkout
27+
uses: actions/checkout@v4
28+
with:
29+
ref: ${{ github.event.pull_request.head.ref }}
30+
repository: ${{ github.event.pull_request.head.repo.full_name }}
31+
fetch-depth: 0
32+
token: ${{ secrets.GITHUB_TOKEN }}
33+
34+
- name: Validate PR
35+
run: |
36+
# Check for any workflow changes
37+
if git diff --name-only "${{ github.event.pull_request.base.sha }}" "${{ github.event.pull_request.head.sha }}" | grep -q "^.github/workflows/"; then
38+
echo "⚠️ Workflow changes detected."
39+
40+
# Get the PR author
41+
AUTHOR="${{ github.event.pull_request.user.login }}"
42+
echo "PR Author: $AUTHOR"
43+
44+
# Allowlist check
45+
if [[ "$AUTHOR" == "misrasaurabh1" || "$AUTHOR" == "KRRT7" ]]; then
46+
echo "✅ Authorized user ($AUTHOR). Proceeding."
47+
elif [[ "${{ github.event.pull_request.state }}" == "open" ]]; then
48+
echo "✅ PR triggered by 'pull_request_target' and is open. Assuming protection rules are in place. Proceeding."
49+
else
50+
echo "⛔ Unauthorized user ($AUTHOR) attempting to modify workflows. Exiting."
51+
exit 1
52+
fi
53+
else
54+
echo "✅ No workflow file changes detected. Proceeding."
55+
fi
56+
57+
- name: Set up Python 3.11 for CLI
58+
uses: astral-sh/setup-uv@v5
59+
with:
60+
python-version: 3.11.6
61+
62+
- name: Install dependencies (CLI)
63+
run: |
64+
uv sync
65+
66+
- name: Run Codeflash to optimize async code
67+
id: optimize_async_code
68+
run: |
69+
uv run python tests/scripts/end_to_end_test_async.py

.github/workflows/pre-commit.yaml

Lines changed: 0 additions & 19 deletions
This file was deleted.

.pre-commit-config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
repos:
22
- repo: https://github.com/astral-sh/ruff-pre-commit
3-
rev: v0.12.7
3+
rev: v0.13.1
44
hooks:
55
# Run the linter.
66
- id: ruff-check
7+
args: [ --config=pyproject.toml ]
78
# Run the formatter.
89
- id: ruff-format
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
import time
2-
async def fake_api_call(delay, data):
3-
time.sleep(0.0001)
4-
return f"Processed: {data}"
2+
import asyncio
3+
4+
5+
async def retry_with_backoff(func, max_retries=3):
6+
if max_retries < 1:
7+
raise ValueError("max_retries must be at least 1")
8+
last_exception = None
9+
for attempt in range(max_retries):
10+
try:
11+
return await func()
12+
except Exception as e:
13+
last_exception = e
14+
if attempt < max_retries - 1:
15+
time.sleep(0.0001 * attempt)
16+
raise last_exception

codeflash.code-workspace

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@
7070
"request": "launch",
7171
"program": "${workspaceFolder:codeflash}/codeflash/main.py",
7272
"args": [
73-
"--file", "src/async_examples/shocker.py", "--verbose"
73+
"--file",
74+
"src/async_examples/concurrency.py",
75+
"--function",
76+
"task",
77+
"--verbose"
7478
],
7579
"cwd": "${input:chooseCwd}",
7680
"console": "integratedTerminal",

codeflash/api/aiservice.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ def get_new_explanation( # noqa: D417
298298
annotated_tests: str,
299299
optimization_id: str,
300300
original_explanation: str,
301+
original_throughput: str | None = None,
302+
optimized_throughput: str | None = None,
303+
throughput_improvement: str | None = None,
301304
) -> str:
302305
"""Optimize the given python code for performance by making a request to the Django endpoint.
303306
@@ -314,6 +317,9 @@ def get_new_explanation( # noqa: D417
314317
- annotated_tests: str - test functions annotated with runtime
315318
- optimization_id: str - unique id of opt candidate
316319
- original_explanation: str - original_explanation generated for the opt candidate
320+
- original_throughput: str | None - throughput for the baseline code (operations per second)
321+
- optimized_throughput: str | None - throughput for the optimized code (operations per second)
322+
- throughput_improvement: str | None - throughput improvement percentage
317323
318324
Returns
319325
-------
@@ -333,6 +339,9 @@ def get_new_explanation( # noqa: D417
333339
"optimization_id": optimization_id,
334340
"original_explanation": original_explanation,
335341
"dependency_code": dependency_code,
342+
"original_throughput": original_throughput,
343+
"optimized_throughput": optimized_throughput,
344+
"throughput_improvement": throughput_improvement,
336345
}
337346
logger.info("Generating explanation")
338347
console.rule()

0 commit comments

Comments
 (0)