简体   繁体   English

如何计算 Django Python 中的响应时间

[英]How to calculate response time in Django Python

I am new to Django Python.我是 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/displayed onto the portal.请告知如何计算从用户输入搜索条件到相关信息加载/显示到门户的响应时间。 Thanks.谢谢。

Django is a python framework for backend operations. Django 是一个用于后端操作的 Python 框架。 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.它的目的是处理http请求,因此您的问题“从用户输入搜索条件到加载/显示相关信息的那一刻”在这种情况下非常模糊。 Your question suggests you are looking at some interactive Javascript/Ajax based frontend?您的问题表明您正在研究一些基于 Javascript/Ajax 的交互式前端?

If you are curious about render times for individual http requests, you may approach this with a custom middleware, something along these lines:如果您对单个 http 请求的渲染时间感到好奇,您可以使用自定义中间件来解决这个问题,大致如下:

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:然后,在相应的 Django settings.py部分添加这个中间件:

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

The time it took to produce the response will be added to a custom http header "X-total-time".生成响应所花费的时间将添加到自定义 http 标头“X-total-time”中。 Note this will involve all rendering, calculation, 3rd party system and DB ops.请注意,这将涉及所有渲染、计算、第 3 方系统和数据库操作。

Since Django 1.10 this works differently now.从 Django 1.10 开始,现在的工作方式有所不同。

https://docs.djangoproject.com/en/3.0/topics/http/middleware/ 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

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:要安装上面的中间件,只需将其添加到 settings.py 中:

MIDDLEWARE_CLASSES = (
    'project.stats_middleware.StatsMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    ...
)

You can see it in the internet browsers (F12) or if you use POSTMAN, it show the time.您可以在 Internet 浏览器 (F12) 中看到它,或者如果您使用 POSTMAN,它会显示时间。 In also you can use standard python library time for measuring the execution time of a piece of code.您还可以使用标准 python 库time来测量一段代码的执行时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM