Skip to content

Instantly share code, notes, and snippets.

@prateek
Forked from laserson/README.md
Created November 9, 2015 22:16
Show Gist options
  • Save prateek/6062b4b470f88f279c5f to your computer and use it in GitHub Desktop.
Save prateek/6062b4b470f88f279c5f to your computer and use it in GitHub Desktop.
Generate FlameGraph for Python code using plop

Create a FlameGraph to visualize where your code is spending its time.

Requires plop and FlameGraph.

cat profile.out | /path/to/FlameGraph/stackcollapse.pl | /path/to/FlameGraph/flamegraph.pl > profile.big.svg
def print_stacks(stack_counts, outfile):
with open(outfile, 'w') as op:
for (stack, count) in stack_counts.iteritems():
for frame in stack:
path = '/'.join(frame[0].split('/')[-2:])
loc = frame[1]
fn = frame[2]
print >>op, "%s:%i:%s" % (path, loc, fn)
print >>op, "%i\n" % count
import plop.collector
collector = plop.collector.Collector()
collector.start(duration=6000.)
# <... my code ...>
collector.stop()
print_stacks(collector.stack_counts, 'profile.out')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment