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.

Installation

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 locustfile.py):

from locust import HttpLocust, TaskSet, task

class UserBehavior(TaskSet):

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

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

    @task
     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 locustfile.py is located, we can run locust by the command:

locust --host=http://example.com  

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 http://example.com -f ….../locust/locustfile.py --master&
$ locust -f …../locust/locustfile.py --slave&
$ locust -f …../locust/locustfile.py --slave&
$ locust -f …../locust/locustfile.py --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
    os.chdir(".../Downloads/")
    import csv
    with open('auth.csv') as f:
        reader = csv.reader(f)
        content=[]
        for x in reader:
            content.append(x)
        content = [l[0] for l in content]
        print content 
        i=0
        @task
        def index(self):
            for i in content:
self.client.request(method="GET",url="/",  
headers={'Auth-token':i})  
   def A(self):
       follow(self)

   @task
   def feedback(self):
       for i in content:
self.client.request(method="POST",url="/feedback",  
data = {"feedback":"test"},headers={"Auth-Token":i})  
   def A(self):
       feedback(self)

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

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