【发布时间】:2021-11-25 04:30:50
【问题描述】:
我正在使用locust 执行负载测试,并使用来自LoadTestShape 的自定义类实现了基于RPS 的负载生成。
这个自定义类包含不同负载形状的方法,如_tick_step_increase、_tick_rps_based 等:
class CustomLoadShape:
# factory class that holds different shape methods #
def __init__(self, step_interval=5, max_rps=500):
self.step_interval = step_interval
self.max_rps = max_rps
def _tick_step_increase(self):
# use self.interval here #
pass
def _tick_square_wave(self):
# use self.max_rps here #
pass
class MyCustomLoadShape(LoadTestShape, CustomLoadShape):
# this class is present in locustfile #
def __init__(self):
CustomLoadShape.__init__()
setattr(self, "tick", self._tick_rps_based) # this is how i make sure the `tick` method is defined
我正在接受一些 cli 参数,例如 max-rps,并希望将它们传递给 MyCustomLoadShape(最终传递给 CustomLoadShape)。
我知道在任务函数中,我们可以通过self.environment.parsed_options.max_rps访问cli args。
直觉上,我尝试在__init__ 中做同样的事情,但它没有self.environment,而self.runner 是None(不知何故,它在tick 方法中显示为一个对象)。
虽然我们可以使用self.runner.environment.parsed_options.max_rps 在tick 方法中访问cli args,但我想知道在__init__ 中是否有一种方法可以做到这一点,以便我们可以以更OOP 的方式处理属性。
【问题讨论】:
标签: python load-testing locust