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"
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值