Start Load Testing with Locust…

Eranda Rajapakshe
Analytics Vidhya
Published in
5 min readDec 1, 2019

Intro

JMeter was my goto tool for load testing, especially during HTTP API development. But I always had difficulties in configuring it maybe because lack of configuration flexibility of maybe it wasn’t that straightforward.

Locust is alternative load testing, which is not matured as JMeter but it answers some of the basic issues JMeter is having. Locust is basically a Python-based framework where the user needs to write code to get the things done as opposed to JMeter configurations. As a developer, I prefer coding over configuration as it gives more flexibility and it won’t hurt to learn a bit of Python (if you are not already familiar with it) ;) Also Locust test can be easily scalable to demonstrate concurrent users. For a detailed comparison between JMeter and Locust, you can refer here [1]

See Locust in Action

Locust is a Python-based framework, so you need to have Python installed in your machine. I will use Python3 for this example and you can find a guide to install python3 here [2]

If you are using MacOSx it may be having Python3 already, get it verified with the following command.

python3 --version

Example REST API

We need to have a sample HTTP API to be tested with Locust. Since we are using Python in this example I will use Python Flask framework to create a simple HTTP API. But you can use whatever the way you are already familiar with to create an application with simple HTTP API(s).

I will create two Python-based virtual environments for each application (for REST API and for Locust) This will help us to keep all the executables needed for each project in an isolated environment. Since we are using Python3 it has venv support embedded inside.

You can find the simple REST API I created for this demo here [3] With the following steps you can boot it up.

Demo HTTP API as four APIs

  • [POST]: /login
  • [POST]: /logout
  • [GET]: /sampleGetApi
  • [POST]: /samplePostApi
  1. Create a virtual environment[4]

Navigate to ([3]) the http-app directory and run following command, (this will create a new directory venv in the current location)

$ python3 -m venv venv

2. Activate virtual environment

$ source venv/bin/activate

in Windows

$ venv\Scripts\activate

3. Install flask into the venv

$ pip3 install flask

4. Run the application

$ python3 hello_api.py

This will start the HTTP app on http://127.0.0.1:5000/

  • FYI: You can exist from the venv anytime using the following command,
$ deactivate

Running Locust

Now we can run the Locust load test on the REST API we created. I will first explain the demo steps and later go through the code.

  1. Create a virtual environment for Locust

Navigate to ([3]) the locust-app directory and run following command, (this will create a new directory venv in the current location)

$ python3 -m venv venv

2. Activate virtual environment

$ source venv/bin/activate

in Windows

$ venv\Scripts\activate

3. Install Locust into the venv

$ pip3 install locustio

4. Run the application (default command)

$ locust

If the file name is not locustfile.py, you can run it like this,

$ locust -f locust_files/my_locust_file.py

This will startup the locust server as follows,

5. Open the Locust control panel in the browser (http://localhost:8089)

You can provide the following inputs. Host is the HTTP endpoint we are trying the load test on. For this, I will use the endpoint I created in the previous step.

6. Start swarming

After clicking the button, load test will start and you can stop it after a while. In the following result page you can see, login and logout APIs were called 10 times (once per each user) and sampleGetApi is called more than samplePostApi.

Locust Code

If you open-up the locustfile.py, it will look like this. This is only a slight updated version from Locust quick start example [6]

I will go through a few of the important points here. You can refer to [6], [7] for more information.

  1. UserBehavior class and WebsiteUser class

UserBehavior is a subclass of TaskSet which contains actions done by the users and WebsiteUser is a subclass of Locust which configures the Locust test.

2. on_start and on_stop

As mentioned in comments, on_start and on_stop methods will be called by each thread upon starting and finishing respectively.

3. @task annotation

This will specify each action that each user should be done (iteratively) and the number passed into the annotation gives a weight for each task. It will make sure one task executes more over others.

4. @seq_task(n) annotation

By defaults, tasks are executed in random order. Using this annotation you can give an order to the task execution.

5. task_set and wait_time

task_set defines which set of tasks (a subclass of TaskSet) to be used in the execution and the wait_time defines the time to wait between two task executions.

That's all I’m planning to cover with this post and you can refer to the Locust documentation[6] for all the other cool features. Happy load testing.

>> Dedicated to the best QA girl I’ve met Dilshani :) <<

References

[1]. https://www.blazemeter.com/blog/jmeter-vs-locust-which-one-should-you-choose/

[2]. https://realpython.com/installing-python/

[3]. https://github.com/erandacr/locust-tryout/tree/master/http-app

[4]. https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

[5]. https://github.com/erandacr/locust-tryout/tree/master/locust-app

[6]. https://docs.locust.io/en/stable/quickstart.html

[7]. https://medium.com/@linh22jan/load-test-with-locust-37c4f85ee2fb

--

--