如果失败百分比或平均响应时间超过testrongtop中定义的值,则无法停止测试。下面是示例代码:-
@events.test_stop.add_listener
def results(environment, **kw):
if environment.stats.total.fail_ratio > 0.01:
logging.error("Test failed due to failure ratio > 1%")
environment.process_exit_code = 1
elif environment.stats.total.avg_response_time > 2:
logging.error("Test failed due to average response time ratio > 200 ms")
environment.process_exit_code = 1
elif environment.stats.total.get_response_time_percentile(0.95) > 8:
logging.error("Test failed due to 95th percentil response time > 800 ms")
environment.process_exit_code = 1
else:
environment.process_exit_code = 0
@task
def osp_pick(self):
return super().request(
name="pick",
json_file="data/pick.json",
ssm=self.ssm,
my_task=self,
)
class StockTransfer(HttpUser):
host = "https://zz-zz.dev.zz.zz.zz.com" # Change this
tasks = [Handler]
我使用下面的命令从终端运行测试:-
locust -f --headless -u 1000 -r 100 --run-time 2m
命令按预期工作,测试运行2分钟,但如果错误百分比为>测试不会失败;0.01或平均响应时间为>2秒
看看蝗虫插件的--check-fail-ratio
,以获得经过战斗测试的方法来实现这一点:
https://github.com/SvenskaSpel/locust-plugins命令行选项或者,如果您想在实际测试期间运行check,并在失败时中断它,您可以使用背景greenlet:
def checker(environment):
while not environment.runner.state in [STATE_STOPPING, STATE_STOPPED, STATE_CLEANUP]:
time.sleep(1)
if environment.runner.stats.total.fail_ratio > 0.2:
logging.info(f"fail ratio was {environment.runner.stats.total.fail_ratio}, quitting")
environment.process_exit_code = 2
environment.runner.quit()
return
@events.init.add_listener
def on_locust_init(environment, **_kwargs):
if not isinstance(environment.runner, WorkerRunner):
gevent.spawn(checker, environment)