How to load test with Locust
Why using Locust?
At some point, JMeter is your on the go tools for your load testing. Of course, JMeter is easy to use, well documented, and on top of all, it's free. I always using JMeter as my tools for load testing since its support distributed testing which allows me to load with more than 100 thousand users.
The problem is started when I am not acting alone in the project. As a team, it's a common practice to review your peer’s code and JMeter falls out heavily on this. Since JMeter relies on its GUI with XML file, it's hard to comment on your peer’s code and pinpoint improvement that you could propose. And that’s when Locust is come on top.
Locust is written in python and code in python to deliver your load test script. It's even on their first page.
Locust is an easy-to-use, distributed, user load testing tool. It is intended for load-testing web sites (or other systems) and figuring out how many concurrent users a system can handle.
Install Locust
If you already install pip, just write this on your terminal
$ pip3 install locust
Once Locust is installed, a locust command should be available in your shell. To see available options, run:
$ locust --help
Locust is supported on Python 3.6, Python 3.7, and Python 3.8. You read it right, RIP Python 2.7.
Create your Locust file
Open your favorite code editor and write your code. Basically its a python code. There is ton example in the documentation and here is my example code :
class LoadTest is my subclass of task set, which contains a user behavior that you desired. In my example, the user is only call /path1 and after that visit /path2. Task set can use any API method such as GET, POST, PATCH, and etc.
On the other hand, MyLocust is a subclass of Locust. This subclass represents how users will swarm the API. At the example code, I added wait_time between 3 seconds and 7 seconds which defines users will swarm at a random time between that period.
Locust also supports a weighted task. For example, at e-commerce users tends to windows shopping rather than shopping itself. Let's say every 5 users open task1 will go to task2.
Running your Locust file
At current directory, run this command :
$ locust -f yourlocustfile.py
at default, Locust will run in web browser mode and you need to open your browser and go to localhost:8089 to add your users and hatch rate
After you click start swarming, you could see the result of your ongoing test.
If you want to run it with only CLI command or maybe just not need it, you can always run a no-web mode with this command
$ locust -f locustfile.py --headless -u 1000 -r 100 -t 1h30m
-u
specifies the number of Users to spawn, and -r
specifies the hatch rate (number of users to spawn per second).-t
is a timer for your script. Locust will shut down once the time is up.
Conclusions
Locust is a very nice tool and widely used by any web engineer in the world. Big IT companies like Riot, Google, and AWS used this because of reliability, flexibility, and capability running distributed.
BIBLIOGRAPHY
- Locust Documentations, https://docs.locust.io/en/stable/quickstart.html
- Locust Homepage, https://locust.io/