Skip to content

Commit 6d096e7

Browse files
committed
Added a patch for debug mode, to add a filtering option to the django-cprofile-middleware.
- Legacy-Id: 15004
1 parent 7d4cb5b commit 6d096e7

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

ietf/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,10 @@ def skip_unreadable_post(record):
951951
'patch/add-patch-already-patched-flag.patch',
952952
'patch/fix-patch-no-chdir.patch',
953953
]
954+
if DEBUG:
955+
CHECKS_LIBRARY_PATCHES_TO_APPLY += [
956+
'patch/add-django-cprofile-filter.patch',
957+
]
954958

955959
STATS_NAMES_LIMIT = 25
956960

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--- django_cprofile_middleware/middleware.py.old 2018-04-04 06:32:29.282187502 -0700
2+
+++ django_cprofile_middleware/middleware.py 2018-04-04 06:44:00.126537763 -0700
3+
@@ -1,4 +1,5 @@
4+
import pstats
5+
+import re
6+
7+
try:
8+
import cProfile as profile
9+
@@ -14,6 +15,15 @@
10+
from django.utils.deprecation import MiddlewareMixin
11+
12+
13+
+class Stats(pstats.Stats):
14+
+ def filter_stats(self, regex):
15+
+ oldstats = self.stats
16+
+ self.stats = newstats = {}
17+
+ filter = re.compile(regex)
18+
+ for func, (cc, nc, tt, ct, callers) in oldstats.iteritems():
19+
+ if filter.search(pstats.func_std_string(func)):
20+
+ newstats[func] = (cc, nc, tt, ct, callers)
21+
+
22+
class ProfilerMiddleware(MiddlewareMixin):
23+
"""
24+
Simple profile middleware to profile django views. To run it, add ?prof to
25+
@@ -62,8 +72,13 @@
26+
response['Content-Length'] = len(output)
27+
else:
28+
io = StringIO()
29+
- stats = pstats.Stats(self.profiler, stream=io)
30+
- stats.strip_dirs().sort_stats(request.GET.get('sort', 'time'))
31+
+ stats = Stats(self.profiler, stream=io)
32+
+ if request.GET.get('stripdirs', False):
33+
+ stats = stats.strip_dirs()
34+
+ filter = request.GET.get('filter', None)
35+
+ if filter:
36+
+ stats.filter_stats(filter)
37+
+ stats.sort_stats(request.GET.get('sort', 'time'))
38+
stats.print_stats(int(request.GET.get('count', 100)))
39+
response = HttpResponse('<pre>%s</pre>' % io.getvalue())
40+
return response

0 commit comments

Comments
 (0)