Generating custom loads with Locust

Thilani Mahaarachchi
5 min readSep 28, 2021


The significance in ramping-up and ramping-down the user load

Performance tests support in creating reliable applications and make a better end-user experience. Hence, before we release an application to production it is recommended to run performance tests. Load testing scripts that can simulate actual user behaviors should be developed to understand performance bottlenecks. And then developers can come up with appropriate fixes. User patterns and application usage should be analyzed before writing performance scripts. These analyses indicate where the usual and peak traffic times exist.

A load test carried out with a thorough understanding of these patterns gives more accurate results. Every application has its peak hours in real user environments, where concurrent users tend to increase gradually before the start of peak time, which is known as ramp-up. And at the end of peak time, a drop in concurrent users is observed which is ramp-down. And in spike tests, we drastically ramp up and ramp down traffic load to evaluate the behavior of the software application under sudden increment or decrement in user load and determine recovery time after a spike in user load.

These ramping ups and ramping downs help to test how the server auto-scaling happens to manage the load. Reflect more real-like server metrics like CPU utilization and memory utilization further. Response time will also be close to the production environment so the testers can come up with more realistic performance statistics.


We can generate similar load test scenarios and check at what stage our infrastructure starts misbehaving using Locust. Locust is an open-source performance testing framework written in Python which allows writing performance scripts in pure Python. The tool is implemented to resolve some specific issues with existing performance tools. You can write some effective performance scripts using this framework with some Python experience.

LoadTestShape class in Locust

Locust supports controlling user count and simulates load test strategies by using its LoadTestShape class. When we need to test how our applications behave during high traffic times, that cannot be achieved by simply setting or changing the user count and spawn rate. In such scenarios, a completely custom-shaped load test is required.

You can simply do it by creating a class that inherits LoadTestShape. In the same class, you need to define a tick() method that returns a tuple with the ‘user count’ and ‘spawn rate’(number of users to start per second). If you want to stop the test, you can return None. Locust calls this tick() method roughly once per second.

Let’s look at the below Locust script.

Figure (1).

This script executes a GET request to launch

To create a more real user behavior in code, a wait_time has been declared which makes a simulated user wait for 0.5 seconds after each task. Then in the class which inherits the LoadTestShape class, time base stages are declared.

In this script,

  • The load will start with 0 users and a spawn rate of 10 until the 15 seconds mark
  • Then continue with a spawn rate of 10 users and ramp-up to 50 users until the 45 seconds mark
  • Then again continue with a spawn rate of 10 users and ramp-up to 100 users until the 75 seconds mark
  • Then continue with a spawn rate of 10 users and ramp-down to 30 users until the 90 seconds mark
  • Then continue with a spawn rate of 10 users and ramp-down to 10 users until the 105 seconds mark
  • Then continue with a spawn rate of 1 user and ramp-down to 1 user until the 120 seconds mark
  • And finally, end the stages and ramp-down to 0 user

Once you run the script by running the command locust -f <file_name>.py, you can access the web user interface at http://localhost:8089 as shown below.

Figure (2). Running the Locust file

You don’t need to provide the total user and hatch rate for a locust script with a custom load shape as we define those values in stage steps. Hence, the fields for the user count and hatch rate are disabled in the web interface.

Figure(3). Starting a new load test

And then click on the “Start swarming” button. In the console log following output can be observed.

Figure(4).Console log which explains the load

The test will stop once it completes all the stages and the following statistics can be observed in the console.

Figure(5).Console log statistics

In the statistics tab of the web user interface, Locust presents data about the executed script.

Figure(6).User interface statistics

And the Number of Users graph in the charts tab indicates how the users are ramped up and ramped down during the defined interval.

Figure(7).Number of Users graph


A load testing script that can simulate real-world scenarios is required to verify whether a web application behaves well during high traffic times. Comping up with a strategy and simulating the real usage pattern of an application can help us find the actual issues at the correct time using tools like Locust.