forked from p34709852/monkey
Added a profiling decorator, that can be used on methods to get their performance info
This commit is contained in:
parent
f73beac3a7
commit
8603d18879
|
@ -0,0 +1,25 @@
|
||||||
|
from cProfile import Profile
|
||||||
|
import pstats
|
||||||
|
|
||||||
|
|
||||||
|
def profile(sort_args=['cumulative'], print_args=[100]):
|
||||||
|
profiler = Profile()
|
||||||
|
|
||||||
|
def decorator(fn):
|
||||||
|
def inner(*args, **kwargs):
|
||||||
|
result = None
|
||||||
|
try:
|
||||||
|
result = profiler.runcall(fn, *args, **kwargs)
|
||||||
|
finally:
|
||||||
|
filename = _get_filename_for_function(fn)
|
||||||
|
with open(filename, 'w') as stream:
|
||||||
|
stats = pstats.Stats(profiler, stream=stream)
|
||||||
|
stats.strip_dirs().sort_stats(*sort_args).print_stats(*print_args)
|
||||||
|
return result
|
||||||
|
return inner
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
|
def _get_filename_for_function(fn):
|
||||||
|
function_name = fn.__module__ + "." + fn.__name__
|
||||||
|
return function_name.replace(".", "_")
|
Loading…
Reference in New Issue