
技术分享1 分钟阅读
locust性能测试,脚本编写和常用方法
基础用法
import json
from locust import HttpUser, task, between
# function 包装task
class MyHttpUser(HttpUser):
wait_time = between(1, 2) # 执行任务 等待时长 检查点 思考时间
@task
def index_page(self):
url = "https://www.zaojingyoutu.top:8000/api/login/"
payload = json.dumps({
"username": "zjy",
"password": "121212126"
})
headers = {
'Content-Type': 'application/json'
}
self.client.post(url,data=payload,headers=headers)

文件名称为locustfile.py
启动:locust 命令就行了


无GUI运行
命令:locust -f locustfile.py –host=111111 –headless -u 10 -r 2 -t 15s
-h, --help 查看帮助
-H HOST, --host=HOST 指定被测试的主机,采用以格式:http://10.21.32.33
--web-host=WEB_HOST 指定运行 Locust Web 页面的主机,默认为空 ''。
-P PORT, --port=PORT, --web-port=PORT 指定 --web-host 的端口,默认是8089
-f LOCUSTFILE, --locustfile=LOCUSTFILE 指定运行 Locust 性能测试文件,默认为: locustfile.py
--csv=CSVFILEBASE, --csv-base-name=CSVFILEBASE 以CSV格式存储当前请求测试数据。
--html LOCUST_HTML .html 存储HTML报告文件
--master Locust 分布式模式使用,当前节点为 master 节点。
--slave Locust 分布式模式使用,当前节点为 slave 节点。
--master-host=MASTER_HOST 分布式模式运行,设置 master 节点的主机或 IP 地址,只在与 --slave 节点一起运行时使用,默认为:127.0.0.1.
--master-port=MASTER_PORT 分布式模式运行, 设置 master 节点的端口号,只在与 --slave 节点一起运行时使用,默认为:5557。注意,slave 节点也将连接到这个端口+1 上的 master 节点。
--master-bind-host=MASTER_BIND_HOST Interfaces (hostname, ip) that locust master should bind to. Only used when running with --master. Defaults to * (all available interfaces).
--master-bind-port=MASTER_BIND_PORT Port that locust master should bind to. Only used when running with --master. Defaults to 5557. Note that Locust will also use this port + 1, so by default the master node will bind to 5557 and 5558.
--expect-slaves=EXPECT_SLAVES How many slaves master should expectu to connect before starting the test (only when --no-web used).
--headless no-web 模式运行测试,需要 -u 和 -r 配合使用.
-u NUM_CLIENTS, --clients=NUM_CLIENTS 指定并发用户数,作用于 --no-web 模式。
-r HATCH_RATE, --hatch-rate=HATCH_RATE 指定每秒启动的用户数,作用于 --no-web 模式。
-t RUN_TIME, --run-time=RUN_TIME 设置运行时间, 例如: (300s, 20m, 3h, 1h30m). 作用于 --no-web 模式。
-L LOGLEVEL, --loglevel=LOGLEVEL 选择log级(DEBUG/INFO/WARNING/ERROR/CRITICAL). 默认是 INFO.
--logfile=LOGFILE 日志文件路径。如果没有设置,日志将去 stdout/stderr
--print-stats 在控制台中打印数据
--only-summary 只打印摘要统计
--no-reset-stats Do not reset statistics once hatching has been completed。
-l, --list 显示测试类, 配置 -f 参数使用
--show-task-ratio 打印 locust 测试类的任务执行比例,配合 -f 参数使用.
--show-task-ratio-json 以 json 格式打印 locust 测试类的任务执行比例,配合 -f 参数使用.
-V, --version 查看当前 Locust 工具的版本.
--web-auth :账号:密码,仅在web情况使用分布式运行
同一台电脑:
主机: locust -f test3.py –master
从: locust -f test3.py –worker
不同机器
启动主进程:locust -f test.py –master
启动助攻进程:locust -f test.py –worker –master-host=主控机器ip –master-port=5557(默认的端口,如果主控没有修改的话,可以不用写)
前置、后置处理
on_start :每个虚拟用户在启动时都会调用该方法
on_stop 当虚拟用户用户停止运行(被终止)时调用
class UserBehavior(SequentialTaskSet):
# 限制在所有用户准备完成前处于等待状态
def on_start(self):
#前置处理代码
@task
def test_visit(self):
#处理代码
def on_stop(self):
#后置处理代码
class WebsiteUser(FastHttpUser):
host = "https://www.baidu.com"
tasks = [UserBehavior]
min_wait = 100
max_wait = 300
if __name__ == '__main__':
import os
os.system("locust -f test.py")
设置集合点
from locust import SequentialTaskSet, task, FastHttpUser,events
from gevent._semaphore import Semaphore
all_locusts_spawned = Semaphore()
all_locusts_spawned.acquire()
def on_hatch_complete(**kwargs):
all_locusts_spawned.release() # 创建钩子方法
# 挂在到locust钩子函数(所有的Locust示例产生完成时触发)
events.spawning_complete.add_listener(on_hatch_complete)
class UserBehavior(SequentialTaskSet):
@task
def test_visit(self):
all_locusts_spawned.wait() # 限制在所有用户准备完成前处于等待状态
url = 'https://www.zaojingyoutu.top:8000/api/getMobile/?name=美国'
self.client.get(url)
class WebsiteUser(FastHttpUser):
host = "https://www.baidu.com"
tasks = [UserBehavior]
min_wait = 100
max_wait = 300
if __name__ == '__main__':
import os
os.system("locust -f testgather.py")执行权重
Locust会根据你在用户类中定义的任务及其权重随机地模拟用户行为
class UserBehavior(SequentialTaskSet):
@task
def test1(self):
#处理代码
@task(2)
def test2(self):
#处理代码
class WebsiteUser(FastHttpUser):
host = "https://www.baidu.com"
tasks = [UserBehavior]
min_wait = 100
max_wait = 300
if __name__ == '__main__':
import os
os.system("locust -f test.py")设置@task(2)参数,test2任务被选择的概率为其他任务的两倍
读者评论
评论会同步写入该文在 Notion 中的页面底部(与正文同页,便于管理)。
暂无评论,欢迎抢沙发。