mirror of
https://github.com/pretix/pretix.git
synced 2026-05-05 15:14:04 +00:00
39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
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)
|