Skip to content

Commit ad1d085

Browse files
committed
feedback
1 parent c9aaaad commit ad1d085

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

codeflash.code-workspace

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"request": "launch",
7171
"program": "${workspaceFolder:codeflash}/codeflash/main.py",
7272
"args": [
73-
"--all",
73+
"--file", "src/async_examples/shocker.py", "--verbose"
7474
],
7575
"cwd": "${input:chooseCwd}",
7676
"console": "integratedTerminal",

codeflash/code_utils/codeflash_wrap_decorator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ async def async_wrapper(*args: Any, **kwargs: Any) -> Any: # noqa: ANN401
108108

109109

110110
def codeflash_performance_async(func: F) -> F:
111-
function_name = func.__name__
112-
line_id = f"{func.__name__}_{func.__code__.co_firstlineno}"
113-
loop_index = int(os.environ.get("CODEFLASH_LOOP_INDEX", "1"))
114-
115111
@wraps(func)
116112
async def async_wrapper(*args: Any, **kwargs: Any) -> Any: # noqa: ANN401
113+
function_name = func.__name__
114+
line_id = f"{func.__name__}_{func.__code__.co_firstlineno}"
115+
loop_index = os.environ["CODEFLASH_LOOP_INDEX"]
116+
117117
test_module_name, test_class_name, test_name = extract_test_context_from_frame()
118118

119119
test_id = f"{test_module_name}:{test_class_name}:{test_name}:{line_id}:{loop_index}"

codeflash/optimization/function_optimizer.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1541,11 +1541,25 @@ def run_optimized_candidate(
15411541
candidate_helper_code = {}
15421542
for module_abspath in original_helper_code:
15431543
candidate_helper_code[module_abspath] = Path(module_abspath).read_text("utf-8")
1544+
if self.function_to_optimize.is_async:
1545+
from codeflash.code_utils.instrument_existing_tests import (
1546+
instrument_source_module_with_async_decorators,
1547+
)
1548+
1549+
success, instrumented_source = instrument_source_module_with_async_decorators(
1550+
self.function_to_optimize.file_path, self.function_to_optimize, TestingMode.BEHAVIOR
1551+
)
1552+
if success and instrumented_source:
1553+
with self.function_to_optimize.file_path.open("w", encoding="utf8") as f:
1554+
f.write(instrumented_source)
1555+
logger.debug(
1556+
f"Applied async behavioral instrumentation to {self.function_to_optimize.file_path} for candidate {optimization_candidate_index}"
1557+
)
1558+
15441559
try:
15451560
instrument_codeflash_capture(
15461561
self.function_to_optimize, file_path_to_helper_classes, self.test_cfg.tests_root
15471562
)
1548-
15491563
candidate_behavior_results, _ = self.run_and_parse_tests(
15501564
testing_type=TestingMode.BEHAVIOR,
15511565
test_env=test_env,

tests/test_instrument_async_tests.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,11 @@ async def test_async_function():
248248
func,
249249
temp_dir,
250250
"pytest",
251-
mode=TestingMode.BEHAVIOR,
252-
source_module_path=source_file
251+
mode=TestingMode.BEHAVIOR
253252
)
254253

255-
assert success
256-
assert instrumented_test_code is not None
257-
assert 'codeflash_wrap(' in instrumented_test_code
254+
assert not success
255+
assert instrumented_test_code is None
258256

259257

260258
def test_inject_profiling_async_function_performance_mode(temp_dir):
@@ -316,14 +314,11 @@ async def test_async_function():
316314
func,
317315
temp_dir,
318316
"pytest",
319-
mode=TestingMode.PERFORMANCE,
320-
source_module_path=source_file
317+
mode=TestingMode.PERFORMANCE
321318
)
322319

323-
assert success
324-
assert instrumented_test_code is not None
325-
# Test should use wrapper calls but source module should have async decorators
326-
assert 'codeflash_wrap(' in instrumented_test_code
320+
assert not success
321+
assert instrumented_test_code is None
327322

328323

329324
def test_mixed_sync_async_instrumentation(temp_dir):
@@ -390,14 +385,12 @@ async def test_mixed_functions():
390385
async_func,
391386
temp_dir,
392387
"pytest",
393-
mode=TestingMode.BEHAVIOR,
394-
source_module_path=source_file
388+
mode=TestingMode.BEHAVIOR
395389
)
396390

397-
assert success
398-
assert instrumented_test_code is not None
399-
# Both sync and async function calls should use wrappers in test file
400-
assert 'codeflash_wrap(' in instrumented_test_code
391+
# Async functions should not be instrumented at the test level
392+
assert not success
393+
assert instrumented_test_code is None
401394

402395

403396

0 commit comments

Comments
 (0)