您如何将设置中的值传递给locust.io中的on_start

问题描述 投票:0回答:1

蝗虫示例文件:

from locust import HttpLocust, TaskSet, task, between

class UserBehavior(TaskSet):

    def setup(self):
        self.store_this = 'some_stuff'


    def on_start(self):
        print(self.store_this)

    @task(1)
    def index(self):
        self.client.get('https:example.com/about')

    @task(1)
    def about(self):
        self.client.get('https:example.com')


class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    wait_time = between(5, 9)

但是这会产生错误“ AttributeError:'UserBehavior'对象没有属性'store_this',但是如果我使用pdb进行调试,可以这样做。如何将信息从setup传递到on_start

但是如果我这样做:

class UserBehavior(TaskSet):

    store_this = ''

    def setup(self):
        self.store_this = 'some_stuff'


    def on_start(self):
        print('In on start %s ' %self.store_this)

将重置为空白字符串并打印

In on start some_stuff
In on start 
In on start 

那么,在设置,on_start,on_stop和拆卸之间传递信息的正确方法是什么?

python-3.x locust
1个回答
0
投票

setup()仅对给定类型的TaskSet执行一次。

如果要存储可从TaskSet的所有实例访问的值,请使用类变量(UserBehaviour.store_this)或全局变量。

如果需要在TaskSet的不同实例中存储不同的值,则不能使用setup(),因此需要在on_start()中设置该值。

© www.soinside.com 2019 - 2024. All rights reserved.