Changed perfstats middleware to log sql queries
This commit is contained in:
@@ -1,45 +1,25 @@
|
||||
import views, json, tempfile
|
||||
import hotshot
|
||||
import hotshot.stats
|
||||
import views, json, tempfile, time
|
||||
from django.conf import settings
|
||||
from django.db import connection
|
||||
|
||||
tmpfile = None
|
||||
prof = None
|
||||
isRunning = False
|
||||
|
||||
def restart_profile():
|
||||
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:
|
||||
class ProfileMiddleware:
|
||||
def process_request (self, request):
|
||||
global isRunning
|
||||
if not isRunning:
|
||||
prof.start()
|
||||
isRunning = True
|
||||
else:
|
||||
print "Profiler was already running. Results may be unpredictable"
|
||||
|
||||
self.t = time.time()
|
||||
print "Process request"
|
||||
|
||||
def process_response (self, request, response):
|
||||
global isRunning
|
||||
if isRunning:
|
||||
prof.stop()
|
||||
isRunning = False
|
||||
totalTime = time.time() - self.t
|
||||
tmpfile = tempfile.NamedTemporaryFile(prefix='sqlprof-t=' + str(totalTime) + "-", delete=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"
|
||||
return response
|
||||
|
||||
Reference in New Issue
Block a user