python locust--Setups, Teardowns, on_start, and on_stop

创建一个locust测试脚本,如下:

from locust import HttpLocust, TaskSet, task


class UserBehavior(TaskSet):
    def setup(self):
        print('task setup')

    def teardown(self):
        print('task teardown')

    def on_start(self):
        # 虚拟用户启动Task时运行
        print('start')

    def on_stop(self):
        # 虚拟用户结束Task时运行
        print('end')

    @task(2)
    def index(self):
        self.client.get("/")

    @task(1)
    def profile(self):
        self.client.get("/profile")


class WebsiteUser(HttpLocust):
    def setup(self):
        print('locust setup')

    def teardown(self):
        print('locust teardown')
    task_set = UserBehavior
    min_wait = 5000
    max_wait = 9000


if __name__ == '__main__':
    pass

说明:

Locust类有setup和teardown,TaskSet类有setup、teardown、on_start、on_stop。

每次启动locust时运行setup方法,退出时运行teardown方法,locust执行TaskSet时运行TaskSet的setup方法,退出时运行teardown方法,每个虚拟用户执行操作时运行on_start方法,退出时执行on_stop方法,运行上面的脚本,执行顺序如下:

执行顺序如下:

Locust setup

TaskSet setup

TaskSet on_start

TaskSet tasks

TaskSet on_stop

TaskSet teardown

Locust teardown

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值