Locust:An Easy Distributed Load Testing Framework

Akshaye Kumar Srivastava
4 min readDec 22, 2020

--

This is a tool-torial(Hit a bunch of commands ,see it up running , save time and invest it in something more interesting). You can consider this as a wrapper over the existing documentation

Locust is open source , scalable, distributed user load testing framework for web services . Locust test scenarios as well as Locust itself are written in Python. Writing test cases in python allows the tests to be diverged, complex and specific for a certain web service.

Installation

Very Easy Installation, just need to follow the installation guide 😜

We will be Using the google compute engine VMs to run the Locust in Distributed Manner.For every VM which we are going to use we need to install locust in it and have test files in it

Getting Started with Locust

Here is sample Locust test scenario which hits the some endpoint of some service , we can follow the guide and write more complex tests scenarios accordingly

import time
from locust import HttpUser, task
class QuickstartUser(HttpUser):
@task
def first_task(self):
self.client.get("/health")

We will be running locust across multiple VMs, so we need to have the test file in every VM which we are going to use .

Locust runs distributed on master slave pattern

Run the following command to start a Master Node on VM-1

locust -f <path of the test script> --master --web-port 8001

This command starts a Master Node and the Locust UI will be present at <External IP of VM-1>:8001or in case we run it on our local machine can check the UI at localhost:8001

we can see UI like this

At present there are O workers

Now lets use VM-2 as a slave machine and spawn Locust workers on it

Locust uses file descriptors(FD) to simulate large concurrent users

By default the soft limit for the file descriptor is set to 1024

Use the following command in the terminal of VM-2 and increase the number of file descriptors

To Find Hard Limit for FD , run the below command

ulimit -Hn

To set Soft Limit to the above value , run the below command

ulimit -S -n <value from above command>

Now we can spawn multiple workers on VM-2, use the below snippet and can save it as run_all_slaves.sh and run the script

for((i=0;i<CORES;i++))
do
locust -f <filepath of test script> --worker --master-host=<VM-1 Internal IP> &
done

Here CORES is the number of cpu a VM-2 has . This runs Locust Workers equal to the number of cores of the VM-2. So slave VM-2 can have workers from 1 to number of cores it has.

We can run master and slave on same VM also .

To run the master and slave on the local machine all the steps remain same upto this , we just need to ignore — master-host =<> part from the above script

Once this is run , we can check the UI which shows currently running workers

Now the number of workers is 3 ,as i ran the above command for 3 cores

We can add more workers to the slave VM or even a new VM as slave even during the test scenarios have started running , we just need to run the above script in the new VM or the existing VM

Use the UI to input the user count and spawn rate and Host(Service which we want to test)

Explore the UI to get charts, reports or to increase/decrease the users and see the cpu usage of the worker nodes

Hitting a non existing endpoint
Results from hitting a non existing endpoint of a service

There are multiple load testing frameworks , what sets locust apart is that it is

  1. Easy to set up and use
  2. Test scenarios are simple python code
  3. Can generate huge amount of concurrent users(yes you require to run workers on multiple VMs but still… , also can run it on Kubernetes and avoid setting up multiple VMs) and aggregate the result on a user friendly UI

This is all about starting with Locust , do check out out the documentation https://docs.locust.io/en/stable/what-is-locust.html to write some nice and interesting test scenarios with various configurations like task weightage , user class , events etc

--

--

Akshaye Kumar Srivastava
Akshaye Kumar Srivastava

Written by Akshaye Kumar Srivastava

Trying out things in Software Engineering Since 2018

Responses (1)