locust集合点

一、理解
(1)有时测试需要让所有并发用户完成初始化后再进行压力测试,这就需要类似于LoadRunner中的集合点的概念,框架本身没有直接封装

(2)通过locust得基于gevent并发得机制,引入gevent的锁的概念,代入到locust的钩子函数中,实现集合点统一并发概念

(3)semaphore是一个内置的计数器:
每当调用acquire()时,内置计数器-1
每当调用release()时,内置计数器+1
计数器不能小于0,当计数器为0时,acquire()将阻塞线程直到其他线程调用release()

二、实现
1、相关类库

from gevent._semaphore import Semaphore
all_locusts_spawned = Semaphore()
all_locusts_spawned.acquire()

2、代码实例

from gevent._semaphore import Semaphore
all_locusts_spawned = Semaphore(0)
all_locusts_spawned.acquire()

def on_hatch_complete(**kwargs):
    all_locusts_spawned.release()
def on_start(self):
	all_locusts_spawned.wait()  //创建钩子方法
events.hatch_complete += on_hatch_complete //挂载到locust钩子函数(所有的Locust实例产生完成时触发)

class UserBehavior(TaskSet):
	@task(2)
	def index(self):
		self.client.get("/1111")
	@task(1)
	def profile(self):
		self.client.get("/")
 
class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 2000
    max_wait = 5000
发布了222 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42976139/article/details/103592474