# Load Testing a Django Application using LocustIO | Pranjal Jain & Vibhash Chandra

Source: https://djangotv.com/videos/djangocon-europe/2021/djangocon-2021-load-testing-a-django-application-using-locustio-pranjal-jain-vibhash-chandra/
Date: 2021-07-31
Speakers: Speaker 1, Pranjal Jain, Speaker 3

## Summary

Load testing measures an application’s capacity, response times, scalability, and resource use so teams can find bottlenecks rather than functional bugs. The speakers explain how to install Locust, define simulated users and weighted tasks in Python, run tests through its web UI or headless mode, and inspect metrics such as request rates, percentiles, failures, and response times using a Django quiz application. They also show distributed testing with a Locust master and multiple workers, and explain how task sets, parameterisation, correlation, and custom clients can model more complex applications and protocols. They argue that the right user load should be based on expected traffic or prior experience, with additional machines used when a single laptop cannot generate enough load.

## Transcript

### [00:09] Introduction to Locust

**Speaker 1:** [00:09] Hello everyone, I'm Vibha Hasht. I work as senior developer at ServiceNow. Today we are going to talk about road testing Django applications using Lukas Taiwan. For this talk, I have been joined by my former colleague Pranjil Jil.

**Pranjal Jain:** [00:28] Hey everyone, I am Pranjil. I work as a software development engineer at Amazon India. Okay, let me share my screen. Okay, so for this talk we assume that you have basic understanding of Django and Python. Okay. So I came to know about Locust Io when I was working on a web application sometime back At that time, our database update was failing in production under heavy user load. So we wanted to replicate this issue In our local dev

[01:14] environment, but we did not have load testing setup. So we were looking for something, some load testing tool or library which could perform load testing very like which was very easy to set up and did not require much perfectly load testing with locust io perfectly fits in this In locastyo, we can write Python scripts, run our run our tests, and get the results easily Okay. So one principle that I truly believe in is you cannot improve what you cannot measure.

### [02:01] Performance and Load Testing

**Pranjal Jain:** [02:01] And load testing is a phase and software development cycle, which perfectly fits in this, which is very closely related to this principle. Okay. So flow testing and stress testing come under the umbrella of performance testing In performance testing, we evaluate our system, our application, and get some metrics on speed and scalability of our system, response time of our application. Software and hardware resource usage. So in load testing, we check how much load our

[02:47] application can handle So the goal is not to find out any bug in our application or our system, but to eliminate the bottlenecks

**Speaker 1:** [03:02] When we are testing our application for its functional requirements, in those cases, usually we use a browser to simulate user behavior in giving in some scenarios and then we then we see whether is whether there is a deviation from the actual requirements to find out the bugs But in case of opponents testing and subsequently load testing, we don't have any browser, so we cannot actually check the UI. So, what we do is we test these types of tests are performed at protocol level. In these cases, we try to measure that what is the maximum capacity of the system.

### [03:47] Locust Features and Installation

**Speaker 1:** [03:47] See if that is that is we are trying to determine the maximum upper limit of the number of users systems can handle. In these cases, we won't be considering actions which are performed by browser like JavaScript execution and all those. So locust IO provides us similar features. So it allows us to do the third testing of web applications and APIs. It's an open source tool and as Pranjal mentioned that we can write our tests in Python That's where our main advantages. Once we have written our test, it provides us

[04:33] a web-based UI to actually start our test and see the results If we don't want to use web-based UI, we can r run it in headless mode also Setting up locast iO is fairly simple. We need we need any version of Python which is below 3. 9. It does not run with it does not run with Pi 2. 3. 9 and above. All we have to, if you want to install it, all we have to do is just run pip install locist command in our virtual environment. Once locust IO is installed, next step is to write a locust file.

### [05:18] Locust File Structure

**Speaker 1:** [05:18] So in these in this locust file we try to define our users and Write our scenarios V in form of Python classes which can be executed Once these this locust file has been written or to run the run the test, all we have to do is just run the locust command. Additionally, we can pass a host parameter which will which will which will define the application which we are trying to test. For example, in this case you can see mysite. com, which will be tested Let's look into the structure of locust file that we can create to

[06:05] start our load test So three main components of our locust file are task, task set, and users. Users actually represent the users object of user class represent the users that we are trying to generate during our load test So these users will perform certain tasks for a particular scenario. We can group these tasks to actually group these tasks to actually represent a particular scenario that we are trying to test. So these users are when we actually when we are actually running the load test these users are generated randomly And

[06:51] and b based on the scenarios that we have written, the tasks are performed But addiction in real life scenario, what happens that it may happen the users are users won't be Going through different scenarios in equal proportion, it may happen that some of the operations are performed more frequently than others So i in or i if you want to if you want to divide those tasks or scenarios in some sort of proportion Locustyo provides that option. So let's look into this piece of code where we have defined

[07:36] two tasks at one at line number six and another at line number 10 using task decorator. And additionally, we have provided a parameter. So this parameter three and five actually represent the weight of the task. It me it just means that these tasks will be executed in the proportion of five is to three.

### [08:00] Django Load Test Demo

**Pranjal Jain:** [08:00] Okay , let me show you a quick demo for Locus Tio. So for testing, for doing load testing, we have developed a very basic Django application Which is having some basic operations like login, logout, and a scenario where a user can see the questions and they will be given four choices and They can select one of the choice and submit their answer. The answers will be recorded and the scores will be calculated and shown on the dashboard. So we have one more feature in this that is that dashboard. So I'll show you what all things we have included in our locust

[08:48] file To do the load testing on our this basic Django application. So as told earlier, we have a user class that is a class website user here. So this user this is a simulated user and this will do all the this will generate the load Next, we have a user behavior class, which is a collection of all the tasks that is a and it is extending task set So in the task we have few things like few methods like on start and on stop. So As the name suggests, onStart method will

[09:34] perform all the things that we need to do when before we start doing load testing on our on our application. So here I'm performing a login operation. On start. So as it makes sense, before trying to test load on my Django application, I want to log into my application. Next thing on stop method. So on stop I'm performing.

**Speaker 1:** [10:02] We looked into one scenario where user was able to log in and fetch questions and Post responses for those questions. So we performed load testing for that particular scenario. Let's look into another scenario where user will log in and view the dashboard. So for the for creating a separate scenario, we could have we got we could have created a separate task set. In this file, we have not created a separate task test. Instead of that, we have included One more task at line number 31, where we have a method get dashboard. Okay. If you wanted to create a separate task set, we could have just included it in

### [10:48] Analyzing Test Results

**Speaker 1:** [10:48] At line number forty one. Okay, so for now we'll just I we have just com uncommented the get dashboard method And since we will comment out get question post-answer methods so that it is not included in our scenario. So let's start our load test again and see what are the results that we are getting. So we will we have started our load test with 50 users at same spawn rate of ten So slowly users have increased and now it's up to 50. So if we can see that we are getting some requests for dashboard also.

[11:34] So far there are 120 around 120 requests with median timers, median time of 23 milliseconds, and 98th percentile is 730 milliseconds, which means that 90 percent of requests. are under the this time. Average response time is 183 milliseconds and minimum is 11 Yes, we will run this test for I guess wait till four hundred or five hundred requests It will it will take some time. Meanwhile we can see that we have we are gonna

[12:20] we have got one failure with login. Let's look into the Failure chart, what is that? Okay, there is some server error. Maybe we can look into that later, but yeah, for now we can go back to our statistics and We have or slowly the as the number of requests is increasing, the average time response time is also increasing. Right now it's the two hundred. Okay. Maximum time is almost eighty-nine thousand that's that's using So

[13:06] instead of maybe four and five let's stop at three mark of three hundred requests Yeah, I think we can stop our load test here. And as soon as we stop our test log out Function is also called and if we go back to our console, we can see that snapshot of the test will be captured. So stop it for like yeah so whatever results we had seen on the dashboard that particular snapshot is captured here final Snapshot is captured here and we

[13:51] can see this result. So for this particular load test we when we were actually preparing for this, so Pranjil was saying that This is one scenario where we can actually get a better result. We looked into one scenario where user was able to log in and fetch questions and Post responses for those questions. So we performed load testing for that particular scenario. Let's look into another scenario where user will log in and view the dashboard. So for the for creating a separate scenario, we could have we got we could have created a separate task set.

[14:37] in this file, we have not created a separate task test. Instead of that, we have included One more task at line number 31, where we have a method get dashboard. Okay. If you wanted to create a separate Tas task it, we could have just included it in at line number forty one. Oh yeah. Okay, so for now we will just we have just come uncommented the get dashboard method and since we will comment out get question post answer methods so that it is not included in our scenario. So let's start our load test again and see what are the results that we are getting.

[15:25] So we will we have started our load test with 50 users at same spawn rate of 10 So slowly users have increased and now it's up to 50. So if you can see that we are getting some requests for dashboard also. So far there are 120 around 120 requests with median timers, median time of 23 milliseconds, and 98th percentile is 730 milliseconds, which means that 90 percent of requests. are under the this time. Average response time is 183 millisecond and minimum is 11 Yes, we will run this test for

[16:12] I guess wait till four hundred or five hundred requests It will it will take some time. Meanwhile we can see that we have we are gonna we have got one failure with Login. Let's look into the failure chart. What is that? Okay, there is some server error. Maybe we can look into that later, but yeah, for now we can go back to our statistics and We have or slowly the as the number of requests is increasing, the average time response time is also increasing. Right

[16:57] now it's the two hundred. Okay. Maximum time is almost eighty-nine thousand that 's that 's using So instead of maybe four and five, let's stop at three mark of three hundred requests. Yeah, I think we can stop our load test here. And as soon as we stop our test logout function is also called and If we go back to our console, we can see that snapshot of the test will be captured.

### [17:43] Application Models and Views

**Speaker 1:** [17:43] So stop it for like. Yeah, so whatever results we had seen on the dashboard, that particular snapshot is captured here. Final snapshot is captured here, and we can see this result. So For this particular load test we when we were actually preparing for this, so Pranjil was saying that this is one scenario where we can actually get a better result. So I we so we looked into that part. Let's look let's check what we have actually done in our views. py file and models So basically in this

[18:29] So basically in this in this get method in this get method we have We are just fetching all the test objects and returning it with the template. So let's look into the structure of test Model class. So test model class has a foreign key reference for users, some other fields like created date, end date, total time score, and active. This user class. So for this particular user class since what is happening is when

### [19:11] Distributed Load Testing

**Pranjal Jain:** [19:11] Okay, so we have our Django application up and running, our local server up and running, and we perform load testing on our application under multiple scenarios. So we did load testing for 50 users, but what if we want to increase the load on our application and increase the number of users to maybe say 500 or thousands or many more. So in this case, a single sub machine might not suffice. So for this to perform to test scalability of our system and check how much scalable our how much load test

[19:57] our system can handle under multiple machines Locust provides us a very out-of-the-box feature for load testing in distributed mode. So for this For this, we have a single locust master and multiple worker scenario configuration. What happens here is multiple workers generate the load. They perform a load test, they do load generation on our Application under the image of multiple simulated users and by default every three seconds they generate report and return

[20:42] submit those reports to the master The master, locust master collects all those reports and consolidates the data and shows us the results. So yeah. So for this we need to we need to run two commands. First of all, we need two locust files. one for master and one for worker. Those can be in multi same machine or maybe different virtual machines or whatever And we need to run the master command and mention the host of our application and the worker command. which will run workers and you need to link it to the master host.

[21:30] So you need to provide the host of your master So

**Speaker 1:** [21:36] I think yeah. Yeah one thing here so this master host is given as my site. com so they don't go by this. We it won't be in first case where we have provided host as my site. com. That is the application which we are trying to load test. But in the second one, we are actually trying, we are actually providing host name of the Machine where we are running our master low pest. So it can be it will be different for us

**Pranjal Jain:** [22:12] Yeah, correct. So to perform distributed load testing, we have we have run our applications in EC2 instances and yeah so one for master and we have run multiple workers here as you can see. This is the dashboard for distributed. It's everything same, just that you can check here the multiple workers are mentioned. Right now we have two workers running. I'll start performing load testing for say maybe 250 users at the spawn rate of 25 Yeah, let's start swarming and attacking our website.

[23:01] So as you can see, the logins have started, the question URLs and the answer URLs are getting hit And yeah, so the same way we have the response time The request per second here, everything is the same. You can check the charts here, real-time charts, the failures the exceptions and download the data in multiple formats. One extra thing here is you can check as you can see the workers tab here You can see the number of users distributed across both the workers, like we had entered 250 users

[23:46] and 125 users are working on each worker and the CPU users is mentioned. Yeah, so we already have 200 users here and we had entered 250. Let that let this run So what happens is the distributed load testing is done and the master locust will have all the data. Once that is done, you can check and analyze and perform and check the performance of your system and maybe increase it, improve it whenever and wherever needed.

### [24:30] Advanced Locust Features

**Speaker 1:** [24:30] Okay, apart from primary features like defining different types of users or designing multiple scenarios or load testing in distributed mode, locust IO provides many more features. Some of these features are like Right now we have seen load testing for HTTP protocols. If you want to or do the same for other protocols, locust I can be extended for that also if you want to see Change the client that can also be done. So all these are documented on the official documents site. Link has been provided. We can see the link on this particular slide.

[25:16] So if you find this interesting, do visit the documentation and explore more. If you have any question, you can reach out to us on these email IDs. And if you have a

**Pranjal Jain:** [25:39] So I hope you understood about how we can use Locust Io to load test our applications and you'll use it. Whenever you get a chance.

### [25:57] Questions and Answers

**Speaker 1:** [25:57] Okay, so Amit has one question that what do you recommend for testing say a normal laptop or how many users can we test? And how will we know if the delay is our machine or application via CPU or network? Okay, so

**Speaker 3:** [26:28] Amit to answer this, you can use as many users as you want. As this is load testing, we want to check how many users our application can handle. And for each request it will the locust dashboard will show the CPU usage and the response time and request per seconds I hope this answers your question.

**Speaker 1:** [27:02] I think even in when we do load testing with other tools like load runner or new load, so we are able to do it on our laptops itself. Yeah. So it depends on the type of application also. So normally like in the type of application which I am working on right now, so I usually I look for We look for support up to 200 to 50 users per second. So in that case, when we are trying to simulate that kind of behavior, we are able to do it directly on our laptops. Okay, Piuse has one more question. How to identify the upper limit of users that we can test

[27:49] with locust on local machine. I am not exactly sure about this part as of yet. I will check and maybe okay so if we are if we are trying to test any particular scenario So in that case what we usually do is there are there are two ways. Either if we have already faced similar scenario, then in that case we use prior experience to decide number of users For load testing, otherwise, if there is no

[28:36] No prior experience, then in that case analysis is done by the business to understand that how many users are we expecting. So on basis of that we decide the number of users and from there on if say if you want to if you want to Test it for large number of users we can we can use multiple machines or if it is lower number then we can go for single machine. But how many to do on a single machine that part I'm not sure of And this is one of the skills that that comes with performance testing is to

[29:21] actually identify the number actually I find out the number of users that will be That will be that should number of users that should be simulated during load test. Okay There is another question. Can I also do the load test with more complex web application? And how would I do that? Okay, so if we look into the look into other load testing tools that we have in market, so one of the key feature that is that is available with other tools is record and play feature. So basically whatever so if you want to if you want to

[30:06] Simulate a scenario. So that scenario we run manually, and the tool itself records that particular scenario. It captures all the network calls that are going through, and then those can be used for those can be used for further test setup that feature is not available with locust io if we are able to if there are alternatives, we can if we are able to do capture the network calls for a particular scenario, then in that case after recording play, we run into first is scenario design for a scenario design, then for a particular scenario we want to do parameterization

[30:53] correlation these things so for any complex as application as long as if we are able to do these three things I think we can continue with the load testing. So scenario design, parameterization, correlation, all three can be done with locust i also like we should we mentioned in the video for scenario design we can we can distribute we can create multiple task sets and those and actually divide our entire flow into different tasks group into task set and then Those task can be cre can be used as can be used to actually simulate a particular scenario.

[31:42] So once that is done, and once that is done, then after that we would want to if you want to make it closer to real time. So in that case we would like to have like different weights for particular you particular tasks or even for the users So I think different rates for users that was not mentioned, but that also we can do. So that helps in us in modeling the test closer to real time. What apart from that I mentioned parameterization where say if you want to like we had shown that if you want to get csrf token so that can be one parameter for a particular used user

[32:27] so that that can also be configured since we are using python here so we can we can directly code all those things and then we can we can have say if you want to we want to test with 200 users then in that case if you we can have 200 200 user credentials and then keep it in then keep it in a list and use that list to actually assign users to different types of scenario or even If you want, we can we can divide it into percentages and try to simulate complex flows.
