Using Locust.io for Performance Testing

By: Slav Kurochkin

In this post, we going to explore Locust.io for Performance and Stress testing using Python.

I have been using JMeter for performance testing and it is a great tool, but sometimes you feel limited and want to have more freedom and control over the tool. So I started to explore what is available out there on the web and found out few guys from MIT felt the same, so they decided to build another open source Performance Tool in Python,

First, if you still don't have Python on your machine, you would have to install Python 2.7.*. Once the installation is completed select the IDE where you going to write your code, I prefer PyCharm, but you can pick whatever you more comfortable with.

We will need the tool itself in order to run the tests, so go ahead and install Locust.io, follow the instruction on official website, or simply open command line and run:

pip install locustio

Now we have everything we need to run the first simple test, open PyCharm and create your Python file, lets name it locustfile.py, add a website user:

class WebsiteUser(HttpLocust):
    host = "http://localhost:8080/"
    task_set = Tasks
    min_wait = 5000
    max_wait = 9000

And then create your test, make sure you add a tag @task, before your test, so locust knows what to run:

@task
def index(self):
self.client.get("/")

And that is it, well not really, modern websites using APIs and will require you send some information in the header, for example, token and here is how you do it:

    def auth_header(self):
        self.headers = {'Authorization': "Token " + self.token()}
        self.client.headers = self.headers

In my case as you can see I'm calling the self.token() function, wich is basically extracting the token for me on background, now once I have everything set, here is how the complete test will look like:

import json
from locust import HttpLocust, TaskSet
from locust import task

class Tasks(TaskSet):


  def auth_header(self):
        self.headers = {'Authorization': "Token " + self.token()}
        self.client.headers = self.headers

@task
def index(self):
self.auth_header
self.client.get("/")

class WebsiteUser(HttpLocust):
    host = "http://localhost:8080/"
    task_set = Tasks
    min_wait = 5000
    max_wait = 9000

Alright it is all great but how do I run it, well let's run the command line, make sure you are in the right directory(where your locustfile.py located) :

locust -f locustfile.py

Now you can open Locust locally(in my case it is on localhost:8089) and execute performance tests:

Performance Testing With Locust
Performance Testing With Locust

Once the tests cicked off you will see it running:

Performance Testing With Locust
Performance Testing With Locust
View Comments

Leave a Comment

Use markdown or basic HTML and be nice.