Performance Testing Tools - Locust

Performance Testing Tools - Locust



http://locust.io/
  1. Open source load testing tool.
  2. Python based tool.
  3. Web UI Based

Install Locust on MAC : 

brew install libevent
sudo easy_install locustio
Once you have locust installed help should display a list of commands you can run with locust.

locust --help 

Setting up Locust on Linux machine incase you face any issues the below link could come handy.
https://gist.github.com/heyman/95ee0df663b2d4b9d6fb

Running your first test : 

Here I am taking the example of load testing Http API's.
For http request the HttpLocust class needs to be used. When this class is instantiated this will get an Http session which makes the HTTP call.
The below code makes a post call with the content type json and the "key":"value" as the json body.
The min_wait and max_wait (in ms) are the attributes the define the min and max time that a stimulated user waits before executing each task.

locust_test.py
from locust import HttpLocust, TaskSet, task
class MyTaskSet(TaskSet):
    @task
    def index(self):
        self.client.post("/insert", json={"key":"value"})

class MyLocust(HttpLocust):
    task_set = MyTaskSet
    min_wait = 5000
    max_wait = 15000

For get call : 

self.client.get("/products")

Running Locust :

The below command runs locust the file locust_test.py --host is the host to load test. Here I am running my application locally on port 32323.

 locust -f /filepath/locust_test.py --host=http://localhost:32323/
Locust runs on port : 8089

Locust UI :
http://localhost:8089/

Specify the number of user and the hatch rate and start swarming.


The results can be viewed on the UI : 
The statistics can also be downloaded as a csv file .
The total no of requests failures avg min and max time can all be tracked.



You can define multiple task in the Task set and specify weightage for each of the tasks.  Higher the weight the more the no of times the task would get executed.
from locust import HttpLocust, TaskSet, task

class MyTaskSet(TaskSet):
    @task(1)
    def insert(self):
        self.client.post("/insert", json={"key":"value"})

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

class MyLocust(HttpLocust):
    task_set = MyTaskSet
    min_wait = 500
    max_wait = 1500

Results : 


Using locust you can make multiple API calls specify the weighing and generate production like load for performance testing. 

Comments