locust 集合设置完后,运行成功,但是监控发现没有等待后发请求,而是直接发请求了,请帮忙看看怎么回事?
import locust
#from gevent._semaphore import Semaphore
import gevent #集合点:多用户同一时间并发操作(监控平台设置的用户数,如100,20,一共100个用户,每秒增加5个,则会等到100个用户的时候才一起发请求。
#三、集合点
all_locusts_spawned =gevent._semaphore.Semaphore() #集合类实例化
all_locusts_spawned.acquire()#加锁
def on_hatch_complete(**kwargs):
all_locusts_spawned.release()#释放锁
# 挂在到locust钩子函数(所有的Locust实例产生完成时触发)
#老版本:locust.events.hatch_complete += on_hatch_complete
locust.events.spawning_complete.add_listener(on_hatch_complete())
#二、主函数调用的任务类,类名可随意
class UserBehavior2(locust.TaskSet):
#1. 构造函数,可放变量
def on_start(self):
self.index=0
print("======onstart begin :")
all_locusts_spawned.wait() # 集合点:创建钩子方法
print("======onstart end:")
@locust.task
def test_visit(self):
url = "/getJoke?page=" + str(self.parent.share_data[2]) + "&count=" + str(self.index + 2)
res=self.client.get(url, name='getJoke分页查询')
res=res.text
#一.继承locust.HttpUser的类为主函数,相当于main,类名可随意
class WebSiteUser232323(locust.HttpUser): #locust.HttpLocust
#1.被测网站域名
host = 'https://api.apiopen.top'
#3.性能测试任务类:登录还是注册
tasks=[UserBehavior2] #调用哪个任务
#4.等待时间设置
wait_time = locust.between(3,7)
#locust -f locustS.py --host=https://www.baidu.com localhost:8089