Reputation: 517
I am unable to stop the test, if failure percentage or avg response time goes above the defined in test_stop. Below is the sample code:-
@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]
I am using below command to run the test from terminal:-
locust -f --headless -u 1000 -r 100 --run-time 2m
Command is working as expected, test is running for 2 min, but test does not fails if error percentage is > 0.01 or of average response time is > 2 sec
Upvotes: 0
Views: 1394
Reputation: 11396
Have a look at locust-plugins's --check-fail-ratio
for a battle tested way to achieve this:
https://github.com/SvenskaSpel/locust-plugins#command-line-options
Or, if you want to run the check during the actual test and interrupt it when it fails, you can use a background 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)
Upvotes: 2