Code

ProfilingDjango: runserver-profiler.diff

File runserver-profiler.diff, 1.3 KB (added by akaihola, 8 years ago)

patch for management.py enables profiling for runserver

Line 
1Index: django/core/management.py
2===================================================================
3--- django/core/management.py   (revision 2598)
4+++ django/core/management.py   (working copy)
5@@ -925,6 +925,15 @@
6         sys.stderr.write(s.read())
7         sys.exit(1)
8 
9+import hotshot, time, os
10+def make_profiler_handler(inner_handler):
11+    def handler(environ, start_response):
12+        profname = "%s.%.3f.prof" % (environ['PATH_INFO'].strip("/").replace('/', '.'), time.time())
13+        profname = os.path.join('/tmp', profname)
14+        prof = hotshot.Profile(profname)
15+        return prof.runcall(inner_handler, environ, start_response)
16+    return handler
17+
18 def runserver(addr, port):
19     "Starts a lightweight Web server for development."
20     from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException
21@@ -942,7 +951,7 @@
22         print "Development server is running at http://%s:%s/" % (addr, port)
23         print "Quit the server with CONTROL-C (Unix) or CTRL-BREAK (Windows)."
24         try:
25-            run(addr, int(port), AdminMediaHandler(WSGIHandler()))
26+            run(addr, int(port), make_profiler_handler(AdminMediaHandler(WSGIHandler())))
27         except WSGIServerException, e:
28             # Use helpful error messages instead of ugly tracebacks.
29             ERRORS = {