django經過middleware計算每一個頁面的詳細執行時間

你能夠自定義一個MiddleWare類,而後在settings.py引用這個中間件,添加到MIDDLEWARE_CLASSES裏,而後在公共模板裏添顯示代碼便可。css

 

添加到公共模板裏的代碼:python

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

 

StatsMiddleware 中間件代碼app

 

 

from django.db import connectionthis

from time import timedebug

from operator import addcode

import reregexp

 

class StatsMiddleware(object):中間件

 

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

        """

        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

相關文章
相關標籤/搜索