Fixed profiler

This commit is contained in:
krateng 2023-10-26 18:52:23 +02:00
parent f545e400a1
commit 8eb495bbaf
2 changed files with 23 additions and 12 deletions

View File

@ -634,7 +634,7 @@ def artist_info(dbconn=None,**keys):
"topweeks":len([
week for week in ranges(step="week") if (week != twk) and any(
(e.get('artist_id') == artist_id) and (e.get('rank') == 1) for e in
sqldb.count_scrobbles_by_artist(since=week.first_stamp(),to=week.last_stamp(),resolve_ids=False,dbconn=dbconn)
sqldb.count_scrobbles_by_artist(since=week.first_stamp(),to=week.last_stamp(),resolve_ids=False,associated=True,dbconn=dbconn)
)
# we don't need to check the whole thing, just until rank is lower, but... well, its a list comprehension
])
@ -864,9 +864,9 @@ def start_db():
with sqldb.engine.connect() as dbconn:
with dbconn.begin():
for week in ranges(step='week'):
_ = sqldb.count_scrobbles_by_artist(since=week.first_stamp(),to=week.last_stamp(),resolve_ids=False,dbconn=dbconn)
_ = sqldb.count_scrobbles_by_track(since=week.first_stamp(),to=week.last_stamp(),resolve_ids=False,dbconn=dbconn)
_ = sqldb.count_scrobbles_by_album(since=week.first_stamp(),to=week.last_stamp(),resolve_ids=False,dbconn=dbconn)
sqldb.count_scrobbles_by_artist(since=week.first_stamp(),to=week.last_stamp(),resolve_ids=False,associated=True,dbconn=dbconn)
sqldb.count_scrobbles_by_track(since=week.first_stamp(),to=week.last_stamp(),resolve_ids=False,dbconn=dbconn)
sqldb.count_scrobbles_by_album(since=week.first_stamp(),to=week.last_stamp(),resolve_ids=False,dbconn=dbconn)

View File

@ -15,10 +15,16 @@ SINGLE_CALLS = False
# only save the last single call instead of adding up all calls
# of that function for more representative performance result
if SINGLE_CALLS:
profiler = cProfile.Profile()
if not SINGLE_CALLS:
profilers = {}
times = {}
def profile(func):
realfunc = func
while hasattr(realfunc, '__innerfunc__'):
realfunc = realfunc.__innerfunc__
def newfunc(*args,**kwargs):
clock = Clock()
@ -27,10 +33,10 @@ def profile(func):
if FULL_PROFILE:
benchmarkfolder = data_dir['logs']("benchmarks")
os.makedirs(benchmarkfolder,exist_ok=True)
if not SINGLE_CALLS:
if SINGLE_CALLS:
localprofiler = cProfile.Profile()
else:
localprofiler = profiler
localprofiler = profilers.setdefault(realfunc,cProfile.Profile())
localprofiler.enable()
result = func(*args,**kwargs)
@ -39,10 +45,15 @@ def profile(func):
localprofiler.disable()
seconds = clock.stop()
realfunc = func
while(hasattr(realfunc,'__innerfunc__')):
realfunc = realfunc.__innerfunc__
log(f"Executed {realfunc.__name__} ({args}, {kwargs}) in {seconds:.2f}s",module="debug_performance")
if not SINGLE_CALLS:
times.setdefault(realfunc,[]).append(seconds)
if SINGLE_CALLS:
log(f"Executed {realfunc.__name__} ({args}, {kwargs}) in {seconds:.2f}s",module="debug_performance")
else:
log(f"Executed {realfunc.__name__} ({args}, {kwargs}) in {seconds:.2f}s (Average: { sum(times[realfunc])/len(times[realfunc]):.2f}s)",module="debug_performance")
if FULL_PROFILE:
targetfilename = os.path.join(benchmarkfolder,f"{realfunc.__name__}.stats")
try: