8

I'm doing a verification in my Locust setup and if that fails, I would like locust to exit immediately.

To do that, I'm raising an exception, but locust continues with the tests until time limit is reached.

I'd like for it to not even start the tests if setup fails. Is there a way to do that?

Locust code

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...

As you can see from ^ only exits when time limit is reached, even though setup failed.

Joao Coelho
  • 2,838
  • 4
  • 30
  • 36

3 Answers3

3

For me - helps this thing. Just one problem - locust exits with code 0, like "successful"

import greenlet


class MyLocust(Locust):
    task_set = MyTaskSet

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

If you want stop single locust,use below code. locust.exceptions will help for stop single locust operation.

from locust.exception import StopLocust

class MyLocust(Locust):
    task_set = MyTaskSet

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

If you want stop all locust,use below code.

from locust.main import runners

class MyLocust(Locust):
        task_set = MyTaskSet

        def setup(self):
            if True:
                raise runners.locust_runner.quit()
PrakashT
  • 883
  • 1
  • 7
  • 17
1

You could always use quit() to quit Locust all together. Another option would be to use raise StopLocust():

class MyLocust(Locust):
    task_set = MyTaskSet

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

Also, check this out Stopping a locust

BarakStout
  • 61
  • 1
  • 2