-
-
Save alex-bender/84b37b0389da2553bfbe to your computer and use it in GitHub Desktop.
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 = [] | |
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