Easily add cProfile profiling to django views.
Project description
django-cprofile-middleware
This is a simple profiling middleware for Django applications. I wrote it because I got tired of printing "start" "stop" "stop 2" in my programs to find the bottlenecks.
I found a simple example on @dcramer's slideshare and modified it to support sorting.
Installing
$ pip install django-cprofile-middleware
Then add django_cprofile_middleware.middleware.ProfilerMiddleware
to the end your MIDDLEWARE
in settings.py. This option was called MIDDLEWARE_CLASSES
in versions of Django before 1.10.
For example:
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'startup.do.work.FindProductMarketFitMiddleware',
...
'django_cprofile_middleware.middleware.ProfilerMiddleware'
)
The profiler will only be available when the Django setting DEBUG
is set to True
. By default it's also required to be an authenticated user with is_staff
set to True
which is making the request to be profiled. The is_staff
check can be configured as follows:
DJANGO_CPROFILE_MIDDLEWARE_REQUIRE_STAFF = False
Running & Sorting Results
Once you've installed it, log in as a user who has staff privileges and add ?prof
to any URL to see the profiler's stats. For example to see profile stats for http://localhost:8000/foo/
, visit http://localhost:8000/foo/?prof
.
You can also pass some options:
count: The number of results you'd like to see. Default is 100.
sort: The field you'd like to sort results by. Default is time
. For all the options you can pass, see the docs for pstats.
download: Download profile file, that can be visualized in multiple viewers, e.g. SnakeViz or RunSnakeRun
Enjoy!
Email me with any questions: omar.bohsali@gmail.com.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file django-cprofile-middleware-1.0.5.tar.gz
.
File metadata
- Download URL: django-cprofile-middleware-1.0.5.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
b942185a38f3b582935a55c768f126ce9a6f0cefceee3b5d19e6b307ad129889
|
|
MD5 |
81981ba4b8a8f120a73aff927bd97ac5
|
|
BLAKE2b-256 |
1bb000ed4daccce442c73a3513e07ac1896f3d8dbb5c2eece836199e4fa17cb0
|