Opened 9 years ago

Last modified 13 months ago

#25704 assigned New feature

Response time in WSGIRequestHandler.log_request — at Version 2

Reported by: Andrei Fokau Owned by: nobody
Component: HTTP handling Version: dev
Severity: Normal Keywords:
Cc: Sarah Abderemane Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Andrei Fokau)

It's often useful to know how much time it takes for runserver to respond without setting up a middleware or using the debug toolbar. Currently, one could monkeypatch WSGIRequestHandler (e.g. in manage.py) in order to get the time:

from django.core.servers.basehttp import WSGIRequestHandler
_handle = WSGIRequestHandler.handle

def handle(self):
    self.request_started = time.time()
    _handle(self)

def log_request(self, code='-', size='-'):
    self.log_message('"%s" %s %s %dms',
                     self.requestline, str(code), str(size),
                     (time.time() - self.request_started) * 1e3)

WSGIRequestHandler.handle = handle
WSGIRequestHandler.log_request = log_request

or via middleware

class ResponseTimeMiddleware(object):
    def process_view(self, request, view_func, view_args, view_kwargs):
        start = time.time()
        response = view_func(request, *view_args, **view_kwargs)

        if response and getattr(response, 'is_rendered', True) is False:
            response.rendered_content

        logging.getLogger('response_time').debug(
            'Response time %dms', (time.time() - start) * 1000)
        return response

It seems easy to add it in the WSGIRequestHandler e.g. https://github.com/django/django/pull/5606. The response time is slightly longer than actual response time due to late measuring but think an easy implementation is better than the exact duration.

This feature is blocked by https://code.djangoproject.com/ticket/25684

Change History (2)

comment:1 by Simon Charette, 9 years ago

Description: modified (diff)
Triage Stage: UnreviewedAccepted

I'm not sure this should be part of the actual output of runserver but it would make sense to at least provide it as a kwarg to the logger calls to allow third party and users to refer to it in a formater or filter.

e.g. one could write a formatter that turns the text bold if request take more than X ms to complete.

Last edited 9 years ago by Simon Charette (previous) (diff)

comment:2 by Andrei Fokau, 9 years ago

Description: modified (diff)
Triage Stage: AcceptedUnreviewed
Note: See TracTickets for help on using tickets.
Back to Top