Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to profile django channels?

My technology stack is Redis as a channels backend, Postgresql as a database, Daphne as an ASGI server, Nginx in front of a whole application. Everything is deployed using Docker Swarm, with only Redis and Database outside. I have about 20 virtual hosts, with 20 interface servers, 40 http workers and 20 websocket workers. Load balancing is done using Ingress overlay Docker network.

The problem is, sometimes very weird things happen regarding performance. Most of requests are handled in under 400ms, but sometimes request can take up to 2-3s, even during very small load. Profiling workers with Django Debug Toolbar or middleware-based profilers shows nothing (timing 0.01s or so)

My question: is there any good method of profiling a whole request path with django-channels? I would like how much time each phase takes, i.e when request was processed by Daphne, when worker started processing, when it finished, when interface server sent response to the client. Currently, I have no idea how to solve this.

like image 699
Valar Avatar asked Dec 26 '16 12:12

Valar


People also ask

What is the difference between Django and channels?

It’s built on a Python specification called ASGI. Channels builds upon the native ASGI support in Django. Whilst Django still handles traditional HTTP, Channels gives you the choice to handle other connections in either a synchronous or asynchronous style.

How to configure channel_layers in Django?

Then Configure the CHANNEL_LAYERS by setting a default backend and routing: This uses a Redis backend which is also needed in production. Normally, Django uses HTTP to communicate between the client and server: The client sends an HTTP request to the server. Django parses the request, extracts a URL, and then matches it to a view.

How do I start a Django project?

Install Django, Django Channels, and ASGI Redis, and then create a new Django project and app: NOTE: During the course of this tutorial, we will create a variety of different files and folders.

How does Django communicate with the server?

Normally, Django uses HTTP to communicate between the client and server: The client sends an HTTP request to the server. Django parses the request, extracts a URL, and then matches it to a view.


1 Answers

Django-silk might be helpful to you in profiling the request and database searching time with following reasons:

  1. It is easy to set by simply adding the configs on settings.py of your Django project.
  2. Can be customised: by using the provided decorator, you can profile the function or methods and get their running performance.
  3. Dynamic setting: you can choose to dynamically allocate silk to methods and also set the profiling rate you want during the running time.

As the documentation states:

Silk is a live profiling and inspection tool for the Django framework. Silk intercepts and stores HTTP requests and database queries before presenting them in a user interface for further inspection

Note: silk may double your database searching time, so it may cause some trouble if you set it on your production environment. However, the increase from silk will be shown separately on the dash board.

https://github.com/jazzband/django-silk

like image 67
Lord Vice Avatar answered Oct 13 '22 23:10

Lord Vice