2017-08-15 01:14:32 +02:00
|
|
|
import cProfile
|
|
|
|
from functools import wraps
|
2020-06-24 01:52:37 +02:00
|
|
|
from typing import Callable, TypeVar, cast
|
2017-08-15 01:14:32 +02:00
|
|
|
|
2020-06-24 01:52:37 +02:00
|
|
|
FuncT = TypeVar('FuncT', bound=Callable[..., object])
|
2017-08-15 01:14:32 +02:00
|
|
|
|
2020-06-24 01:52:37 +02:00
|
|
|
def profiled(func: FuncT) -> FuncT:
|
2017-08-15 01:14:32 +02:00
|
|
|
"""
|
|
|
|
This decorator should obviously be used only in a dev environment.
|
|
|
|
It works best when surrounding a function that you expect to be
|
|
|
|
called once. One strategy is to write a backend test and wrap the
|
|
|
|
test case with the profiled decorator.
|
|
|
|
|
|
|
|
You can run a single test case like this:
|
|
|
|
|
|
|
|
# edit zerver/tests/test_external.py and place @profiled above the test case below
|
|
|
|
./tools/test-backend zerver.tests.test_external.RateLimitTests.test_ratelimit_decrease
|
|
|
|
|
|
|
|
Then view the results like this:
|
|
|
|
|
2017-10-09 12:00:05 +02:00
|
|
|
./tools/show-profile-results test_ratelimit_decrease.profile
|
2017-08-15 01:14:32 +02:00
|
|
|
|
|
|
|
"""
|
2020-06-24 01:52:37 +02:00
|
|
|
func_: Callable[..., object] = func # work around https://github.com/python/mypy/issues/9075
|
|
|
|
|
2017-08-15 01:14:32 +02:00
|
|
|
@wraps(func)
|
2020-06-24 01:52:37 +02:00
|
|
|
def wrapped_func(*args: object, **kwargs: object) -> object:
|
2017-08-15 01:14:32 +02:00
|
|
|
fn = func.__name__ + ".profile"
|
|
|
|
prof = cProfile.Profile()
|
2020-06-24 01:52:37 +02:00
|
|
|
retval = prof.runcall(func_, *args, **kwargs)
|
2017-08-15 01:14:32 +02:00
|
|
|
prof.dump_stats(fn)
|
|
|
|
return retval
|
2020-06-24 01:52:37 +02:00
|
|
|
return cast(FuncT, wrapped_func) # https://github.com/python/mypy/issues/1927
|