Load Testing using Locust

Aziz Zoaib
2 min readSep 21, 2019

Hello Gurus, recently for one of my projects I need to look into different load testing tools to load test one of our web app in order to understand how much load it can survive in peak hours… I came across very powerful tool called “Locust”. I will explain it below right from installation to execution from cloud.

Introduction

Locust is an easy-to-use, distributed, user load testing tool. It is intended for load-testing web sites (or other systems) and figuring out how many concurrent users a system can handle. For more information go to following link

Installation (Ubuntu)

Pre-Requisite is to install python and pip. Use below commands to install both.

sudo apt-get update
sudo apt-get install python3.6

Now it’s time to install locustio. Use below command to install.

sudo python3 -m pip install locustio

How to use?

Locust works by using locustfile, and that file should be written in python.
Let’s have a look at basic locustfile and how to start load test using that locustfile.

from locust import HttpLocust, TaskSet, taskdef index(l):    
l.client.get("/")
def stats(l):
l.client.get("/stats/requests")
class UserTasks(TaskSet):
# one can specify tasks like this
tasks = [index, stats]
@task
def page404(self):
self.client.get("/does_not_exist")
class WebsiteUser(HttpLocust):
"""
Locust user class that does requests to the locust web server running on localhost
"""
host = "http://127.0.0.1:8089"
min_wait = 2000
max_wait = 5000
task_set = UserTasks

Above locustfile is the most basic example of initiating the load test on localhost:8089 as specified in host attribute.

Let’s have a look how to initiate the load test. There are 2 ways of initiating the load test.

  1. Using the WebUI.
  2. Using command line only.

Web based
Using below command we can start the locust with 1 master and 2 slaves both running on same instance.

locust -f locustfile.py --master & locust -f locustfile.py --slave 
--master-host=127.0.0.1 & locust -f locustfile.py --slave
--master-host=127.0.0.1 &

You can now browse the WebUI of locust using the Public IP of instance and start the load test. You need to provide Virtual Users and Hatch rate which is explained very well here.

Command based

Using below command we can start the locust with 1 master and 1 slave both running on same instance.

locust -f locustfile.py --master & locust -f locustfile.py --slave 
--master-host=127.0.0.1 --no-web -c 100 -r 10 --run-time 1h30m

Note: Locust master and slave should have same locustfile to start the load test.

Conclusion
Above setup can be extended to use multiple instance to generate huge amount of load to test autoscaling and high availability of clusters running on orchestration tools like kubernetes.

Enjoy load testing! :)

--

--