Commit 82cbb9a1 by Bridger Maxwell

Matched stop and start calls in perf middleware.

parent 6e9e9c76
...@@ -5,17 +5,21 @@ from django.conf import settings ...@@ -5,17 +5,21 @@ from django.conf import settings
tmpfile = None tmpfile = None
prof = None prof = None
isRunning = False
def restart_profile(): def restart_profile():
global prof, tmpfile global prof, tmpfile, isRunning
try: try:
oldname = tmpfile.name oldname = tmpfile.name
except: except:
oldname = "" oldname = ""
if prof != None: if prof != None:
if isRunning:
prof.stop()
prof.close() prof.close()
tmpfile = tempfile.NamedTemporaryFile(prefix='prof',delete=False) tmpfile = tempfile.NamedTemporaryFile(prefix='prof',delete=False)
prof = hotshot.Profile(tmpfile.name) prof = hotshot.Profile(tmpfile.name, lineevents=1)
isRunning = False
print "Filename", tmpfile.name print "Filename", tmpfile.name
return (tmpfile.name, oldname) return (tmpfile.name, oldname)
...@@ -23,13 +27,19 @@ restart_profile() ...@@ -23,13 +27,19 @@ restart_profile()
class ProfileMiddleware: class ProfileMiddleware:
def process_request (self, request): def process_request (self, request):
prof.start() global isRunning
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):
try: global isRunning
if isRunning:
prof.stop() prof.stop()
except: isRunning = False
print "Profiler not active. If you didn't just restart, this is an error"
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