misc: add a profiling middleware
gitea/ants-hub/pipeline/head This commit looks good Details

This commit is contained in:
Benjamin Dauvergne 2023-04-14 00:28:52 +02:00
parent 3a8c844535
commit 782d8adb59
2 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,33 @@
# ANTS-Hub - Copyright (C) Entr'ouvert
import cProfile
import pstats
import time
from django.conf import settings
def profile_middleware(get_response):
if not settings.PROFILE_FILE:
return get_response
start = time.time()
pr = cProfile.Profile()
def middleware(request):
nonlocal start, pr
pr.enable()
try:
return get_response(request)
finally:
pr.disable()
# dump profile every 20 seconds
if time.time() - start > 20:
start = time.time()
with open(settings.PROFILE_FILE, 'w') as fd:
sortby = pstats.SortKey.CUMULATIVE
ps = pstats.Stats(pr, stream=fd).sort_stats(sortby)
ps.print_stats()
return middleware

View File

@ -20,8 +20,11 @@ INSTALLED_APPS = [
]
MIDDLEWARE = [
'ants_hub.middleware.profile_middleware',
]
PROFILE_FILE = None
SILENCED_SYSTEM_CHECKS = [
'admin.E408',
'admin.E409',