Locust:新旧版本对比,以及新旧版本对集合点的实现

由最近的研究来看,locust大致分为新旧两个版本:

  • 新版本以1开头,现在最新稳定版为1.4.3;
  • 旧版本以0开头,比如我们公司现在常用的开发版本为0.12.2.

那么他们有什么区别呢?目前有如下发现:

  • 安装方式的变化:
    • 旧版本安装命令为:pip install locustio
    • 新版本安装命令为:pip install locust
  • 参数变化
    • 吴图形模式启动参数: 旧版本为--no-web 新版本为--headless
      -代码中类的变化

集合点

下面就新旧两个版本,对集合点进行实现:

当前新版本:

  • Locust 1.4.3
  • gevent 21.1.2
  • greenlet 1.0.0
from locust import HttpLocust, TaskSet, task, events,HttpUser
from gevent._semaphore import Semaphore

all_locusts_spawned = Semaphore()
all_locusts_spawned.acquire()

def on_hatch_complete(**kwargs):
    # 创建钩子方法
    all_locusts_spawned.release()
# 挂在到locust钩子函数(所有的Locust示例产生完成时触发)
# events.hatch_complete += on_hatch_complete
events.spawning_complete.add_listener(on_hatch_complete)
class Knight_Login(TaskSet):
    def on_start(self):
        all_locusts_spawned.wait()
    @task(1)
    def index(self):
        all_locusts_spawned.wait()
        url = 'https://www.baidu.com'
        self.client.get(url)
    @task(1)
    def login(self):
        all_locusts_spawned.wait()
        self.client.get("http://www.baidu.com")
# class Knight_User(HttpLocust):   ###这是1.0之前写法
class Knight_User(HttpUser):
    # task_set = Knight_Login   ###这是1.0之前写法
    tasks = [Knight_Login]
    host = "http://www.whyfjz.com"
    min_wait = 1000
    max_wait = 3000

旧版本实现:

  • locust 0.12.2
  • gevent 1.4.0
  • greenlet0.4.15
from locust import HttpLocust, TaskSet, task, events
from gevent._semaphore import Semaphore

all_locusts_spawned = Semaphore()
all_locusts_spawned.acquire()

def on_hatch_complete(**kwargs):
    # 创建钩子方法
    all_locusts_spawned.release()

# 挂在到locust钩子函数(所有的Locust示例产生完成时触发)
events.hatch_complete += on_hatch_complete
class Knight_Login(TaskSet):
    def on_start(self):
        all_locusts_spawned.wait()
    @task(1)
    def index(self):
        url = 'https://www.baidu.com'
        self.client.get(url)
    @task(1)
    def login(self):
        pass
class Knight_User(HttpLocust):
    task_set = Knight_Login
    host = "https://www.sina.com"

你可能感兴趣的:(Locust,python)