2022-03-06 05:17:31 +03:00
|
|
|
import os
|
|
|
|
|
2022-02-26 22:39:23 +03:00
|
|
|
import cProfile, pstats
|
|
|
|
|
2022-03-06 05:17:31 +03:00
|
|
|
from doreah.logging import log
|
|
|
|
from doreah.timing import Clock
|
|
|
|
|
2022-04-09 22:39:04 +03:00
|
|
|
from ..pkg_global.conf import data_dir
|
2022-03-06 05:17:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
profiler = cProfile.Profile()
|
2022-02-26 22:39:23 +03:00
|
|
|
|
2022-04-12 20:04:22 +03:00
|
|
|
FULL_PROFILE = False
|
|
|
|
|
2022-02-26 22:39:23 +03:00
|
|
|
def profile(func):
|
|
|
|
def newfunc(*args,**kwargs):
|
2022-03-06 05:17:31 +03:00
|
|
|
|
2022-04-12 20:04:22 +03:00
|
|
|
if FULL_PROFILE:
|
|
|
|
benchmarkfolder = data_dir['logs']("benchmarks")
|
|
|
|
os.makedirs(benchmarkfolder,exist_ok=True)
|
2022-03-06 05:17:31 +03:00
|
|
|
|
|
|
|
clock = Clock()
|
|
|
|
clock.start()
|
|
|
|
|
2022-04-12 20:04:22 +03:00
|
|
|
if FULL_PROFILE:
|
|
|
|
profiler.enable()
|
2022-02-26 22:39:23 +03:00
|
|
|
result = func(*args,**kwargs)
|
2022-04-12 20:04:22 +03:00
|
|
|
if FULL_PROFILE:
|
|
|
|
profiler.disable()
|
2022-04-09 22:20:48 +03:00
|
|
|
|
2022-03-12 10:28:48 +03:00
|
|
|
log(f"Executed {func.__name__} ({args}, {kwargs}) in {clock.stop():.2f}s",module="debug_performance")
|
2022-04-12 20:04:22 +03:00
|
|
|
if FULL_PROFILE:
|
|
|
|
try:
|
|
|
|
pstats.Stats(profiler).dump_stats(os.path.join(benchmarkfolder,f"{func.__name__}.stats"))
|
|
|
|
except:
|
|
|
|
pass
|
2022-03-12 10:28:48 +03:00
|
|
|
|
2022-02-26 22:39:23 +03:00
|
|
|
return result
|
|
|
|
|
|
|
|
return newfunc
|