Created
April 28, 2024 08:40
-
-
Save zh3389/b39bc93cae5d717592345b3eaa627922 to your computer and use it in GitHub Desktop.
自动化性能测试使用Python的locust库进行API接口的压力测试。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from locust import HttpUser, task, between | |
class WebsiteUser(HttpUser): | |
wait_time = between(5, 15) # 定义用户操作之间的等待时间 | |
@task | |
def load_test_api(self): | |
response = self.client.get("/api/data") | |
assert response.status_code == 200 # 验证返回状态码为200 | |
@task(3) # 指定该任务在总任务中的执行频率是其他任务的3倍 | |
def post_data(self): | |
data = {"key": "value"} | |
response = self.client.post("/api/submit", json=data) | |
assert response.status_code == 201 # 验证数据成功提交后的响应状态码 | |
# 运行Locust命令启动性能测试: | |
# locust -f your_test_script.py --host=http://your-api-url.com |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment