Locust性能测试参数与代码详解的详细说明,涵盖命令行参数、代码配置、分布式测试及结果分析等内容。
在命令行中运行Locust时,我们可以使用一些参数:
- `-f` 或 `--locustfile`:指定locustfile的路径,默认为`locustfile.py`。 - `--host`:指定被测试应用的URL(例如:http://example.com)。 - `-u` 或 `--users`:设置模拟的最大用户数。 - `-r` 或 `--spawn-rate`:设置每秒启动的用户数。 - `-t` 或 `--run-time`:设置测试运行的时间(例如:10s, 1m, 1h)。 - `--headless`:无界面模式,即不启动Web界面,直接运行测试。 - `--web-host`:指定Web界面的主机地址(默认为0.0.0.0)。 - `--web-port`:指定Web界面的端口(默认为8089)。 - `--csv`:指定输出CSV文件的前缀(将生成多个CSV文件,如:_stats.csv, _distribution.csv等)。 - `--html`:生成HTML报告。
with self.client.get("/resource", catch_response=True) as response: if "success" not in response.text: response.failure("未找到关键字") elif response.elapsed > 1000: # 响应时间>1秒标记失败 response.failure("响应超时")
import csvdef read_data(): with open("users.csv") as f: return list(csv.DictReader(f))class TestUser(HttpUser): users = read_data() current_user = 0 @task def update_profile(self): user = self.users[self.current_user % len(self.users)] self.client.put(f"/user/{user['id']}", json={"name": user["name"]}) self.current_user += 1
# 阶梯式等待(前5秒快,之后慢)def custom_wait(user): if user.run_time < 5: return 0.5 # 前5秒等待0.5秒 return 3class CustomUser(HttpUser): wait_time = custom_wait
locust -f locustfile.py --host http://your-api.com
访问 http://localhost:8089 配置测试
locust -f locustfile.py --headless -u 1000 -r 100 -t 5m
继承自 HttpUser 或 User,定义用户行为。
使用 @task 装饰器标记用户任务。
from locust import HttpUser, task, betweenclass MyUser(HttpUser): wait_time = between(1, 3) # 用户任务之间的等待时间(1-3 秒) @task def index_page(self): self.client.get("/") # 访问首页 @task(2) # 权重为 2,执行概率是其他任务的两倍 def about_page(self): self.client.get("/about") # 访问关于页面
between(min, max):随机等待时间(单位:秒)。
constant(seconds):固定等待时间。
constant_pacing(seconds):确保任务间隔至少为指定时间。
# 示例:固定等待 2 秒wait_time = constant(2)# 示例:任务间隔至少 1 秒wait_time = constant_pacing(1)
通过 @task(weight) 设置任务的执行概率。
权重高的任务会被更频繁地执行。
@task(3) # 权重为 3def search_product(self): self.client.post("/search", json={"q": "example"})@task(1) # 权重为 1def view_home(self): self.client.get("/")
使用 self.client 发送 HTTP 请求(GET/POST/PUT/DELETE 等)。
支持添加 headers、params、json 数据等。
@taskdef login(self): self.client.post("/login", json={"username": "user", "password": "123"})
在代码中定义响应时间或错误率的阈值,超过时触发失败告警。
@taskdef test_api(self): response = self.client.get("/api/data") if response.elapsed.total_seconds() > 0.5: # 响应时间超过 500ms,标记为失败 events.request_failure.fire( request_type="GET", name="test_api", response_time=response.elapsed.total_seconds(), exception="Response time exceeded SLA" )
locust -f my_test.py --master
locust -f my_test.py --slave --master-host=192.168.1.100
Master 节点:负责协调所有 Slave 节点,聚合测试数据。
Slave 节点:实际执行测试任务,发送请求到目标系统。
端口说明:
Master 默认监听 5557(接收 Slave 连接)和 5558(接收测试数据)。
可通过 --master-port 自定义端口。
Locust 提供了 Web 界面和 CSV 文件两种方式查看测试结果。
(默认 http://localhost:8089)
实时统计:
Number of users:当前并发用户数。
Hatch rate:每秒生成的用户数。
RPS(Requests per Second):每秒请求数。
Failure rate:请求失败率。
图表展示:
RPS 曲线:每秒请求数随时间的变化。
响应时间分布:中位数(Median)、平均值(Average)、最小/最大响应时间。
用户数变化:用户并发量随时间的增长。
失败请求:
查看失败请求的具体路径、错误类型和异常信息。
使用 --csv=filename 参数生成以下文件:
filename_stats.csv:请求统计(响应时间、成功率等)。
filename_failures.csv:失败请求的详细信息。
filename_exceptions.csv:异常记录。
Median:响应时间的中位数(50% 的请求响应时间小于该值)。
Average:平均响应时间。
Min/Max:最小和最大响应时间。
RPS:每秒处理的请求数(QPS)。
Fail %:请求失败率.
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。