Skip to content

Instantly share code, notes, and snippets.

@hirokiky
Created March 2, 2015 06:36
Show Gist options
  • Save hirokiky/aa6cead86a0fc9becf2a to your computer and use it in GitHub Desktop.
Save hirokiky/aa6cead86a0fc9becf2a to your computer and use it in GitHub Desktop.
Django Middleware to show call graph.
from pycallgraph import PyCallGraph
from pycallgraph import Config
from pycallgraph import GlobbingFilter
from pycallgraph.output import GraphvizOutput
class ProfilerMiddleware(object):
includes = []
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
@sbalasa
Copy link

sbalasa commented Mar 22, 2022

Thanks but how’d you integrate it into an existing Django project?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment