zoukankan      html  css  js  c++  java
  • django通过middleware计算每个页面的详细执行时间

    你可以自定义一个MiddleWare类,然后在settings.py引用这个中间件,添加到MIDDLEWARE_CLASSES里,然后在公共模板里添显示代码即可。

    添加到公共模板里的代码:

    <div id="stats"><!-- STATS: Total: %(totTime).2fs <br/>Python: %(pyTime).2fs<br/>DB: %(dbTime).2fs <br/>Queries: %(queries)d --></div>

    StatsMiddleware 中间件代码

    from django.db import connection

    from time import time

    from operator import add

    import re

    class StatsMiddleware(object):

        def process_view(self, request, view_func, view_args, view_kwargs):

            """

            In your base template, put this:

            {% if debug %}  <div id="stats"><!-- STATS: Total: %(totTime).2fs <br/>

            Python: %(pyTime).2fs <br/>

            DB: %(dbTime).2fs <br/>

            Queries: %(queries)d --></div> {% endif %}

            Here's the css style I use:

            #stats { font-size: 65%; padding: 5px;

            z-index: 1000; position: absolute; right: 5px; top: 5px;

            -moz-opacity: .7; opacity: .7;}

            """

            #This stuff will only happen if debug is already on

            if not settings.DEBUG:

                return None

            # get number of db queries before we do anything

            n = len(connection.queries)

            # time the view

            start = time()

            response = view_func(request, *view_args, **view_kwargs)

            totTime = time() - start

            # compute the db time for the queries just run

            queries = len(connection.queries) - n

            if queries:

                dbTime = reduce(add, [float(q['time'])

                                      for q in connection.queries[n:]])

            else:

                dbTime = 0.0

            # and backout python time

            pyTime = totTime - dbTime

            stats = {

                'totTime': totTime,

                'pyTime': pyTime,

                'dbTime': dbTime,

                'queries': queries,

                }

            # replace the comment if found

            if response and response.content:

                s = response.content

                regexp = re.compile(r'(?P<cmt><!--s*STATS:(?P<fmt>.*?)-->)')

                match = regexp.search(s)

                if match:

                    s = s[:match.start('cmt')] + 

                        match.group('fmt') % stats + 

                        s[match.end('cmt'):]

                    response.content = s

            return response

    保存为:statsmiddleware.py, 
    然后添加到settings.py的MIDDLEWARE_CLASSES里 

    文章转载:http://www.sharejs.com/codes/python/8638

  • 相关阅读:
    微信小程序wx:key以及wx:key=" *this"详解:
    JavaScript实现按照指定长度为数字前面补零输出的方法
    多行文字溢出点点点的3中实现方法
    C#多态“说来也说”——逻辑层BLL中的多态使用
    .NET文件并发与RabbitMQ(初探RabbitMQ)
    StackExchange.Redis客户端读写主从配置,以及哨兵配置。
    RedisRepository封装—Redis发布订阅以及StackExchange.Redis中的使用
    StackExchange.Redis帮助类解决方案RedisRepository封装(散列Hash类型数据操作)
    StackExchange.Redis帮助类解决方案RedisRepository封装(字符串类型数据操作)
    StackExchange.Redis帮助类解决方案RedisRepository封装(基础配置)
  • 原文地址:https://www.cnblogs.com/weiok/p/4872127.html
Copyright © 2011-2022 走看看