Create a gist now

Instantly share code, notes, and snippets.

Profile method decorator in Python showing a graphical (.svg) representation (as well as a text-based representation) as the output.
import pstats
import sys
import subprocess
import os
import cProfile as profile
except ImportError:
import profile
def profile_method(filename, rows=50, sort=(('cumul',), ('time',)), show_graph=False):
Decorator to profile the decorated function or method.
To use:
@profile_method('', show_graph=True)
def my_func_to_profile():
Depends on:
- Graphviz to generate the graph: must be installed with the GRAPHVIZ_DOT environment variable pointing to dot.exe.
- gprof2dot: must be in the PYTHONPATH).
- desktop: optional: if available will open the generated .svg automatically (a viewer must be properly registered)
:param int rows:
How many rows of data to print.
:param tuple sort:
How to sort the stats of the printed data.
Options available from:
stats = pstats.Stats(prof)
:param bool show_graph:
Whether a graph should be generated. Note: the computer should have an .svg viewer
associated to the extension so that the file is properly opened.
def wrapper(method):
def inner(*args, **kwargs):
prof = profile.Profile()
result = prof.runcall(method, *args, **kwargs)
if show_graph:
# Show text output regardless of showing graph.
tup_sort = sort
s = tup_sort[0]
if isinstance(s, str):
tup_sort = [tup_sort]
stats = pstats.Stats(prof)
for s in tup_sort:
return result
return inner
return wrapper
def _show_graph(filename):
Creates an .svg from the profile generated file and opens it (a proper association to .svg
files must be already defined in the machine).
@param str filename:
This is the file generated from profile_method.
import gprof2dot
initial = sys.argv[:]
output_filename = filename + '.dot'
sys.argv = ['', '-o', output_filename, '-f', 'pstats', filename]
sys.argv = initial
dot = os.environ['GRAPHVIZ_DOT']
except KeyError:
raise AssertionError('The GRAPHVIZ_DOT environment variable must be defined to show graph.')
assert os.path.exists(dot), 'Expected: %s to exist and point to dot.exe' % dot[dot, '-Tsvg', '-O', output_filename])
print('Opening svg created at:', os.path.realpath((output_filename + '.svg')))
import desktop
else: + '.svg')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment