Commit bd52b9e4 by Bridger Maxwell

Changed perfstats middleware to log sql queries

parent 82cbb9a1
import views, json, tempfile import views, json, tempfile, time
import hotshot
import hotshot.stats
from django.conf import settings from django.conf import settings
from django.db import connection
tmpfile = None
prof = None
isRunning = False
def restart_profile(): class ProfileMiddleware:
global prof, tmpfile, isRunning
try:
oldname = tmpfile.name
except:
oldname = ""
if prof != None:
if isRunning:
prof.stop()
prof.close()
tmpfile = tempfile.NamedTemporaryFile(prefix='prof',delete=False)
prof = hotshot.Profile(tmpfile.name, lineevents=1)
isRunning = False
print "Filename", tmpfile.name
return (tmpfile.name, oldname)
restart_profile()
class ProfileMiddleware:
def process_request (self, request): def process_request (self, request):
global isRunning self.t = time.time()
if not isRunning:
prof.start()
isRunning = True
else:
print "Profiler was already running. Results may be unpredictable"
print "Process request" print "Process request"
def process_response (self, request, response): def process_response (self, request, response):
global isRunning totalTime = time.time() - self.t
if isRunning: tmpfile = tempfile.NamedTemporaryFile(prefix='sqlprof-t=' + str(totalTime) + "-", delete=False)
prof.stop()
isRunning = False output = ""
for query in connection.queries:
output += "Time: " + str(query['time']) + "\nQuery: " + query['sql'] + "\n\n"
tmpfile.write(output)
print "SQL Log file: " , tmpfile.name
tmpfile.close()
print "Process response" print "Process response"
return response return response
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment