如何使蝗虫的安装失败?

时间:2019-09-25 21:59:01

标签: python locust

我正在我的蝗虫设置中进行验证,如果失败,我希望蝗虫立即退出。

为此,我提出了一个例外,但是蝗虫会继续进行测试,直到达到时间限制为止。

如果安装失败,我希望它甚至不开始测试。有办法吗?

蝗虫代码

class MyLocust(Locust):
    task_set = MyTaskSet

    def setup(self):
        if True:
            raise ValueError('Setup failed')

stdout / stderr:

locust -f MyTest.py --no-web -c 10 -r 10 -t 5s

INFO/locust.main: Run time limit set to 5 seconds
INFO/locust.main: Starting Locust 0.11.0
INFO/locust.runners: Hatching and swarming 10 clients at the rate 10 clients/s...
    Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
    Total                                                              0     0(0.00%)                                       0.00

ERROR/stderr: Traceback (most recent call last):
ERROR/stderr: 
ERROR/stderr: File "src/gevent/greenlet.py", line 766, in gevent._greenlet.Greenlet.run
ERROR/stderr: 
ERROR/stderr: File "/venv_37_1/lib/python3.7/site-packages/locust/runners.py", line 114, in start_locust
    locust().run(runner=self)
ERROR/stderr: 
ERROR/stderr: File "/venv_37_1/lib/python3.7/site-packages/locust/core.py", line 192, in __init__
    super(HttpLocust, self).__init__()
ERROR/stderr: 
ERROR/stderr: File "/venv_37_1/lib/python3.7/site-packages/locust/core.py", line 143, in __init__
    self.setup()
ERROR/stderr: 
ERROR/stderr: File "/MyTest.py", line 220, in setup
    raise ValueError('Setup failed')
ERROR/stderr: 
ERROR/stderr: ValueError: Setup failed
ERROR/stderr: 
ERROR/stderr: 2019-09-25T21:43:39Z
ERROR/stderr: 
ERROR/stderr: <Greenlet at 0x12c8c2950: start_locust(<class 'MyTest.LoadTest'>)> failed with ValueError
INFO/locust.runners: All locusts hatched: LoadTest: 10
    Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
    Total                                                              0     0(0.00%)                                       0.00

    Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
    Total                                                              0     0(0.00%)                                       0.00

INFO/locust.main: Time limit reached. Stopping Locust.
INFO/locust.main: Shutting down (exit code 0), bye.
INFO/locust.main: Cleaning up runner...
INFO/locust.main: Running teardowns...

从^中可以看到,即使setup失败,也只有在达到时间限制时才会退出。

3 个答案:

答案 0 :(得分:2)

对我来说-帮助事情。只是一个问题-蝗虫以代码0退出,例如“成功”

import greenlet


class MyLocust(Locust):
    task_set = MyTaskSet

    def setup(self):
        if True:
            raise greenlet.error('Setup failed')

答案 1 :(得分:2)

如果要停止蝗虫,请使用以下代码。 locust.exceptions将有助于停止单个蝗虫的运行。

from locust.exception import StopLocust

class MyLocust(Locust):
    task_set = MyTaskSet

    def setup(self):
        if True:
            raise StopLocust()

如果要停止所有蝗虫,请使用以下代码。

from locust.main import runners

class MyLocust(Locust):
        task_set = MyTaskSet

        def setup(self):
            if True:
                raise runners.locust_runner.quit()

答案 2 :(得分:0)

您可以始终使用quit()一起退出蝗虫。另一种选择是使用raise StopLocust()

class MyLocust(Locust):
    task_set = MyTaskSet

    def setup(self):
        if True:
            raise StopLocust()

此外,请查看Stopping a locust