forked from CGM_Public/pretix_original
Add profiling utilities
This commit is contained in:
0
src/pretix/helpers/profile/__init__.py
Normal file
0
src/pretix/helpers/profile/__init__.py
Normal file
38
src/pretix/helpers/profile/middleware.py
Normal file
38
src/pretix/helpers/profile/middleware.py
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import cProfile
|
||||||
|
import os
|
||||||
|
import random
|
||||||
|
|
||||||
|
import time
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
|
||||||
|
class CProfileMiddleware(object):
|
||||||
|
blacklist = (
|
||||||
|
'/healthcheck/',
|
||||||
|
'/jsi18n/'
|
||||||
|
)
|
||||||
|
|
||||||
|
def __init__(self, get_response):
|
||||||
|
self.get_response = get_response
|
||||||
|
# One-time configuration and initialization.
|
||||||
|
|
||||||
|
def __call__(self, request):
|
||||||
|
# Code to be executed for each request before
|
||||||
|
# the view (and later middleware) are called.
|
||||||
|
for b in self.blacklist:
|
||||||
|
if b in request.path:
|
||||||
|
return self.get_response(request)
|
||||||
|
|
||||||
|
if settings.PROFILING_RATE > 0 and random.random() < settings.PROFILING_RATE / 100:
|
||||||
|
profiler = cProfile.Profile()
|
||||||
|
profiler.enable()
|
||||||
|
starttime = time.time()
|
||||||
|
response = self.get_response(request)
|
||||||
|
profiler.disable()
|
||||||
|
tottime = time.time() - starttime
|
||||||
|
profiler.dump_stats(os.path.join(settings.PROFILE_DIR, '{time:.0f}_{tottime:.3f}_{path}.pstat'.format(
|
||||||
|
path=request.path[1:].replace("/", "_"), tottime=tottime, time=time.time()
|
||||||
|
)))
|
||||||
|
return response
|
||||||
|
else:
|
||||||
|
return self.get_response(request)
|
||||||
@@ -16,6 +16,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(__file__))
|
|||||||
DATA_DIR = config.get('pretix', 'datadir', fallback=os.environ.get('DATA_DIR', 'data'))
|
DATA_DIR = config.get('pretix', 'datadir', fallback=os.environ.get('DATA_DIR', 'data'))
|
||||||
LOG_DIR = os.path.join(DATA_DIR, 'logs')
|
LOG_DIR = os.path.join(DATA_DIR, 'logs')
|
||||||
MEDIA_ROOT = os.path.join(DATA_DIR, 'media')
|
MEDIA_ROOT = os.path.join(DATA_DIR, 'media')
|
||||||
|
PROFILE_DIR = os.path.join(DATA_DIR, 'profiles')
|
||||||
|
|
||||||
if not os.path.exists(DATA_DIR):
|
if not os.path.exists(DATA_DIR):
|
||||||
os.mkdir(DATA_DIR)
|
os.mkdir(DATA_DIR)
|
||||||
@@ -223,6 +224,14 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
PROFILING_RATE = config.getfloat('django', 'profile', fallback=0) # Percentage of requests to profile
|
||||||
|
if PROFILING_RATE > 0:
|
||||||
|
if not os.path.exists(PROFILE_DIR):
|
||||||
|
os.mkdir(PROFILE_DIR)
|
||||||
|
MIDDLEWARE.insert(0, 'pretix.helpers.profile.middleware.CProfileMiddleware')
|
||||||
|
|
||||||
|
|
||||||
# Security settings
|
# Security settings
|
||||||
X_FRAME_OPTIONS = 'DENY'
|
X_FRAME_OPTIONS = 'DENY'
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user