Forked from roger-link/pycallgraph_middleware.py
Last active
August 29, 2015 14:06
-
-
Save mdogo/5620dfcfd4cfa31063f2 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
import time | |
from pycallgraph import Config, GlobbingFilter, PyCallGraph | |
from pycallgraph.output import GraphvizOutput | |
from django.conf import settings | |
class CallgraphMiddleware(object): | |
def process_view(self, request, callback, callback_args, callback_kwargs): | |
if settings.DEBUG and 'graph' in request.GET: | |
config = Config( | |
max_depth=getattr(settings, 'CALLGRAPH_MAXDEPTH', 100)) | |
config.trace_filter = GlobbingFilter( | |
include=getattr(settings, 'CALLGRAPH_INCLUDES', []) | |
) | |
graphviz = GraphvizOutput( | |
output_file='callgraph-' + str(time.time()) + '.svg', | |
output_type='svg' | |
) | |
pycallgraph = PyCallGraph( | |
output=graphviz, | |
config=config | |
) | |
pycallgraph.start() | |
self.pycallgraph = pycallgraph | |
def process_response(self, request, response): | |
if settings.DEBUG and 'graph' in request.GET: | |
self.pycallgraph.done() | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment