A lot of people have written middleware classes for Django profiling over the years. Here are some of the ones I directly took inspiration from:
- This ProfileMiddleware gist disables itself when DEBUG is False, returns either a summary table or the raw profiling data file, and uses the cProfile profiler
- This other ProfileMiddleware got really fancy with the sorting options, but lacks a couple of the other features the one above has
- geordi introduced using Graphviz and Gprof2Dot to generate call graph PDFs
- This ProfileMiddleware uses the older hotshot profiler and writes the raw profiling data of all requests while active to disk
- This other ProfileMiddleware still uses the hotshot profiler, but uses a GET parameter and returns the profiling data summary in the browser
- And this ProfileMiddleware again uses the hotshot profiler and a GET parameter, but is a little fancier with the summary formatting
- This ProfilerMiddleware was one of the first ones to use cProfile; GET parameter and in-browser data summary
- And this variation on the previous ProfilerMiddleware added the ability to sort the summary by the specified column
- ProfilerMiddleware was packaged fairly nicely, uses cProfile, and allows sorting of the summary table
Like the geordi middleware, this one uses Gprof2dot to generate call graphs. There doesn’t seem to be a current version of it nicely packaged for automatic installation (a rather old version is on PyPI but doesn’t appear when searching the site). I’ve opened an issue for this, but haven’t received a response yet. In the meantime, I’ve bundled the script in the yet-another-django-profiler distribution for ease of installation.
Graphviz is the graph visualization library used by Gprof2dot to do the graphics work. It’s a native library that needs to be installed in a manner typical for the system it’s running on, so that needs to be done separately from the installation of this library (but isn’t strictly necessary if you just plan to use the statistics tables and not the call graphs).
sbo-sphinx contains some Sphinx extensions and common configuration for the documentation of this and other libraries being developed at Safari Books Online. You don’t need it to run the middleware, but you’ll want to install it if you use Sphinx to build the documentation on your system.