ProfilingDjango: runserver-profiler.diff

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

patch for management.py enables profiling for runserver

  • django/core/management.py

     
    925925        sys.stderr.write(s.read())
    926926        sys.exit(1)
    927927
     928import hotshot, time, os
     929def make_profiler_handler(inner_handler):
     930    def handler(environ, start_response):
     931        profname = "%s.%.3f.prof" % (environ['PATH_INFO'].strip("/").replace('/', '.'), time.time())
     932        profname = os.path.join('/tmp', profname)
     933        prof = hotshot.Profile(profname)
     934        return prof.runcall(inner_handler, environ, start_response)
     935    return handler
     936
    928937def runserver(addr, port):
    929938    "Starts a lightweight Web server for development."
    930939    from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException
     
    942951        print "Development server is running at http://%s:%s/" % (addr, port)
    943952        print "Quit the server with CONTROL-C (Unix) or CTRL-BREAK (Windows)."
    944953        try:
    945             run(addr, int(port), AdminMediaHandler(WSGIHandler()))
     954            run(addr, int(port), make_profiler_handler(AdminMediaHandler(WSGIHandler())))
    946955        except WSGIServerException, e:
    947956            # Use helpful error messages instead of ugly tracebacks.
    948957            ERRORS = {
Back to Top