Django cProfile middleware having configurable triggers and outputs
Django-1.10 or above (For below version 1.10 refer this package)
Follow given instructions to setup customizable-django-profiler
Install via pip
$ pip install customizable-django-profiler
Add customizable_django_profiler.cProfileMiddleware
to the end of MIDDLEWARE
in project's settings.py
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
....
'customizable_django_profiler.cProfileMiddleware',
]
Add PROFILER
in project's settings.py
and set activate = True
.
PROFILER = {
'activate': True,
}
Done! This will provide the profile data on the server console
Example
622 function calls (579 primitive calls) in 0.001 seconds
Ordered by: internal time
List reduced from 207 to 100 due to restriction <100>
ncalls tottime percall cumtime percall filename:lineno(function)
... ... ... ... ... ...
... ... ... ... ... ...
You can customize the Profiler settings via adding some varaibles in Profiler
key in settings.py
Default are
PROFILER = {
'activate': True,
'sort': 'time',
'count': '100' ,
'output': ['console'],
'file_location': 'profile.txt',
'trigger': 'all'
}
Description of cutomizable keys
Set this key to True
to enable Profiler
'activate': True
To disable set to False
'activate': False
Sort according to the set value. Default is 'time'
.
See documentaion for more options
Specify number of rows to output. Default is 100
.
Specify the form of output. Multiple output formats can be selected. Default is ['console']
. Options are 'file'
, dump
and 'response'
.
'file'
and dump
will write the file specified by 'file_location'
key and 'response'
will output the result as response of request.
file
is used for a txt file.
dump
will be used for binary dump.
Some examples are
'output': ['console']
'output': ['console', 'file', 'response']
'output': ['file', 'response']
Specify the location of file you want to write in the results. Only valid if 'file'
in 'output'
key. Default value profile.txt
Specify the trigger for API on which profiler runs. Default is 'all'
. Instead you can trigger profiler by passing a query parameter, which can be specified after :
in 'trigger'
key for example
'trigger' : 'query_param:profile'
Profiling will only be enabled for APIs with profile
in their request parameters.
http://localhost:8000/api/?profile
This project is licensed under the MIT.