How to calculate response time in Django Python

后端 未结 4 1104
半阙折子戏
半阙折子戏 2021-01-23 05:19

I am new to Django Python. Please advise how to calculate a response time from the moment where the user input the search criteria until the relevant information are loaded/disp

相关标签:
4条回答
  • 2021-01-23 06:02

    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.

    0 讨论(0)
  • 2021-01-23 06:04

    You can see it in the internet browsers (F12) or if you use POSTMAN, it show the time. In also you can use standard python library time for measuring the execution time of a piece of code.

    0 讨论(0)
  • 2021-01-23 06:09

    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',
        ...
    )
    
    0 讨论(0)
  • 2021-01-23 06:16

    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
    
    0 讨论(0)
提交回复
热议问题