|
| 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