mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 22:13:26 +00:00
Fixes #2665. Regenerated by tabbott with `lint --fix` after a rebase and change in parameters. Note from tabbott: In a few cases, this converts technical debt in the form of unsorted imports into different technical debt in the form of our largest files having very long, ugly import sequences at the start. I expect this change will increase pressure for us to split those files, which isn't a bad thing. Signed-off-by: Anders Kaseorg <anders@zulip.com>
32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
import cProfile
|
|
from functools import wraps
|
|
from typing import Any, Callable, TypeVar
|
|
|
|
ReturnT = TypeVar('ReturnT')
|
|
|
|
def profiled(func: Callable[..., ReturnT]) -> Callable[..., ReturnT]:
|
|
"""
|
|
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:
|
|
|
|
./tools/show-profile-results test_ratelimit_decrease.profile
|
|
|
|
"""
|
|
@wraps(func)
|
|
def wrapped_func(*args: Any, **kwargs: Any) -> ReturnT:
|
|
fn = func.__name__ + ".profile"
|
|
prof = cProfile.Profile()
|
|
retval: ReturnT = prof.runcall(func, *args, **kwargs)
|
|
prof.dump_stats(fn)
|
|
return retval
|
|
return wrapped_func
|