Last active
January 21, 2023 11:09
-
-
Save tzaffi/16eab6204cbff3db8bfa9339cfbf765b to your computer and use it in GitHub Desktop.
python `setprofile(hook)` to count the number of times each function in the package was called
This file contains hidden or 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
import atexit | |
from collections import Counter, defaultdict | |
import sys | |
CALLS = Counter() # defaultdict(int) | |
def hook(frame, event, arg): | |
if event != "call": | |
return | |
func_name = frame.f_code.co_name | |
func_filename = frame.f_code.co_filename | |
key = f"{func_filename}:{frame.f_lineno}: {func_name}" | |
if "pyteal/pyteal" not in key: | |
return | |
if "pyteal/pyteal/ast" in key or "pyteal/pyteal/ir" in key: | |
return | |
# print(frame) | |
# print(dir(frame)) | |
# exit(42) | |
if key not in CALLS: | |
print(f"hello from {key}") | |
CALLS.update([key]) | |
def print_calls(): | |
for k, v in CALLS.most_common(len(CALLS)): | |
print(f"{k}: {v}") | |
OFF = True | |
if not OFF: | |
sys.setprofile(hook) | |
atexit.register(print_calls) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment