Fixed log paths for profiling decorator

This commit is contained in:
VakarisZ 2020-04-30 16:28:46 +03:00
parent 4dcae80a64
commit 9be8d4af1b
1 changed files with 8 additions and 1 deletions

View File

@ -1,6 +1,9 @@
from cProfile import Profile
import os
import pstats
PROFILER_LOG_DIR = "./profiler_logs/"
def profile(sort_args=['cumulative'], print_args=[100]):
profiler = Profile()
@ -11,7 +14,11 @@ def profile(sort_args=['cumulative'], print_args=[100]):
try:
result = profiler.runcall(fn, *args, **kwargs)
finally:
filename = _get_filename_for_function(fn)
try:
os.mkdir(PROFILER_LOG_DIR)
except os.error:
pass
filename = PROFILER_LOG_DIR + _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)