2017-08-15 01:14:32 +02:00
|
|
|
import cProfile
|
|
|
|
from functools import wraps
|
2020-06-11 00:54:34 +02:00
|
|
|
from typing import Any, Callable, TypeVar
|
2017-08-15 01:14:32 +02:00
|
|
|
|
2017-10-29 06:50:55 +01:00
|
|
|
ReturnT = TypeVar('ReturnT')
|
2017-08-15 01:14:32 +02:00
|
|
|
|
2017-10-29 06:50:55 +01:00
|
|
|
def profiled(func: Callable[..., ReturnT]) -> Callable[..., ReturnT]:
|
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
|
|
|
|
|
|
|
"""
|
|
|
|
@wraps(func)
|
2017-10-29 06:50:55 +01:00
|
|
|
def wrapped_func(*args: Any, **kwargs: Any) -> ReturnT:
|
2017-08-15 01:14:32 +02:00
|
|
|
fn = func.__name__ + ".profile"
|
|
|
|
prof = cProfile.Profile()
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
retval: ReturnT = prof.runcall(func, *args, **kwargs)
|
2017-08-15 01:14:32 +02:00
|
|
|
prof.dump_stats(fn)
|
|
|
|
return retval
|
2017-10-29 06:50:55 +01:00
|
|
|
return wrapped_func
|