Added a patch for debug mode, to add a filtering option to the django-cprofile-middleware.

- Legacy-Id: 15004
This commit is contained in:
Henrik Levkowetz 2018-04-04 17:08:26 +00:00
parent 7d4cb5be59
commit 6d096e7f2a
2 changed files with 44 additions and 0 deletions

View file

@ -951,6 +951,10 @@ CHECKS_LIBRARY_PATCHES_TO_APPLY = [
'patch/add-patch-already-patched-flag.patch',
'patch/fix-patch-no-chdir.patch',
]
if DEBUG:
CHECKS_LIBRARY_PATCHES_TO_APPLY += [
'patch/add-django-cprofile-filter.patch',
]
STATS_NAMES_LIMIT = 25

View file

@ -0,0 +1,40 @@
--- django_cprofile_middleware/middleware.py.old 2018-04-04 06:32:29.282187502 -0700
+++ django_cprofile_middleware/middleware.py 2018-04-04 06:44:00.126537763 -0700
@@ -1,4 +1,5 @@
import pstats
+import re
try:
import cProfile as profile
@@ -14,6 +15,15 @@
from django.utils.deprecation import MiddlewareMixin
+class Stats(pstats.Stats):
+ def filter_stats(self, regex):
+ oldstats = self.stats
+ self.stats = newstats = {}
+ filter = re.compile(regex)
+ for func, (cc, nc, tt, ct, callers) in oldstats.iteritems():
+ if filter.search(pstats.func_std_string(func)):
+ newstats[func] = (cc, nc, tt, ct, callers)
+
class ProfilerMiddleware(MiddlewareMixin):
"""
Simple profile middleware to profile django views. To run it, add ?prof to
@@ -62,8 +72,13 @@
response['Content-Length'] = len(output)
else:
io = StringIO()
- stats = pstats.Stats(self.profiler, stream=io)
- stats.strip_dirs().sort_stats(request.GET.get('sort', 'time'))
+ stats = Stats(self.profiler, stream=io)
+ if request.GET.get('stripdirs', False):
+ stats = stats.strip_dirs()
+ filter = request.GET.get('filter', None)
+ if filter:
+ stats.filter_stats(filter)
+ stats.sort_stats(request.GET.get('sort', 'time'))
stats.print_stats(int(request.GET.get('count', 100)))
response = HttpResponse('<pre>%s</pre>' % io.getvalue())
return response