Code

ProfilingDjango: runserver-profiler3.diff

File runserver-profiler3.diff, 2.3 KB (added by Stefan Tjarks <stefan AT tjarks DOT de>, 6 years ago)

New patch against r7569. Extend runserver command with '--profile' argument. Will run djangos development server with profiling output into systems temporary directory.

Line 
1--- runserver.py        2008-06-09 10:57:57.000000000 +1200
2+++ runserver.py.profiler       2008-06-09 10:57:47.000000000 +1200
3@@ -9,6 +9,8 @@ class Command(BaseCommand):
4             help='Tells Django to NOT use the auto-reloader.'),
5         make_option('--adminmedia', dest='admin_media_path', default='',
6             help='Specifies the directory from which to serve admin media.'),
7+        make_option('--profile', action='store_true', dest='profile',
8+            help='Enable profiling. Write profiles into systems temporary directory.'),
9     )
10     help = "Starts a lightweight Web server for development."
11     args = '[optional port number, or ipaddr:port]'
12@@ -39,6 +41,7 @@ class Command(BaseCommand):
13         use_reloader = options.get('use_reloader', True)
14         admin_media_path = options.get('admin_media_path', '')
15         shutdown_message = options.get('shutdown_message', '')
16+        profile = options.get('profile', False)
17         quit_command = (sys.platform == 'win32') and 'CTRL-BREAK' or 'CONTROL-C'
18 
19         def inner_run():
20@@ -50,7 +53,19 @@ class Command(BaseCommand):
21             print "Quit the server with %s." % quit_command
22             try:
23                 path = admin_media_path or django.__path__[0] + '/contrib/admin/media'
24-                handler = AdminMediaHandler(WSGIHandler(), path)
25+                if profile:
26+                    import hotshot, time, tempfile
27+                    def make_profiler_handler(inner_handler):
28+                        def handler(environ, start_response):
29+                            path = environ['PATH_INFO'].strip("/").replace('/', '.')
30+                            fd, profname = tempfile.mkstemp('.prof', '%s.%3f' % (path, time.time()))
31+                            os.close(fd)
32+                            prof = hotshot.Profile(profname)
33+                            return prof.runcall(inner_handler, environ, start_response)
34+                        return handler
35+                    handler = make_profiler_handler(AdminMediaHandler(WSGIHandler(), path))
36+                else:
37+                    handler = AdminMediaHandler(WSGIHandler(), path)
38                 run(addr, int(port), handler)
39             except WSGIServerException, e:
40                 # Use helpful error messages instead of ugly tracebacks.