Created
March 2, 2015 06:36
-
-
Save hirokiky/aa6cead86a0fc9becf2a to your computer and use it in GitHub Desktop.
Django Middleware to show call graph.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks but how’d you integrate it into an existing Django project?