One database table, one model, many behaviours: Proxy model with Ronald Maravanyika
Published November 22, 2023
This video features Ronald Maravanyika at DjangoCon US 2024 in Durham, North Carolina, USA.
In most cases performance issues are caused by a very small fraction of the application. Identifying these bottlenecks can be a daunting task. Well, not anymore, we now have tools to easily identify these bottlenecks. In this talk we will talk about it all: the why, what and how to do profiling and benchmarking.
We will look at django-silk for profiling while for benchmarking we will be using the ever reliable pytest-benchmark. We will cover the basics and slowly move into seeing things like the actual raw query buried which is buried in Django. Lastly l will share tips on how to avoid over running your services while try to optimise. This talk is suitable for intermediate to senior developers, however junior developers can also benefit.
This talk was presented at: https://2024.djangocon.us/talks/unlocking-performance-benchmarking-and-profiling-django-for-maximum-efficiency/
LINKS:
Follow Ron Maravanyika 👇
On X: https://x.com/ronn_zw
Website: https://ronnzw.github.io/
Follow DjangoCon US 👇
https://fosstodon.org/@djangocon
https://x.com/djangocon
Follow DEFNA 👇
https://www.defna.org/
Video production by the presenter and DjangoCon US 2024 volunteers.
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.
Summarised automatically from the transcript.
Automatically transcribed, so expect mistakes in names and technical terms.
Hello, Django Con US twenty twenty four. Hopefully you are enjoying the conference so far. My name is Ron Malarnika and today I'll be speaking about unlocking performance. uh using benchmarking and profiling to gain the maximum efficiency from your Django application. Um so let me start by introducing myself. My name, uh like I said, my name is Ronald Maravanica And I am a co founder of a nonprofit called Simple Pie.
I'm also a che for PyCon Zimbabwe 2024, which will be later this year in November. You're all welcome to join us. Uh I've been a Django girls organizer for a number of years now and I've been a member of the Django Software Foundation. Uh I think since 2017 if I'm not mistaken, and I'm also a proud member of the Black Python devs community. Um so let's look into the talk itself. Uh I'll start by introducing the topic And then I'll move on to
uh 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 uh gaining efficiency. Uh uh how we are going to gain efficiency By using benchmarking and profiling as well. Um, so let's look at why am I talking about this, right
why a d did I decide to do this talk? So a few years ago I found myself in charge of a huge uh video application for a company. And this application it was very, very uh 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 uh developer I wanted the way that
will allow me to To really see what is going on underneath, to really see if my application is performing to the best standards that I think it it it was performing. So this is when I started to looking at uh options that we have around. And that's where I stumbled upon the uh benchmarking and profiling. Of course I had done it before um in school and things like that. But really practically this was the the first point where I was looking at it So, what was happening now
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? Uh 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
might mean you now have uh 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 um memory consumption, uh you want to have uh fewer demands on the network. or on the database. So firstly define what you are calling efficiency
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 uh 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 um 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 a little bit more code when you're doing that you are bound to introduce uh bugs. So to avoid to do that, um we will look at
three things that I've identified that I think they 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 uh the the small units, right? Then It is also very important for you to run your test before uh benchmarking. So you should ideally do benchmarking after you have done uh
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 um 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 on stop when when your program is finished running Then user we are looking at your central processing unit
time spent uh in 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. Uh this is very common if you look around benchmarking. Uh the time it module it will run a snippet of code for a number of times and then gives you the best uh time out of uh the number of runs that it is
made So um this tool is appropriate to accurately time small statements, um, 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 uh a framework, I'm not dealing with applications. So um 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.
And I had to change to check the change log. It is a fairly stable. Um library that you can use and it is itself by itself it is just a plugin uh 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. Um Settings so you install it in that way. You need uh PyTest obviously and PyTest Django, then you install PyTest uh benchmark Once you do that to use it, um
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 uh text that you wanna that you want to do And then you pass the actual uh function that you wanna that you want to run. And if you are working with methods, you'd need an extra uh two-code wave. Again, it is a fixture Um and you need to specify it so you need to install it uh using your uh benchmark
uh 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 um gate the wave attributes and then you pass in uh 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, uh that's the object. So to specify w we want to test um internal you just specify the internal um there are a whole lot of settings that you can do
uh with the wave uh like um lazy course to true and things like that Um, so this is what you would do if you want to do uh benchmarking uh 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 um a PyTest uh command And you see that after the PyTest command is given you uh 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 uh the particular
uh functions and methods that you have specified you have run and they'll have a specific name of uh that object that you are running uh then the time so now you have got you you have got an understanding of how the the time uh how much time it is actually um using to run your uh 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.
Um if you are using the uh 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, um and um um you are free to to use it So this is basic the the basics of how you do benchmarking. So now we have the time that it takes For you to to run. Okay. Now we'll move on to profiling. Okay. So profiling itself
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 um uh on an application and monitors how long each function takes to execute. Um by so doing it will identify where your application is spending its most time uh in. So you will need a profile, a profiler to do profiling
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, uh the time, uh and the speed as well. So You now have those uh at your disposal when you have benchmarks and profiles um used together So you've got a couple of uh standard libraries out there. Um In this case I've looked at two. There is the profile module and then there is the C
profile module. The C profile module is the most common one. and it is written in C um like you may tell um 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 uh data analysis and things like that. But when it comes to Django uh I U yes you can use this and I'll show you the outcome um that it it will show you. Sorry, I'm going back Um so it will show you an outcome which is like this.
So the 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 uh end calls, through time, cumulative time, um uh per core time, then line number files and each of those ones have been um listed there. So What if you don't want to use C profile? For me specifically I like to use a tool called uh
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 um application. So it is a live profiling tool, live meaning if if if you are doing tasks, it is profiling your 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 uh that's um on the from a queue we'll look at uh how to you can set it up. So you install it using pip You add it to your
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. Uh it comes with all its own static files, so you need to collect your static uh 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
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 the execution time. Um pay pay 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. Um if 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
star There is a whole lot of documentation um 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 um view of what is happening underneath just by clicking um a few things. You also have your tresp to understand uh which Files are being run, are they models, are they views, uh, are they templates and whatnot? So now
because we have that holistic view It is much easier for you to um to identify your your bottlenecks. But what if you want to to uh use the C profiler. You can easily um add that uh using the uh the turning on the Silk Python profiler. You can also turn the its binary version, which will give you an image Um 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.
Um do you want to drill down? Do you want to see what is going underneath? In some cases you 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 uh method. You
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, um it 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. Um then when you do that, like I said, it is a life profiler.
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 uh things The the the the other request, you know the main bunch request you can drill down into seeing uh what exactly is happening the queries that are running the joins that are there and things like that so even at a smaller smaller level you are able to identify those things that are running Which is really good um to to to be able to have all this information at your disposal
So, um, 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
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, uh where other resources are being consumed, be database, be networking resources and whatever. Now we are optimizing uh to your specific things where you want to to your gain. Then you do another benchmark as to compare After that, you establish what are your performance uh baselines and things like that. So, in summary Um benchmarking and profiling you can use it to identify bottlenecks, you can use it to identify resource consuming things.
And you now because you have got an understanding of what's happening underneath, you can easily scale your application. You have uh 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. Um that is all that I had for you. Thank you so much uh for the opportunity to present my talk Um, 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.
Efficiency depends on the resource you are trying to improve: speed, memory, network, database usage, or cost. Define that goal first, since making an application faster can increase its memory or other resource consumption.
Discussed at 4:08Benchmarking means running code and measuring how long it takes. The talk covers command-line `time`, Python’s `timeit`, and the `pytest-benchmark` plugin for measuring Django tests and functions.
Discussed at 5:42Install pytest, pytest-django, and pytest-benchmark, then pass the benchmark fixture into a test and give it the function or method to measure. Running the test produces timing statistics such as the minimum, mean, and standard deviation.
Discussed at 9:29Benchmarking focuses mainly on elapsed execution time, while profiling examines resource consumption and shows where functions spend their time. Used together, they can reveal issues involving time, memory, network, and database resources.
Discussed at 14:05Install Django Silk, add it as an application and middleware, run its migrations, collect static files, and visit the Silk URL in the application. Silk then records requests and lets you inspect their execution details, queries, and call information.
Discussed at 18:00Make the functionality work, refactor and plan the tests, run tests and an initial benchmark, then profile to find resource bottlenecks. Optimize the identified problem, benchmark again to compare results, and establish performance baselines.
Discussed at 23:28Note: We understand that names change, people change, and bodies change. We respect each individual's journey and privacy. If you have any concerns about a video or need us to remove content, please don't hesitate to contact us. We will handle your request with care and promptly address any issues.
Published July 15, 2026
Published July 15, 2026
Published July 15, 2026
Published July 15, 2026
Published July 15, 2026
Published July 14, 2026