Running loadtests - An introduction to locust

JMeter is the leader among loadtesting tools but we were trying to check other alternatives and stumbled upon locust. For those of you who don't like the GUIs and would rather prefer defining user behavior as pure code, this is a very good tool. We tried it for loadtesting a simple android app and below is a simple introduction.


Assuming you already have Python and Pip, locust is very easy to setup.

pip install locustio  

After the installation, locust command should be available in the terminal. For the available options, run

locust --help  

Here is a sample locust script (named

from locust import HttpLocust, TaskSet, task

class UserBehavior(TaskSet):

    def login(self):
          self.client.request(method="POST", url="/login",
          headers={'username': ‘user’, 'password': ‘password’})

     def home(self):
           self.client.request(method="GET", url="/home")

     def profile(self):
           self.client.request(method="GET", url="/home")

class WebsiteUser(HttpLocust):  
     task_set = UserBehavior
     min_wait = 5000
     max_wait = 9000

Normal python callable locust tasks are defined in a TaskSet class.

Starting Locust

From the same directory where the is located, we can run locust by the command:

locust --host=  

To start the loadtesting, you need to hit the URL with hostname/IP with port 8089 i.e. http://hostname-OR-IP:8089 and specify the number of users to simulate and their hatch rate.

It results into a locust’s simple user interface which shows the live analytics.

Running distributed loadtesting from any directory, the command used is as follows:

$ locust -H -f ….../locust/ --master&
$ locust -f …../locust/ --slave&
$ locust -f …../locust/ --slave&
$ locust -f …../locust/ --slave&

Below is a simple python scripts that reads credentials from a CSV file and runs a loadtest.

from locust import HttpLocust, TaskSet, task

class UserBehavior(TaskSet):  
    global content
    global i
    import os
    import csv
    with open('auth.csv') as f:
        reader = csv.reader(f)
        for x in reader:
        content = [l[0] for l in content]
        print content 
        def index(self):
            for i in content:
   def A(self):

   def feedback(self):
       for i in content:
data = {"feedback":"test"},headers={"Auth-Token":i})  
   def A(self):

class WebsiteUser(HttpLocust):  
    task_set = UserBehavior

Hope this helps someone who just discovered locust.

Happy loadtesting! :)

Priyanka Sharma

Priyanka is Senior Cloud and DevOps Engineer. She can churn out CloudFormation templates at a moment's notice and play with Chef/Ansible. Dancing, music, badminton and word games are her hobbies

comments powered by Disqus