from locust import HttpLocust,TaskSet,task
class Discuz_Login(TaskSet):
#task(1)task(10)代表权重,eg:如下表示login方法执行10次时,index方法才执行1次
@task(1)
def index(self):
url=“https://www.baidu.com/”
headers={“User-Agent”:“Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36”}
self.client.get(url,headers=headers,name=“打开首页”)
@task(10)
def login(self):
url=""
headers=""
data=""
with self.client.post(url,headers=headers,data=data,name=‘登录’)as response:
print(response.text)
return response
class Discus_User(HttpLocust):
task_set =Discuz_Login
host = “https://www.baidu.com/”
#思考时间在1秒到3秒内随机使用一个时间,不设置的话默认停顿1秒
min_wait = 1000
max_wait = 3000
执行:
打开cmd,到脚本所在目录:locust -f xx.py
浏览器打开网址:http://localhost:8089/
参数化&关联:
分布式执行locust脚本
电脑cpu是几个核,就可以在本机开几个cmd进程;这样可以最大限度的利用cpu,产生更大的压力
当然slave也可以是不同的电脑
当主机和slave在不同的机器上: