CARVIEW |
Select Language
HTTP/2 200
server: nginx
content-type: text/html;charset=utf-8
cache-control: must-revalidate
expires: Fri, 01 Jan 1999 00:00:00 GMT
set-cookie: trac_form_token=c1b0fa804bef9a12c0a41d9a; HttpOnly; Path=/; Secure
set-cookie: trac_session=a96b9cef37de35b2c4e67a2a; expires=Tue, 21 Oct 2025 13:14:42 GMT; HttpOnly; Path=/; Secure
strict-transport-security: max-age=31536000; includeSubDomains; preload
permissions-policy: interest-cohort=()
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
accept-ranges: bytes
via: 1.1 varnish, 1.1 varnish
date: Wed, 23 Jul 2025 13:14:42 GMT
x-served-by: cache-fra-eddf8230151-FRA, cache-bom-vanm7210073-BOM
x-cache: MISS, MISS
x-cache-hits: 0, 0
x-timer: S1753276482.950981,VS0,VE729
vary: Accept-Encoding
PageStatsMiddleware – Django
Back to Top
Django
The web framework for perfectionists with deadlines.
Issues
Page Stats Middleware
Display performance metrics measured during the generation of a page. Allows you to display total time, python time, database time, and number of queries for the rendered page.
The Python Code
import re from operator import add from time import time from django.db import connection class StatsMiddleware(object): def process_view(self, request, view_func, view_args, view_kwargs): # turn on debugging in db backend to capture time from django.conf import settings debug = settings.DEBUG settings.DEBUG = True # 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 # restore debugging setting again settings.DEBUG = debug 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
The HTML
Put a comment in your template in this format:
<!-- STATS: format_string -->
Example:
<!-- STATS: Total: %(totTime).2f Python: %(pyTime).2f DB: %(dbTime).2f Queries: %(queries)d -->
Warning: This code will make exceptions and 404 errors visible regardless of your DEBUG setting, so don't use it on production servers.
Last modified
18 years ago
Last modified on Sep 16, 2007, 12:03:26 PM
Note:
See TracWiki
for help on using the wiki.
Download in other formats:
Django Links
Learn More
Get Involved
Follow Us
- Hosting by In-kind donors
- Design by Threespot &
© 2005-2025 Django SoftwareFoundation unless otherwise noted. Django is a registered trademark of the Django Software Foundation.