Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
0a57afa
first pass at wrapper deco behavioral
KRRT7 Aug 26, 2025
c9aaaad
don't reapply too early
KRRT7 Aug 26, 2025
ad1d085
feedback
KRRT7 Aug 27, 2025
14353d4
Update codeflash_wrap_decorator.py
KRRT7 Aug 27, 2025
3c05f97
Update function_optimizer.py
KRRT7 Aug 28, 2025
bafad49
Update codeflash_wrap_decorator.py
KRRT7 Aug 28, 2025
745a3f7
test gen for async too
KRRT7 Aug 29, 2025
5a47237
Update aiservice.py
KRRT7 Aug 29, 2025
1ab1886
create path object once
KRRT7 Aug 29, 2025
af96274
use sqlite vs pickle
KRRT7 Aug 29, 2025
4dfda09
no dependencies on codeflash
KRRT7 Aug 29, 2025
2a2a3a3
add tests for async wrapper
KRRT7 Aug 29, 2025
b569d44
linting
KRRT7 Aug 29, 2025
4514dd6
address feedback re: using existing logic from tracer & cf capture
KRRT7 Aug 29, 2025
fb9555e
test
KRRT7 Aug 30, 2025
66f7a84
add unit tests
KRRT7 Aug 30, 2025
aa872b5
be consistent
KRRT7 Aug 30, 2025
0319803
Update codeflash_wrap_decorator.py
KRRT7 Aug 30, 2025
9bbe988
some async changes
KRRT7 Aug 31, 2025
1fe7c04
Update test_add_runtime_comments.py
KRRT7 Aug 31, 2025
91b8902
Update codeflash/code_utils/codeflash_wrap_decorator.py
misrasaurabh1 Sep 2, 2025
0f066c0
Merge branch 'granular-async-instrumentation' of https://github.com/c…
KRRT7 Sep 2, 2025
69fa701
mirror tests
KRRT7 Sep 3, 2025
862415e
add support for async tests in edit_generated_tests
KRRT7 Sep 3, 2025
febf2b6
add tests with run_and_parse_tests
KRRT7 Sep 3, 2025
61aade1
Update test_async_run_and_parse_tests.py
KRRT7 Sep 3, 2025
5bf5c20
⚡️ Speed up method `CommentMapper.visit_ClassDef` by 197% in PR #687 …
codeflash-ai[bot] Sep 3, 2025
b2bf9fa
add missing imports
KRRT7 Sep 3, 2025
ef74bb5
Update edit_generated_tests.py
KRRT7 Sep 3, 2025
e119085
Merge pull request #706 from codeflash-ai/codeflash/optimize-pr687-20…
KRRT7 Sep 3, 2025
600dafc
⚡️ Speed up method `CommentMapper.visit_FunctionDef` by 84% in PR #68…
codeflash-ai[bot] Sep 3, 2025
1142de5
linting / formatting
KRRT7 Sep 3, 2025
14280fa
Merge pull request #709 from codeflash-ai/codeflash/optimize-pr687-20…
KRRT7 Sep 3, 2025
3ca6fad
linter
KRRT7 Sep 3, 2025
73d03ae
add E2E
KRRT7 Sep 3, 2025
0af669f
Update workload.py
KRRT7 Sep 3, 2025
1ebc616
Update workload.py
KRRT7 Sep 3, 2025
d8a6ab2
temp
KRRT7 Sep 3, 2025
7bcd136
go
KRRT7 Sep 3, 2025
2429801
restore original code if not found for async
KRRT7 Sep 3, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions codeflash/code_utils/codeflash_wrap_decorator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
from __future__ import annotations

import gc
import inspect
import os
import pickle
import time
from functools import wraps
from pathlib import Path
from typing import Any, Callable, TypeVar

from codeflash.code_utils.code_utils import get_run_tmp_file

F = TypeVar("F", bound=Callable[..., Any])


def extract_test_context_from_frame() -> tuple[str, str | None, str]:
frame = inspect.currentframe()
try:
while frame:
frame = frame.f_back
if frame and frame.f_code.co_name.startswith("test_"):
test_name = frame.f_code.co_name
test_module_name = frame.f_globals.get("__name__", "unknown_module")
test_class_name = None
if "self" in frame.f_locals:
test_class_name = frame.f_locals["self"].__class__.__name__

return test_module_name, test_class_name, test_name
raise RuntimeError("No test function found in call stack")
finally:
del frame


def codeflash_behavior_async(func: F) -> F:
function_name = func.__name__
line_id = f"{func.__name__}_{func.__code__.co_firstlineno}"
loop_index = int(os.environ.get("CODEFLASH_LOOP_INDEX", "1"))

@wraps(func)
async def async_wrapper(*args: Any, **kwargs: Any) -> Any: # noqa: ANN401
test_module_name, test_class_name, test_name = extract_test_context_from_frame()

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

if not hasattr(async_wrapper, "index"):
async_wrapper.index = {}
if test_id in async_wrapper.index:
async_wrapper.index[test_id] += 1
else:
async_wrapper.index[test_id] = 0

codeflash_test_index = async_wrapper.index[test_id]
invocation_id = f"{line_id}_{codeflash_test_index}"
test_stdout_tag = f"{test_module_name}:{(test_class_name + '.' if test_class_name else '')}{test_name}:{function_name}:{loop_index}:{invocation_id}"

print(f"!$######{test_stdout_tag}######$!")

exception = None
gc.disable()
try:
counter = time.perf_counter_ns()
ret = func(*args, **kwargs)

if inspect.isawaitable(ret):
counter = time.perf_counter_ns()
return_value = await ret
else:
return_value = ret

codeflash_duration = time.perf_counter_ns() - counter
except Exception as e:
codeflash_duration = time.perf_counter_ns() - counter
exception = e
finally:
gc.enable()

print(f"!######{test_stdout_tag}######!")

iteration = os.environ.get("CODEFLASH_TEST_ITERATION", "0")

codeflash_run_tmp_dir = get_run_tmp_file(Path()).as_posix()

output_file = Path(codeflash_run_tmp_dir) / f"test_return_values_{iteration}.bin"

with output_file.open("ab") as f:
pickled_values = (
pickle.dumps((args, kwargs, exception)) if exception else pickle.dumps((args, kwargs, return_value))
)
_test_name = f"{test_module_name}:{(test_class_name + '.' if test_class_name else '')}{test_name}:{function_name}:{line_id}".encode(
"ascii"
)

f.write(len(_test_name).to_bytes(4, byteorder="big"))
f.write(_test_name)
f.write(codeflash_duration.to_bytes(8, byteorder="big"))
f.write(len(pickled_values).to_bytes(4, byteorder="big"))
f.write(pickled_values)
f.write(loop_index.to_bytes(8, byteorder="big"))
f.write(len(invocation_id).to_bytes(4, byteorder="big"))
f.write(invocation_id.encode("ascii"))

if exception:
raise exception
return return_value

return async_wrapper


def codeflash_performance_async(func: F) -> F:
function_name = func.__name__
line_id = f"{func.__name__}_{func.__code__.co_firstlineno}"
loop_index = int(os.environ.get("CODEFLASH_LOOP_INDEX", "1"))

@wraps(func)
async def async_wrapper(*args: Any, **kwargs: Any) -> Any: # noqa: ANN401
test_module_name, test_class_name, test_name = extract_test_context_from_frame()

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

if not hasattr(async_wrapper, "index"):
async_wrapper.index = {}
if test_id in async_wrapper.index:
async_wrapper.index[test_id] += 1
else:
async_wrapper.index[test_id] = 0

codeflash_test_index = async_wrapper.index[test_id]
invocation_id = f"{line_id}_{codeflash_test_index}"
test_stdout_tag = f"{test_module_name}:{(test_class_name + '.' if test_class_name else '')}{test_name}:{function_name}:{loop_index}:{invocation_id}"

print(f"!$######{test_stdout_tag}######$!")

exception = None
gc.disable()
try:
counter = time.perf_counter_ns()
ret = func(*args, **kwargs)

if inspect.isawaitable(ret):
counter = time.perf_counter_ns()
return_value = await ret
else:
return_value = ret

codeflash_duration = time.perf_counter_ns() - counter
except Exception as e:
codeflash_duration = time.perf_counter_ns() - counter
exception = e
finally:
gc.enable()

print(f"!######{test_stdout_tag}:{codeflash_duration}######!")

if exception:
raise exception
return return_value

return async_wrapper
Loading
Loading