Published on

Assessing the Performance of Django Applications with Blackfire

Authors

In today's blog post, I'll be introducing Blackfire and how you can use Blackfire to assess the performance of your Django applications.

Blackfire is a code performance and observability solution that helps improve web applications performance at various steps in a software development life cycle: from development to test, staging and production. It's the complete solution for the monitoring and profiling of your web applications.

Blackfire provides support for PHP and Python monitoring and profiling. It also supports Go profiling. It can also be used to carry out performance testing anywhere in your QA process.

In this blog post, I'll be demonstrating how to monitor and profile Django applications with Blackfire.

Blackfire Installation Process

Blackfire has a number of uses from monitoring to profiling to even testing. In this section, I'm going to explain how to profile a Django application on Blackfire.

In order to be able to profile a Django application on Blackfire, we need to install the following.

  • Create a free Blackfire account

  • To install Blackfire, follow the steps here

After installing Blackfire, run the following command to check if you have the blackfire-python module installed.

blackfire-python

After confirming if you haveblackfire-pythonmodule installed on your machine, the next thing needed is to set up the Django project.

Setting up the Django project

To set up the Django project on a machine, you need to follow these steps:

  • Clone the project, here

  • Make sure you have postgresql installed on your machine because the Django project uses

  • Make sure you have python 3.9 installed on your machine.

  • Install pipenv

pip install --user pipenv
  • Install the project requirements using pipenv
pipenv install
  • Make sure the docker daemon is running and run the following command in the project folder to start the PostgreSQL container
docker-compose up -d
  • Create a database and role for the project by running the following commands:
createdb bigfoot && createuser -s bigfoot
  • Run the following commands in the project folder to generate fake data for the project:
# Run the following command in the pipenv shell
python manage.py migrate
python manage.py generate_fake_data
  • Run the project server with blackfire-python
blackfire-python python manage.py runserver

Navigate to http://127.0.0.1:8000/ to see the bigfoot project running live in your browser.

Profiling the Django Project

Now that we have installed Blackfire and set up our project, the next thing to do is to profile our project. Blackfire can do more than just profiling, we can run test cases and create test scenarios. In order to do more with Blackfire, you'll need to create a.blackfire.ymlfile in the project folder to write test cases and scenarios. For the purpose of this tutorial, we are going to profile the Django application we have running on a local machine. The easiest way to profile a project is via the Blackfire chrome extension. Here is how to profile a Django project with the Blackfire:

  • Install the Chrome extension. After that, you should see the Blackfire logo in your browser extension tab.

  • To profile, make sure the application server is running, you have installed Blackfire and the Python probe. Click on the Blackfire logo and then click Profile.

  • After clicking onProfile,it takes a few seconds to profile and the information about the application will be displayed at the top of your screen.

  • Click on View Timeline to see the time it took each function in your application to fire.

My favourite Blackfire feature is the recommendations feature. It is located on the tab on the left-hand side just after the metrics tab. Blackfire automatically recommends fixes to your application based on best practices and the result from your application profiling.

After profiling the application, Blackfire suggested these recommendations, which would lead me to believe that my application performance can be improved by doing the following:

  • Enabling Django cached loader on production.
  • EnablingDjango DB persistent connections on production.
  • Execute fewer SQL queries. Current SQL queries are 26 but they should be less than or equal to 10.
  • Django DEBUG flag should be false on production
  • Less ORM entities should be created. The current ORM entities created are 516 but according to Blackfire, it should be less than or equal to 50.

If I make these improvements to my application code, i should have a more performant application.

There are so many other capabilities of Blackfire like profiling automation, performance testing, executing test scenarios etc. To see more capabilities of Blackfire, watch this short video.

Conclusion

In today's blog post of the 7days of Platform.sh series, I discussed what Blackfire is and the awesome capabilities that it has. I configured and set up a Django application for Blackfire and profiled it. On the next day of the series, I will be discussing how to set up and deploy a NextJS application on Platform.sh.