Last active
August 29, 2015 14:17
-
-
Save weargoggles/0e8d2753a709e91b1fd9 to your computer and use it in GitHub Desktop.
profiling python with cprofile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I'm pretty sure this would work without writing to any files, | |
# but you'd need somewhere to stream the output. | |
try: | |
import cProfile as profile | |
except ImportError: | |
import profile | |
import pstats | |
import time | |
from functools import wraps | |
import pickle | |
import base64 | |
try: | |
from cStringIO import StringIO | |
except ImportError: | |
from StringIO import StringIO | |
def someFunc(): | |
pr = profile.Profile() | |
pr.enable() | |
# normal body of your function | |
pr.disable() | |
s = StringIO.StringIO() | |
pstats.Stats(pr, stream=s).dump_stats('/tmp/bort') | |
# later... | |
def print_me_some_stats_pretty_please(): | |
stats = pstats.Stats('/tmp/bort') | |
stats.sort_stats('cumulative') | |
stats.print_stats() | |
def print_stats_from_file(filename): | |
stats = pstats.Stats(filename) | |
stats.sort_stats('cumulative') | |
stats.print_stats() | |
# let's have a decorator version | |
module_name = __name__ | |
def profiled(class_name=False): | |
def inner(f): | |
@wraps(f) | |
def wrapper(*args, **kwargs): | |
pr = profile.Profile() | |
pr.enable() | |
x = f(*args, **kwargs) | |
pr.disable() | |
args_string = '+'.join(map(str, args) + map(lambda a: '='.join(map(str, a)), kwargs.iteritems())) | |
kwargs_string = '+'.join(map(lambda a: '='.join(map(str, a)), kwargs.iteritems())) | |
if class_name: | |
name = '.'.join((module_name, class_name, f.__name__)) | |
else: | |
name = '.'.join((module_name, f.__name__)) | |
stats_filename = '{name},{args},{timestamp}'.format( | |
name=name, | |
args=kwargs, | |
timestamp=str(time.time()), | |
) | |
fifo_path = '/tmp/profile-stats' | |
stats = pstats.Stats(pr, stream=StringIO()) | |
stuff = pickle.dumps([ | |
name, | |
args_string, | |
kwargs_string, | |
str(time.time()), | |
stats.stats, | |
stats.get_sort_arg_defs(), | |
]) | |
logger.info("pickle dumped") | |
with open(fifo_path, 'wb') as fifo: | |
fifo.write(base64.b64encode(stuff) + "\n") | |
logger.info("pickle written") | |
return x | |
return wrapper | |
return inner |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment