Python profiling tools
Sat 08 June 2013
This is a small addition to the previous article about the debuggers.
If you don’t know what are you looking for, just use a visualization tool!
There’s so much advice to just roll out your own visualization tool, be it text using the Stats from stdlib or graphical using libraries like pycallgraph or gprof2dot the first reaction would be to actually start writing code to generate reports.
But this is a bad idea, you need to change that code to skew or drill down in the report and generally everything will be confusing if you’re not looking for something specific. You will probably overlook that thing that is killing your performance.
You can install it with pip install RunSnakeRun or apt-get install runsnakerun or from sources.
RunSnakeRun is a well rounded tool, easy to integrate - you can just use it with the stdlib cProfile/profile, just specify the filename argument to profile.run, eg:
import cProfile cProfile.run("main()", filename="my.profile")
After that, run this in the shell:
It looks like this:
This is acceptable and will work well if you don’t have freakishly huge profiles, eg: you just have 1 function than takes too much time.
I really like this tool! It shows you call tree graphs, sortable call tables, call/callee maps, sourcecode, and you can filter everything. It is language agnostic - you probably know this tool if you come from a C/C++ background.
I like this over RunSnakeRun cause it’s a lot more powerful:
- on the call tree graphs: you can sort, change layout/rendering in many ways or export to dot/png - RunSnakeRun doesn’t even show a call tree graph.
- you can see sourcecode
- you got callee maps
- it could be less pain to install it (no wxPython dependency)
Tools to generate KCachegrind profiles
I guess this is the only downside, you need to export in this specific format. But it is well supported:
- django-extensions’s runprofileserver
- this decorator
- repoze.profile - just set the cachegrind_filename
I’m using this function decorator that just litters your /tmp with profile dumps.