Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import sys
import tempfile
import hotshot
import hotshot.stats
from django.conf import settings
from cStringIO import StringIO
class ProfileMiddleware(object):
Displays hotshot profiling for any view.
Add the "prof" key to query string by appending ?prof (or &prof=)
and you'll see the profiling results in your browser.
It's set up to only be available in django's debug mode,
but you really shouldn't add this middleware to any production configuration.
* Only tested on Linux
def process_request(self, request):
if settings.DEBUG and request.GET.has_key('prof'):
self.tmpfile = tempfile.NamedTemporaryFile() = hotshot.Profile(
def process_view(self, request, callback, callback_args, callback_kwargs):
if settings.DEBUG and request.GET.has_key('prof'):
return, request, *callback_args, **callback_kwargs)
def process_response(self, request, response):
if settings.DEBUG and request.GET.has_key('prof'):
out = StringIO()
old_stdout = sys.stdout
sys.stdout = out
stats = hotshot.stats.load(
stats.sort_stats('time', 'calls')
sys.stdout = old_stdout
stats_str = out.getvalue()
if response and response.content and stats_str:
response.content = "<pre>" + stats_str + "</pre>"
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment