Created
June 23, 2020 19:35
-
-
Save ionelmc/60167892b924483432b64bb85cd315f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import time | |
from hunter import Action | |
from hunter import trace | |
class ProfileAction(Action): | |
def __init__(self): | |
self.timings = {} | |
def __call__(self, event): | |
if event.kind == 'call': | |
self.timings[id(event.frame)] = time() | |
else: | |
start_time = self.timings.pop(id(event.frame), None) | |
if start_time is None: | |
return | |
if event.kind == 'exception': | |
print(f"{event.function} raised exception: {event.arg}. Duration: {time() - start_time:.4f}s") | |
elif event.kind == 'return': | |
print(f"{event.function} returned: {event.arg}. Duration: {time() - start_time:.4f}s") | |
else: | |
print(f"Unexpected event kind: {event.kind}") | |
with trace( | |
module_startswith='api.', | |
kind__in=['call', 'return', 'exception'], | |
action=ProfileAction | |
): | |
start_application( ... ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment