我正在我的蝗虫设置中进行验证,如果验证失败,我希望蝗虫立即退出。
为了做到这一点,我抛出了一个异常,但是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失败,也只有在达到时间限制时才会退出。
发布于 2019-11-25 15:15:26
对我来说-对这件事有帮助。只有一个问题-- locust退出时代码为0,比如"successful“
import greenlet
class MyLocust(Locust):
task_set = MyTaskSet
def setup(self):
if True:
raise greenlet.error('Setup failed')发布于 2020-02-16 14:26:42
如果你想停止单一蝗虫,使用下面的代码。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()发布于 2020-02-16 14:06:55
您可以随时使用quit()一起退出蝗虫。另一种选择是使用raise StopLocust():
class MyLocust(Locust):
task_set = MyTaskSet
def setup(self):
if True:
raise StopLocust()另外,请查看Stopping a locust
https://stackoverflow.com/questions/58107082
复制相似问题