Table of contents
1. Basic installation and configuration
2. Add the following to the project's settings.py file:
3. Add silk middleware in MIDDLEWARE, and add silk application in INSTALLED_APPS.
4. Configure silk in the root urls.py file of the project
7. Access the silk application:
Two, Profiling (analysis) configuration and use
1. Configure SILKY_PYTHON_PROFILER to True
3. Send an interface request, open the silk application to view the Profiling options
django-silk is a lightweight Django application performance monitoring tool that can help you understand Django application performance bottlenecks, database queries, and other issues. It can be used in projects where django front and back ends are separated, and the performance can be monitored directly by requesting the background API interface . The following are the steps to use django-silk:
1. Basic installation and configuration
1. Install django-silk:
pip install -i https://pypi.douban.com/simple django-silk
settings.py
2. Add the following to your project's files:
MIDDLEWARE = [
# ...
'silk.middleware.SilkyMiddleware',
]
3. MIDDLEWARE
Add the silk middleware in , and INSTALLED_APPS
add silk
the application in .
INSTALLED_APPS = [
# ...
'silk',
]
4. urls.py
Configure silk in the root file of the project
urlpatterns += [
path('silk/', include('silk.urls', namespace='silk')),
]
Include silk's URL in the main URL configuration.
5. Execute the migration
python manage.py makemigrations
python manage.py migrate
6. Run the silk application:
Start the application, and visit the URL configured by silk (default is ) in your browser /silk
. This will launch silk's Dashboard page.
7. Access the silk application:
Visit any page of django, silk will capture useful information about the request, such as request time, SQL query, HTTP error, cache hit rate, etc., and display them on silk's Dashboard.
You can also view detailed information about each request by clicking the links in the lower part of silk's Dashboard, including URL, request parameters, request headers, response time, SQL query, template rendering time, and other related information.
You can also click SELECT in the above figure to view the specific SQL execution
Two, Profiling (analysis) configuration and use
Django Silk's Profiling feature can help you find performance bottlenecks and call sequences in your code for better code optimization. It can record the response time of each request, SQL query, cache query and some other details that can help you understand the performance status of your application.
When you open Profiling for the first time, you will be prompted how to configure it.
Continue to configure as shown above
1. Configure SILKY_PYTHON_PROFILER to True
SILKY_PYTHON_PROFILER = True
2. Add a decorator
Add to the method you want Profiling (analysis)@silk_profile装饰器
from silk.profiling.profiler import silk_profile
class TestView(View):
@silk_profile() # 为get请求添加装饰器
def get(self, request):
areas = Area.objects.all()
return HttpResponse(areas)
3. Send an interface request, open the silk application to view the Profiling options
Click the image above to display the get request to enter the Detail page
You can also view the Queries (query) option to view SQL related
Then click the SELECT query in the above figure to view the executed SQL statement
Note: When using django-silk, we encourage you to use it in test and development environments. It is not recommended to be used in a production environment. For example, in a production environment, you can set the settings of django-silk to only display when a specific cookie is used during access, and prevent any sensitive information from being displayed.