1. Introduction to Locust
1. Introduction to Locust
Locust (locust) is an open source performance testing tool, the main idea is to simulate a group of users accessing your system
2. Features
(1) Define user behavior in code
- No need to install bulky software, just simple Python code
(2) Distributed and scalable
- Locust supports running load tests on multiple machines, so it can be used to simulate requests from millions of users
(3) Proven and battle tested
- Locust is used in many real projects
(4) Locust has a neat HTML+JS user interface that displays relevant test details in real time
- Since the user interface is web-based, it is cross-platform and easily extensible
2. Locust installation
Install command:
pip install locust
Successful installation verification:
locust --version # 查看安装的版本
locust -h # 查看使用帮助
Note: The version of python is preferably above 3.0
3. How to use Locust
1. Core steps
- Define task (interface request): ordinary function, must have a formal parameter, must use @task to modify the function
- Define task set (user behavior): a class must inherit TaskSet, and the defined tasks are inside the class
- Define user class (user): a class that must inherit from HttpUser, configure the host address, override tasks, the format is a list or dictionary, and the value is the name of the defined task set
- Start the service, configure the execution: locust -f file name
example:
from locust import HttpUser, TaskSet, task
# 定义任务集
class TaskTest(TaskSet):
# 定义任务
@task
def say(params):
print("正在说话")
@task
def sing(params):
print("正在唱歌")
# 定义用户类
class Test(HttpUser):
tasks = [TaskTest]
host = "http://localhost"
Execute the locust step:
(1) Open the pycharm terminal and enter locust -f file name to run
(2) Open the browser and enter localhost:8089 to run the locust control interface
(3) Enter data and click start swarming to execute the code
- Number of users (peak concurrency): the number of concurrent users is the number of users
- Spawn rate (users started/second): The number of users loaded per second
- Host (eg http://www.example.com): host address, if there is one in the code, it can be executed by default
2. Commonly used methods for task sets
(1) on_start: pre-method (pre-task), called once before all tasks
(2) on_stop: post method (post task), called once when the task set stops
(3) @task(weight): Modifies the function and defines the function as a task. weight represents the weight, the greater the weight, the more execution times, the default value is 1
from locust import TaskSet, task
# 定义任务集
class TaskTest(TaskSet):
# 定义任务
@task(10)
def say(params):
print("正在说话")
@task(5)
def sing(params):
print("正在唱歌")
def on_start(self):
self.say()
def on_stop(self):
self.sing()
3. Common attributes of user classes
(1) min_wait: The lower bound of the waiting time between user execution tasks, unit: milliseconds, default value: 1000
(2) max_wait: The upper bound of the waiting time between user execution tasks, unit: milliseconds, default value: 1000
(3) host: URL of the application under test, for example: http://localhost
(4) weight: the probability of the user being selected, the greater the weight, the greater the chance of being selected, the default value: 10
from locust import HttpUser
# 定义用户类
class Test(HttpUser):
tasks = [TaskTest]
host = "http://localhost"
min_wait = 1000
max_wait = 2000
weight = 10
4. Locust distributed application - web interface implementation
(1) Host (control machine)
locust -f 目录\执行文件名 --master
(2) Slave host (executive machine)
locust -f 目录\执行文件名 --worker --master-host=主机ip地址 --master-port=主机端口
Key points that slave hosts must rely on:
- Must have python and locust environment
- Must have a copy of the host script
- If the slave host is on the same machine as the master, --master-host and --master-port can be omitted
5. Locust distributed application - implementation of no-graph mode (non-web interface)
locust -f 目录\执行文件名 --master --headless -u 200 -r 20 --expect-workers 2 --run-time 20s --csv ./result.csv
--headless:不使用web界面
-u:虚拟用户数
-r:每秒生成的用户数
--expect-workers:执行机数量
--run-time:运行时间(h:小时,m:分钟,s:秒)
--csv:保存执行结果
4. Application of Locust in interface testing
Simple case implementation:
from locust import HttpUser, TaskSet, task
headers = {"content-type": "application/json;charset=UTF-8"}
data = {"username": "hogwarts", "passwords": "test12345", "code": ""}
params = {"page": 1, "limit": 20, "goodsSn": "11111", "name": ""}
# 定义任务集
class TaskTest(TaskSet):
# 定义任务
# 登录方法
@task(1)
def login(r):
response = r.client.post(url="/admin/auth/login", json=data, headers=headers)
headers["x-litemall-admin-token"] = response.json()["data"]["token"]
print(response.json())
# 获取商品列表方法
@task(1)
def list(r):
response = r.client.get(url="/admin/goods/list", params=params, headers=headers)
print(response.json())
def on_start(self):
self.login()
def on_stop(self):
self.list()
# 定义用户类
class Test(HttpUser):
tasks = [TaskTest]
host = "https://litemall.hogwarts.ceshiren.com"
min_wait = 1000
max_wait = 3000
operation result: