Added a profiling decorator, that can be used on methods to get their performance info

This commit is contained in:
VakarisZ 2020-04-30 16:14:31 +03:00
parent f73beac3a7
commit 8603d18879
1 changed files with 25 additions and 0 deletions

View File

@ -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(".", "_")