Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate response time in Django

Tags:

python

django

How do I calculate the response time from the moment where the user input the search criteria until the relevant information are loaded/displayed onto the portal?

like image 317
KenL Avatar asked Dec 22 '17 07:12

KenL


3 Answers

Django is a python framework for backend operations. It's purpose is to process http requests, hence your question "from the moment where the user input the search criteria until the relevant information are loaded/displayed" is very vague in this context. Your question suggests you are looking at some interactive Javascript/Ajax based frontend?

If you are curious about render times for individual http requests, you may approach this with a custom middleware, something along these lines:

class StatsMiddleware(object):
    def process_request(self, request):
        "Start time at request coming in"
        request.start_time = time.time()

    def process_response(self, request, response):
        "End of request, take time"
        total = time.time() - request.start_time

        # Add the header.
        response["X-total-time"] = int(total * 1000)
        return response

Then, add this middleware in the corresponding Django settings.py section:

MIDDLEWARE_CLASSES = (
  ...
  'app.stats.StatsMiddleware',
  ...
)

The time it took to produce the response will be added to a custom http header "X-total-time". Note this will involve all rendering, calculation, 3rd party system and DB ops.

like image 136
Andreas Neumeier Avatar answered Sep 23 '22 00:09

Andreas Neumeier


Since Django 1.10 this works differently now.

https://docs.djangoproject.com/en/3.0/topics/http/middleware/

The new style would be as follows:

import time


class StatsMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        start_time = time.time()

        response = self.get_response(request)

        duration = time.time() - start_time

        # Add the header. Or do other things, my use case is to send a monitoring metric
        response["X-Page-Generation-Duration-ms"] = int(duration * 1000)
        return response

No need to store the start time on the request object and retrieve because it all happens in the same method.

It can also be done in a simple function style instead of a class:

import time


def stats_middleware(get_response):

    def middleware(request):
        start_time = time.time()

        response = get_response(request)

        duration = time.time() - start_time

        response["X-Page-Generation-Duration-ms"] = int(duration * 1000)
        return response

    return middleware
like image 44
Tristan Perotti Avatar answered Sep 22 '22 00:09

Tristan Perotti


Here’s the class that does the entire thing

import time


class StatsMiddleware(object):

    def process_request(self, request):
        "Store the start time when the request comes in."
        request.start_time = time.time()

    def process_response(self, request, response):
        "Calculate and output the page generation duration"
        # Get the start time from the request and calculate how long
        # the response took.
        duration = time.time() - request.start_time

        # Add the header.
        response["X-Page-Generation-Duration-ms"] = int(duration * 1000)
        return response

That’s all there’s to it. Just store the time when the request comes in, and retrieve it later.

To install the middleware above, just add it to your settings.py:

MIDDLEWARE_CLASSES = (
    'project.stats_middleware.StatsMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    ...
)
like image 20
Usman Maqbool Avatar answered Sep 21 '22 00:09

Usman Maqbool