Skip to content

bernd-wechner/django-stats-middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django Stats Middleware

Django is one of the most popular Python web frameworks today.

When a Django site seems slow, it's nice to put some real number to to that. The Django Debug Toolbar is the go-to middleware solution for diagnosing Django performance among other things, but it is rather large, and slow itself, adding significantly to the load time of your pages. So it's in a bit of catch-22 bind for simple performance overviews.

For that reason I wrote this tiny little lightweight piece of middleware that reports at the bottom of every page a single line reporting some basic timing stats.

  • Total Time: The time from the request arriving to the response being delivered.
  • Python Time: Time spent running Python code
  • DB Time: Time spent waiting on database queries
  • Number of Queries: The number of database queries run

Where:

​ Python Time + DB Time = Total Time

Here's an example:

sample

To use it just put it at the top of your Django Middleware stack:

MIDDLEWARE = (
    'django_stats_middleware.StatsMiddleware',
	...

That way Total Time will include the time spent in all other middleware as well as your site code.

It includes some minimal CSS and to include that in your site include it as an app (so Django knows to collect static files from it):

    INSTALLED_APPS += (
        'django_stats_middleware',
    )

and in your template load the CSS:

    {% load static %}
    <link rel="stylesheet" type="text/css" href="{% static 'django-stats-middleware/css/default.css' %}" />

About

Django Middleware for reporting very simple lightweight timing stats at end of ever page.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published