Introduction to Locust.io – Today we are going to introduce to you Locust.io. Locust is an open source performance testing tool. You develop your performance test scripts with Python. It allows you to distribute your performance test across different machine so you can create more load on your application under test.
Before deep dividing into Locust, in this article, we are going to show you how to install Locust, develop a simple test script and run it.
How to Install Locust.io
Prerequisite is to have Python 2.7 or 3.0 in your system. Then the only thing you should do is run below commands:
python -m pip install locustio
python3 -m pip install locustio
If everything is fine, you’ll be able to run locust command on terminal.
First Performance Test Script
You need to be familiar with Python to create test scripts. A locust class represents a single user. Locust will spawn one thread for each user that is being simulated. There are some attributes that you should be familiar with.
- TaskSet: You should create a class with TaskSet is where you define the behavior of the user behavior. Then you define the action by @task annotation.
class E-Commerce(TaskSet): @task def browser_site(self): print("open the site") print("request categories") @task def do_shopping(self): print("open the site") print("buy something")
- HttpLocust: In order to do HTTP calls, we need a class with HttpLocust parameter. Here, we define our test specific attributes like think times, etc. In that example, I define the min and max think the time that the system will wait before making any request. My requests will wait between 2 and 3 seconds. You also bind this configuration to your TaskSet. You may have different TaskSet and different Http configurations.
class WebsiteUser(HttpLocust): task_set = E-Commerce min_wait = 2000 max_wait = 3000
Here’s a sample script:
from locust import HttpLocust, TaskSet, task class Test_1(TaskSet): @task(2) def users(self): self.client.get("users") @task(1) def photos(self): self.client.get("photos") class UserBahavior_1(HttpLocust): task_set = Test_1 min_wait = 5000 max_wait = 9000 class Test_2(TaskSet): @task(2) def comments(self): self.client.get("comments") @task(1) def todos(self): self.client.get("todos") class UserBahavior_2(HttpLocust): task_set = Test_2 min_wait = 2000 max_wait = 3000
As you see, we are going to make 4 different GET requests to different endpoints during our load test. But the domain was not defined in the script. We can pass the HTTP domain while running the test from the command line with –host parameter or we can just add host variable in our class as shown below:
class UserBahavior_2(HttpLocust): task_set = Test_2 host = "https://jsonplaceholder.typicode.com" min_wait = 2000 max_wait = 3000
Execute the Script
To run your test, you should go to command line and navigate to the folder where you keep your script. Then, execute the below command by changing the file name.
locust -f your_performance_test_script_name.py --host https://jsonplaceholder.typicode.com/
In case you specified hostname in your class, don’t add –host parameter.
After executing the command, you’ll see below message on the command line.
[2019-03-12 11:50:22,927] Canberk.local/INFO/locust.main: Starting web monitor at *:8089 [2019-03-12 11:50:22,927] Canberk.local/INFO/locust.main: Starting Locust 0.9.0
Open a browser and navigate to http://localhost:8089
You will see a page which can be seen below. You need to set the maximum number of virtual users and Hatch Rate which means the ramp-up rate. Below configuration means that we’ll reach 100 users in 20 seconds.
Then, the test will start and you’ll see the results immediately.
In case you don’t want to deal with web UI, you can just run the below command to set virtual user configuration.
locust -f test_file.py --host https://jsonplaceholder.typicode.com/ --no-web -c 100 -r 5
Then the results will appear on the console.
To save execution results, just add –csv=result.csv to your terminal command. We did not extract any data or do the POST operation.
We’ll continue the Locust tutorials in next days with those examples.
May the performance be with you!
Canberk Akduygu is a Test Lead working on Test Automation and Performance Testing in Amsterdam at Qardio.