--- 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('
%s
' % io.getvalue()) return response