如何在Django Python中计算响应时间 [英] How to calculate response time in Django Python

查看:327
本文介绍了如何在Django Python中计算响应时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django Python的新手。请建议如何计算从用户输入搜索条件开始到相关信息加载/显示到门户上为止的响应时间。

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/displayed onto the portal. Thanks.

推荐答案

Django是用于后端操作的python框架。目的是处理http请求,因此,在这种情况下,从用户输入搜索条件的那一刻起,直到相关信息被加载/显示为止,您的问题非常模糊。您的问题表明您正在查看一些基于Java / Ajax的交互式前端?

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?

如果您对单个http请求的呈现时间感到好奇,则可以使用自定义中间件来解决这个问题。 ,如下所示:

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

然后,将此中间件添加到相应的Django settings.py 部分:

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

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

产生响应所花费的时间将添加到自定义http标头 X-total-time中。请注意,这将涉及所有渲染,计算,第三方系统和数据库操作。

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.

这篇关于如何在Django Python中计算响应时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆