Skip to content

Instantly share code, notes, and snippets.

@laserson
Last active July 25, 2016 01:41
Show Gist options
  • Save laserson/d405c98fe1800b4cc29f to your computer and use it in GitHub Desktop.
Save laserson/d405c98fe1800b4cc29f 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