codeflash-agent/.codeflash/krrt7/odoo/profile_cache_keys.py
Kevin Turcios 20f6c59f05
Lint and format entire repo, not just packages (#23)
Remove .codeflash/ from ruff extend-exclude, add per-file ignores
for .codeflash/, scripts/, evals/, and plugin/ (benchmark/script
patterns like print, eval, magic values). Remove shebangs. Widen
pre-commit hooks to check the full repo.
2026-04-15 03:16:15 -05:00

41 lines
1,019 B
Python

"""Profile the cache key generation to find hotspots"""
import cProfile
import pstats
from inspect import Parameter, signature
def build_cache_key_eval(method, cache_args):
"""Current implementation using eval"""
args = ", ".join(
str(params.replace(annotation=Parameter.empty))
for params in signature(method).parameters.values()
)
values = ["self._name", "method", *cache_args]
code = (
f"lambda {args}: ({''.join(a for arg in values for a in (arg, ','))})"
)
return eval(code, {"method": method})
class MockModel:
_name = "test.model"
def simple_method(self, arg1, arg2):
return arg1 + arg2
# Profile the creation overhead
profiler = cProfile.Profile()
profiler.enable()
for i in range(10_000):
_ = build_cache_key_eval(MockModel.simple_method, ["arg1", "arg2"])
profiler.disable()
# Print stats
stats = pstats.Stats(profiler)
stats.sort_stats("cumulative")
print("=== Top 20 functions by cumulative time ===")
stats.print_stats(20)