Skip to content

Instantly share code, notes, and snippets.

@alex-bender
Forked from hirokiky/callpraphmiddleware.py
Last active August 29, 2015 14:17
Show Gist options
  • Save alex-bender/84b37b0389da2553bfbe to your computer and use it in GitHub Desktop.
Save alex-bender/84b37b0389da2553bfbe to your computer and use it in GitHub Desktop.
from pycallgraph import PyCallGraph
from pycallgraph import Config
from pycallgraph import GlobbingFilter
from pycallgraph.output import GraphvizOutput
class ProfilerMiddleware(object):
includes = []
excludes = []
def can(self, request):
return settings.DEBUG and 'prof' in request.GET
def process_request(self, request, *args, **kwargs):
if self.can(request):
config = Config()
config.trace_filter = GlobbingFilter(include=self.includes)
graphviz = GraphvizOutput(output_file='callgraph.png')
self.profiler = PyCallGraph(output=graphviz, config=config)
self.profiler.start()
def process_response(self, request, response):
if self.can(request):
self.profiler.done()
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment