Locust Quick Start

Introduction

In order to start working with Locust, it was necessary to perform the following steps:

Install Locust

Make a trial run and make sure that there are no errors

Explore locust --help to know the basic launch options or at least remember where to watch them

Prepare (for example, using Flask ) test server with A REST API to which you can send a lot of requests and do no harm to anyone

If you don't know how to make your server - read the articles Introduction во Flask , The first project on Flask and Flask server on Linux + Nginx

Running the test

At the address 192.168.0.106, start the server, for example Flask server which listens to the url count_views

To give a load to this endpoint write:

from locust import HttpUser, task, between class MyWebsiteUser(HttpUser): @task def load_user_profile(self): self.client.get("/count_views") wait_time = between(1, 3)

Then execute

locust -f my_test.py --host=http://192.168.0.106 --headless -u 2000 -r 50

-f my_test.py означает, что запускать нужно файл my_test.py

If this is not prescribed, then by default it will start locustfile.py

--host specifies the address to connect to. The code was self.client.get("/count_views") so the request will be at http://192.168.0.106/count_views/

--headless means you don't want to run the web UI. That is, you will work without a browser.

-u sets the number of users (in old versions of locust there was -c now you need to write -u)

-r sets the number of users created per second. If you want 2000 users and put -r 50, then two thousand will be in 40 seconds.

Result

After the launch, you will see the current results in the console. While the load is small, there will be no failures and there will be zeros in the failures column.

Name # reqs # fails | Avg Min Max Median | req/s failures/s -------------------------------------------------------------------------------------------------------------------------------------------- GET /count_views 191 0(0.00%) | 45 14 342 46 | 0.30 0.00 -------------------------------------------------------------------------------------------------------------------------------------------- Aggregated 191 0(0.00%) | 45 14 342 46 | 0.30 0.00

As the load increases, the first failures will appear

Name # reqs # fails | Avg Min Max Median | req/s failures/s -------------------------------------------------------------------------------------------------------------------------------------------- GET /count_views 8751 19(0.22%) | 1046 0 9625 910 | 137.50 0.10 -------------------------------------------------------------------------------------------------------------------------------------------- Aggregated 8751 19(0.22%) | 1046 0 9625 910 | 137.50 0.10

If you made a server on my instruction «Flask Visit Counter» then you can go to the server port 80 using a browser and see how many requests have already been made

Locust framework image from website www.aredel.com

Task selection

If there is a need to perform only part of the tasks, you can do this using the tag module

You need to connect tag and add before each @task @tag('tag_name') there can be several tags @tag('tag1', 'tag1', 'newTag1')

Example: file using_tags.py

from locust import User, task, tag, between class MyWebsiteUser(User): @tag('tag0') @task def task_0(self): print("Task 0 was performed") @tag('tag1') @task def task_1(self): print("Task 1 was performed") @tag('tag2') @task def task_2(self): print("Task 2 was performed") @tag('tag1', 'tag2', 'tag3') @task def task_3(self): print("Task 3 was performed") wait_time = between(2, 3)

To complete all tasks, just run the file as in the previous example.

If you need, for example, to execute only task_1 and task_3 , note that they have a common tag tag1

Therefore, it is enough to add options --tags tag1

locust -f using_tags.py --host=http://192.168.0.106 --headless --tags tag1 -u1

You can check the result by examining the terminal

[2020-11-04 13:34:09,675] DESKTOP-OP43ER5/INFO/locust.main: Starting Locust 1.3.1 [2020-11-04 13:34:09,675] DESKTOP-OP43ER5/INFO/locust.runners: Spawning 1 users at the rate 1 users/s (0 users already running)... [2020-11-04 13:34:09,675] DESKTOP-OP43ER5/INFO/locust.runners: All users spawned: MyWebsiteUser: 1 (0 already running) Name # reqs # fails | Avg Min Max Median | req/s failures/s -------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------- Aggregated 0 0(0.00%) | 0 0 0 0 | 0.00 0.00 Task 1 was performed Name # reqs # fails | Avg Min Max Median | req/s failures/s -------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------- Aggregated 0 0(0.00%) | 0 0 0 0 | 0.00 0.00 Task 1 was performed Name # reqs # fails | Avg Min Max Median | req/s failures/s -------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------- Aggregated 0 0(0.00%) | 0 0 0 0 | 0.00 0.00 Task 3 was performed Name # reqs # fails | Avg Min Max Median | req/s failures/s -------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------- Aggregated 0 0(0.00%) | 0 0 0 0 | 0.00 0.00

As you can see for yourself task_0 and task_2 did not run

Priority

Suppose you have two tasks, but one needs to be completed five times more often than the other.

You can solve this problem by setting the weight of each task in parentheses after the announcement: task(10)

Copy the file code weight.py

from locust import User, task, tag, between class MyWebsiteUser(User): @task(4) def task_0(self): print("Task 0 was performed") @task(1) def task_1(self): print("Task 1 was performed") wait_time = between(2, 3)

locust -f weigth.py --host=http://192.168.0.106 --headless -u 1

task_0 will be performed four times more often than task_1

Share in social media: