Skip to content

Instantly share code, notes, and snippets.

@ionelmc
Created June 23, 2020 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ionelmc/60167892b924483432b64bb85cd315f3 to your computer and use it in GitHub Desktop.
Save ionelmc/60167892b924483432b64bb85cd315f3 to your computer and use it in GitHub Desktop.
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