Login

pycallgraph

Author:
roppert
Posted:
February 9, 2009
Language:
Python
Version:
1.0
Score:
2 (after 2 ratings)

Simple debug middleware that uses pycallgraph to get a visual representation of the call graph, including number of calls and execution times.

Usage:

  1. Replace myapp in the snippet with the name of your application and or adjust include and exclude according to your needs

  2. Add CallgraphMiddleware to your middlewares in settings.py

  3. Append ?prof to any URL in your application to trigger the callgraph creation

Each callgraph cerated will be named callgraph-timestamp.png. This is because multiple callgraphs will be created when a re-direction occurs for example.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import time
import pycallgraph
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:
            filter_func = pycallgraph.GlobbingFilter(include=['myapp.*'],
                    exclude=['myapp.debug.*'])
            pycallgraph.start_trace(filter_func=filter_func)

    def process_response(self, request, response):
        if settings.DEBUG and 'graph' in request.GET:
            pycallgraph.make_dot_graph('callgraph-' + str(time.time()) + '.png')
        return response

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.