最近在学习 locust 性能工具,发现locust性能工具脚本需要python基础才能写脚本,但是对于性能测试人员来说 python 是基本功夫。
在 locust 中get脚本怎么写,为了方便直接在代码运行调试,采用关闭web模式,通过参考官方文档自己实验get/post代码,参考代码如:
@task(1)def get_7dTest(self):# 定义请求头header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"}response = self.client.get("/7d/", headers=header, verify=False)# print("Response status code:", response.status_code)if response.status_code == 200:print("successful")# print("Response text:", response.json())else:print('failure')
post请求写法如下
# 登陆@task(1)def get_login(self):response = self.client.post("/login", {"userName": "7d", "passWord": "123456"})print("Response json:", response.json())
结果:
# 结果:[2021-04-24 21:36:49,495] liwen.local/INFO/locust.main: Run time limit set to 1 seconds[2021-04-24 21:36:49,495] liwen.local/INFO/locust.main: Starting Locust 1.4.4[2021-04-24 21:36:49,496] liwen.local/INFO/locust.runners: Spawning 1 users at the rate 1 users/s (0 users already running)...[2021-04-24 21:36:49,496] liwen.local/INFO/locust.runners: All users spawned: webTestDunShan: 1 (1 total running)[2021-04-24 21:36:49,496] liwen.local/INFO/root: Terminal was not a tty. Keyboard input disabledName # reqs # fails | Avg Min Max Median | req/s failures/s-----------------------------------------------------------------------------------------

本文介绍了如何在locust性能测试工具中使用Python编写get和post请求脚本,包括设置头信息、调试无web界面,并展示了实际的请求响应和登录示例。
最低0.47元/天 解锁文章
6311

被折叠的 条评论
为什么被折叠?



