forked from ietf-tools/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-django-cprofile-filter.patch
More file actions
40 lines (37 loc) · 1.59 KB
/
add-django-cprofile-filter.patch
File metadata and controls
40 lines (37 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
--- django_cprofile_middleware/middleware.py.old 2018-04-04 06:32:29.282187502 -0700
+++ django_cprofile_middleware/middleware.py 2018-04-06 10:11:18.936855634 -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('psort') or 'time')
stats.print_stats(int(request.GET.get('count', 100)))
response = HttpResponse('<pre>%s</pre>' % io.getvalue())
return response