# Unlocking Performance: Benchmarking and profiling Django for Maximum Efficiency with Ron Maravanyika

Source: https://djangotv.com/videos/djangocon-us/2024/unlocking-performance-benchmarking-and-profiling-django-for-maximum-efficiency-with-ron-maravanyika/
Date: 2024-12-06
Speakers: Ronald Maravanyika

## Summary

Ron Maravanyika explains how to define application efficiency before measuring it: faster execution may increase memory, database, or network costs, so teams should decide which resources matter. He distinguishes benchmarking, which measures how long code takes to run, from profiling, which shows where time and resources are spent, and demonstrates tools including Unix `time`, Python’s `timeit`, `pytest-benchmark`, `cProfile`, and Django Silk. He recommends testing and refactoring first, benchmarking small units, profiling requests and suspected code paths, fixing identified bottlenecks, and benchmarking again to establish performance baselines and make data-driven scaling decisions.

## Transcript

### [00:00] Introduction

[00:21] Hello, Django Con US twenty four. Hopefully you are enjoying the conference so far. My name is Ron Malarnika and today I'll be speaking about unlocking performance. Using benchmarking and profiling to gain the maximum efficiency from your Django application. So let me start by introducing myself. My name, like I said, my name is Ronald Maravanyika And I am a co founder of a nonprofit called Simple Pie.

[01:06] I'm also a che for PyCon Zimbabwe 2024, which will be later this year in November. You're all welcome to join us. I've been a Django girls organizer for a number of years now and I've been a member of the Django Software Foundation. I think since 2017 if I'm not mistaken, and I'm also a proud member of the Black Python devs community. So let's look into the talk itself. I'll start by introducing the topic And then I'll move on to

[01:51] Talking about benchmarking and the options that we have in benchmarking. I'll then move on to profiling. And also look at the options that we have in profiling. I will hone down more on Silk, Django Silk, which is one of the profilers that we will be talking about today. Then I will be talking about gaining efficiency. How we are going to gain efficiency By using benchmarking and profiling as well. So let's look at why am I talking about this, right

[02:36] why a d did I decide to do this talk? So a few years ago I found myself in charge of a huge video application for a company. And this application it was very, very big for the business because it was being used mostly by executives. to deliver their messages to the business. So I was very, very nervous and at that time me being an intermediate developer I wanted the way that

[03:22] will allow me to really see what is going on underneath, to really see if my application is performing to the best standards that I think it was performing. So this is when I started to looking at options that we have around. And that's where I stumbled upon the benchmarking and profiling. Of course I had done it before in school and things like that. But really practically this was the first point where I was looking at it So, what was happening now

### [04:08] Defining Application Efficiency

[04:08] at that time is I wanted to be able to see if the application is fast that is what I was concerned about. But the things that I did not consider was what exactly is An application that is fast. So the first thing, if you are going to be dealing with benchmarking and profiling, look at the efficiency. Are you trying to improve speed? Are you trying do you have the time? Right? So there are different resources that you might be looking at when you are Looking at benchmarking and profiling. So at times improving speed

[04:54] might mean you now have huge consumption of memory and other resources. Do you need that? Do you have the budget for that? Things like that. At times you don't have the time to be able to make your application as efficient as it is supposed to be maybe because it is not a priority or the feature that you are working on is not a priority. At times you are looking at reducing the costs when it comes to memory consumption, you want to have fewer demands on the network. or on the database. So firstly define what you are calling efficiency

### [05:42] Benchmarking Fundamentals

[05:42] Okay, so now that you have that defined, now let's look at benchmarking. What exactly are we talking about? So benchmarking is literally just watching your script run and timing the time that it takes to run. Okay, this is what Benchmarking is and we 'll look at some of the tools that you can use for that. But before we do that again It the moment you are trying to see how fast your code is and you are writing a little bit more code when you're doing that you are bound to introduce bugs. So to avoid to do that, we will look at

[06:27] three things that I've identified that I think they will be very useful. So the first thing is when you are doing benchmark at that point you should have your test plan in place You should have your test suite in place. Then you need to organize your test as in unit test. So you need to do benchmarking as part or just after your unit test Because mostly you wanna do benchmarking on the small units, right? Then It is also very important for you to run your test before benchmarking. So you should ideally do benchmarking after you have done

[07:13] refactoring. So to begin benchmark, the easiest tool that you can use is timing. It's a Unix tool that you can run on your command line, specify your file, and it will output three values. The real The user and the sys. The real is the actual time that the process is taking from start to finish. So it's like you have a stopwatch you click on run, you start your stop while they click on stop when your program is finished running Then user we are looking at your central processing unit

[07:58] time spent in the computational process of the thing. Then you have the CIS, which is looking at the cumulative time spent by the CPU. Usually this is the time around memory allocation and stuff like that. Okay, so that's the basic tool that you can use. We also have time it module. This is very common if you look around benchmarking. The time it module it will run a snippet of code for a number of times and then gives you the best time out of the number of runs that it is

### [08:44] Pytest Benchmarking

[08:44] made So this tool is appropriate to accurately time small statements particularly in an isolation Okay, so that's the other tool that you can use. But all these to me they sounded like scripts, I'm running scripts, I'm not dealing with a framework, I'm not dealing with applications. So I looked around and I found something called PyTest Benchmark The reason why honestly I stumbled upon it is because it has got the word PyTest in it.

[09:29] And I had to change to check the change log. It is a fairly stable. Library that you can use and it is itself by itself it is just a plugin that provides the benchmark fixture so you see we use a lot of fixtures in here and You really don't need to set it up, all you need to do is to install it and then set up PyTest. Settings so you install it in that way. You need PyTest obviously and PyTest Django, then you install PyTest benchmark Once you do that to use it,

[10:14] this code might be familiar to you if you use PyTest. So to use it, all you need to do is to pass it as one of the arguments in your text that you wanna that you want to do And then you pass the actual function that you wanna that you want to run. And if you are working with methods, you'd need an extra two-code wave. Again, it is a fixture and you need to specify it so you need to install it using your benchmark

[11:00] Asset And you can now it is now it will be now callable from the benchmark. So again, you pass benchmark as an argument in your method then you gate the wave attributes and then you pass in your method that you want to test. So you can pass it as part of the class. So in this case you can see we have the class 4, that's the object. So to specify w we want to test internal you just specify the internal there are a whole lot of settings that you can do

[11:46] With the wave like lazy course to true and things like that so this is what you would do if you want to do benchmarking for functional method, and what you would see is an output that is pretty much like this. So to run it you run it like you are running a PyTest command And you see that after the PyTest command is given you the number of tests that you've passed, you see at the bottom of the line the benchmark You have got the maximum, the mean, the standard deviation and things like that. So this is the actual time that the particular

[12:31] Functions and methods that you have specified you have run and they'll have a specific name of that object that you are running then the time so now you have got you have got an understanding of how the time how much time it is actually using to run your functions and methods. So one of the questions that I often get is where does it fit in the folder structure of Chiango? Well that depends on you but normally for me I'd like to edit in your root directory so where your applications are, but in the root directory.

### [13:19] Profiling and Resource Usage

[13:19] If you are using the cookie cutter template. Just edit within where your applications are. So that directory where you are editing your applications, add in the benchmark module in there and add everything in there But obviously this depends on you, and you are free to use it So this is basic the basics of how you do benchmarking. So now we have the time that it takes For you to run. Okay. Now we'll move on to profiling. Okay. So profiling itself

[14:05] Unlike benchmarking where we are just looking at the time, profiling, we are looking at the consumption of resources. This is the main difference between the two. We are not only looking at the time, we are but we are specifically looking at the resources aspect and we do that through a profiler. A profiler is simply A program or code that runs on an application and monitors how long each function takes to execute. By so doing it will identify where your application is spending its most time in. So you will need a profile, a profiler to do profiling

[14:54] Okay, so the main difference is that the other one is just looking at the time, the other one is looking at the resources, consumption of your resources. So if you remember when we defined what we are calling efficiency you now have the your memory, your network, the time, and the speed as well. So You now have those at your disposal when you have benchmarks and profiles used together So you've got a couple of standard libraries out there. In this case I've looked at two. There is the profile module and then there is the C

[15:41] profile module. The C profile module is the most common one. and it is written in C like you may tell and It is a general purpose profiler. You can use it for multiple things. You can use it for raw Python code. You can use it for scripts. You can even use it in data analysis and things like that. But when it comes to Django I U yes you can use this and I'll show you the outcome that it will show you. Sorry, I'm going back so it will show you an outcome which is like this.

[16:26] So the way you would use it is you would write it Is part of your middleware. So it has to get your request and responses. So there are multiple ways that you can do that, and there is a lot of documentation around that. So it will give you end calls, through time, cumulative time, per core time, then line number files and each of those ones have been listed there. So What if you don't want to use C profile? For me specifically I like to use a tool called

### [17:14] Django Silk

[17:14] Django C is a 30-part package. Which is being supported by the Just Band team. Kudos to you, Just Band team. Really like this application. So it is a live profiling tool, live meaning if you are doing tasks, it is profiling your requests it is profiling the client, the response and everything as you do them. So it is On the jar it is an application that's on the from a queue we'll look at how to you can set it up. So you install it using pip You add it to your

[18:00] middleware, and like I said, the silk is an app, so you need to install it. After you install it, you need to run your migration. It comes with all its own static files, so you need to collect your static files And then to actually use it you just go to your URL then just say slash you know your home URL just say slash silk And boom, voila! Now you are monitoring your request as they come through. So you have all your requests coming through. You can narrow down into your request. By simply clicking on the particular request, you are able to see much more information about the

[18:49] request. And you can move through the tapes, right? For example, if I want to see the XQ tab, this is the information that I'm getting. I'm seeing the number of joints that are the execution time. Pay query that's running. You can actually select each of them and drill down a bit more. Now you are seeing the raw query that is being run. Right. If you use OAuth you are now seeing that this is the OAuth application and these are the queries that are running. You now have your tables and whatever that is in D so in my case I saw something that was slowing down my application which was select

[19:37] star There is a whole lot of documentation in Postgres around Select Star So immediately I was able to see okay so this is a potential risk of why my application is slow. Now I'm optimizing that. So this is really cool to have this overview view of what is happening underneath just by clicking a few things. You also have your tresp to understand which Files are being run, are they models, are they views are they templates and whatnot? So now

[20:22] because we have that holistic view It is much easier for you to identify your bottlenecks. But what if you want to use the C profiler. You can easily add that using the turning on the Silk Python profiler. You can also turn the its binary version, which will give you an image let me show you what that looks like. So you get an image like this and it will tell you the parallel functions that are running and whole lot of things depending on your use case.

### [21:09] Targeted Code Profiling

[21:09] Do you want to drill down? Do you want to see what is going underneath? In some cases you will see that there are two processes running in parallel and You know, the you now have more information on how to optimize your application So what if you want to test small chunks of code? Because sometimes you have a suspicion that you think this line of code is where the problem is coming. From so there are two methods that you can do with silk. So you have the decorator method, and you also have the context manager method. So this is your decorator method. You

[21:55] specify your profiler after importing it, obviously, then you give it a name so that you'll be able to see it. In the UI for silk. So that view blog post is what we are going to be checking. So now you are profiling your specific function. Then for decorators, it is good to use width. Now we are at the lower level. You now want to monitor maybe it's not a wall function, it's just a small piece of the wall function that you want to monitor. Then when you do that, like I said, it is a life profiler.

[22:41] Under the profiler tab, you are now able to see all those smaller things that you want to monitor and like what you are doing with the other things The other request, you know the main bunch request you can drill down into seeing what exactly is happening the queries that are running the joins that are there and things like that so even at a smaller level you are able to identify those things that are running Which is really good to be able to have all this information at your disposal

### [23:28] The Optimization Workflow

[23:28] So like I specified, you have two profilers that we have. There's also another one called dynamic profiling. Dynamic profiling usually you are using it for something that you don't have access to. It's outside the scope of this talk, but you should definitely look into it So, the overall process that I would recommend for you to follow is firstly make sure your application is running so you have to functionality in place, refactor your functionality b even before you do your testing, plan your testing, test, then you do the first

[24:13] set of benchmarking. Then you will do profiling. After you do profiling, you have identified the bottlenecks where your memory is consumed. where your time is consumed, where other resources are being consumed, be database, be networking resources and whatever. Now we are optimizing to your specific things where you want to your gain. Then you do another benchmark as to compare After that, you establish what are your performance baselines and things like that. So, in summary benchmarking and profiling you can use it to identify bottlenecks, you can use it to identify resource consuming things.

[25:02] And you now because you have got an understanding of what's happening underneath, you can easily scale your application. You have data driven decisions that you have. So if you want to request for more resources from the exec team, you can use the data to do that. That is all that I had for you. Thank you so much for the opportunity to present my talk if you have questions, follow me on social media. You can also drop me an email and I'll gladly respond to you. Thank you.
