一、脚本
#! --^-- coding:utf-8 --^--
from locust import HttpUser, task, between, TaskSet
from random import choice
# 任务类
class TestLogin(TaskSet):
@task
def to_login(self):
#data = {"username": "admin","password":"admin"}
headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36", }
data = [{"wd": "qtp"},{"wd": "locust"},{"wd": "java"},{"wd": "loadrunner"}]
res = self.client.get("", params=choice(data), headers=headers)
print("------",res.status_code)
print(res.content.decode('utf-8'))
class WebUser(HttpUser):
tasks = [TestLogin]
wait_time = between(2, 5)
host = "https://www.baidu.com/s"
# if __name__ == '__main__':
# os.system("locust -f testsm_demo.py")
二、100个用户并发随机搜索“qtp”,"locust","java","loadrunner"

这段代码展示了如何利用Python的Locust库进行并发负载测试。定义了一个TaskSet类TestLogin,其中包含了登录任务,随机选择'qtp'、'locust'、'java'、'loadrunner'之一作为搜索词进行GET请求。WebUser类指定了并发用户数为100,每个用户等待时间在2到5秒之间,目标URL为百度搜索。
6104

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



