Skip to content

Instantly share code, notes, and snippets.

@donkirkby
Created April 20, 2015 18:38
Show Gist options
  • Save donkirkby/947e8ab9187845d04e62 to your computer and use it in GitHub Desktop.
Save donkirkby/947e8ab9187845d04e62 to your computer and use it in GitHub Desktop.
Record all different call stacks that arrive at a method
from collections import Counter
import traceback
stack_counts = Counter()
def record(limit=10):
stack = traceback.extract_stack(limit=limit+1)[:-1]
stack_report = '\n'.join(map(str, stack))
stack_counts[stack_report] += 1
def print_report():
# negative count to display highest count first
entries = [(-count, stack_report)
for stack_report, count in stack_counts.iteritems()]
entries.sort()
print '== {} stack traces =='.format(sum(stack_counts.values()))
for count, stack_report in entries:
print '=== {} times ==='.format(-count)
print stack_report
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment