Skip to content

Instantly share code, notes, and snippets.

@dziegler
Created April 3, 2010 00:31
Show Gist options
  • Save dziegler/353910 to your computer and use it in GitHub Desktop.
Save dziegler/353910 to your computer and use it in GitHub Desktop.
# Orignal version taken from http://www.djangosnippets.org/snippets/186/
# Original author: udfalkso
# Modified by: Shwagroo Team
# Modified again by: David Ziegler
import sys
import os
import re
import hotshot, hotshot.stats
import tempfile
import pprint
from django.db import connection
from django.conf import settings
import cProfile
from cStringIO import StringIO
import pstats
words_re = re.compile( r'\s+' )
group_prefix_re = [
re.compile( "^.*/django/[^/]+" ),
re.compile( "^(.*)/[^/]+$" ), # extract module path
re.compile( ".*" ), # catch strange entries
]
class ProfileMiddleware(object):
"""
Displays hotshot profiling for any view.
http://yoursite.com/yourview/?prof
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, is available for superuser otherwise,
but you really shouldn't add this middleware to any production configuration.
WARNING: It uses hotshot profiler which is not thread safe.
"""
def process_request(self, request):
if (settings.DEBUG or request.user.is_superuser) and request.REQUEST.has_key('prof'):
connection.queries = []
self.prof = cProfile.Profile()
def process_view(self, request, callback, callback_args, callback_kwargs):
if (settings.DEBUG or request.user.is_superuser) and request.REQUEST.has_key('prof'):
args = (request,) + callback_args
return self.prof.runcall(callback, *args, **callback_kwargs)
def get_group(self, file):
for g in group_prefix_re:
name = g.findall( file )
if name:
return name[0]
def get_summary(self, results_dict, sum):
list = [ (item[1], item[0]) for item in results_dict.items() ]
list.sort( reverse = True )
list = list[:40]
res = " tottime\n"
for item in list:
res += "%4.1f%% %7.3f %s\n" % ( 100*item[0]/sum if sum else 0, item[0], item[1] )
return res
def summary_for_files(self, stats_str):
stats_str = stats_str.split("\n")[5:]
mystats = {}
mygroups = {}
sum = 0
for s in stats_str:
fields = words_re.split(s);
if len(fields) == 7:
time = float(fields[2])
sum += time
file = fields[6].split(":")[0]
if not file in mystats:
mystats[file] = 0
mystats[file] += time
group = self.get_group(file)
if not group in mygroups:
mygroups[ group ] = 0
mygroups[ group ] += time
return "<pre>" + \
" ---- By file ----\n\n" + self.get_summary(mystats,sum) + "\n" + \
" ---- By group ---\n\n" + self.get_summary(mygroups,sum) + \
"</pre>"
def process_response(self, request, response):
if (settings.DEBUG or request.user.is_superuser) and request.REQUEST.has_key('prof'):
stats = self.prof.create_stats()
out = StringIO()
old_stdout, sys.stdout = sys.stdout, out
self.prof.print_stats(1)
sys.stdout = old_stdout
stats_str = out.getvalue()
if response and response.content and stats_str:
response.content = "<pre>" + stats_str + "</pre>"
response.content = "\n".join(response.content.split("\n")[:100])
response.content += self.summary_for_files(stats_str)
response.content += '\n%d SQL Queries in %.3f seconds :\n' %(
len(connection.queries), sum([ float(i.get('time', 0)) for i in connection.queries if i['sql'].find('cachebot_cachebotsignals') < 1 ]))
response.content += pprint.pformat([i for i in connection.queries if i['sql'].find('cachebot_cachebotsignals') < 1])
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment