Django

Code

ProfilingDjango: runserver-profiler3.diff

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

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

  • runserver.py

    old new  
    99            help='Tells Django to NOT use the auto-reloader.'), 
    1010        make_option('--adminmedia', dest='admin_media_path', default='', 
    1111            help='Specifies the directory from which to serve admin media.'), 
     12        make_option('--profile', action='store_true', dest='profile', 
     13            help='Enable profiling. Write profiles into systems temporary directory.'), 
    1214    ) 
    1315    help = "Starts a lightweight Web server for development." 
    1416    args = '[optional port number, or ipaddr:port]' 
     
    3941        use_reloader = options.get('use_reloader', True) 
    4042        admin_media_path = options.get('admin_media_path', '') 
    4143        shutdown_message = options.get('shutdown_message', '') 
     44        profile = options.get('profile', False) 
    4245        quit_command = (sys.platform == 'win32') and 'CTRL-BREAK' or 'CONTROL-C' 
    4346 
    4447        def inner_run(): 
     
    5053            print "Quit the server with %s." % quit_command 
    5154            try: 
    5255                path = admin_media_path or django.__path__[0] + '/contrib/admin/media' 
    53                 handler = AdminMediaHandler(WSGIHandler(), path) 
     56                if profile: 
     57                    import hotshot, time, tempfile 
     58                    def make_profiler_handler(inner_handler):  
     59                        def handler(environ, start_response): 
     60                            path = environ['PATH_INFO'].strip("/").replace('/', '.') 
     61                            fd, profname = tempfile.mkstemp('.prof', '%s.%3f' % (path, time.time())) 
     62                            os.close(fd) 
     63                            prof = hotshot.Profile(profname) 
     64                            return prof.runcall(inner_handler, environ, start_response)  
     65                        return handler 
     66                    handler = make_profiler_handler(AdminMediaHandler(WSGIHandler(), path)) 
     67                else: 
     68                    handler = AdminMediaHandler(WSGIHandler(), path) 
    5469                run(addr, int(port), handler) 
    5570            except WSGIServerException, e: 
    5671                # Use helpful error messages instead of ugly tracebacks.