Skip to content

Commit

Permalink
Added example for how to stop a test on a certain condition.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberw committed May 27, 2020
1 parent 3bbc669 commit 55a350d
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/stop_on_threshold.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# An example of how to stop locust if a threshold (in this case the fail ratio) is exceeded
from locust import task, HttpUser, events
from locust.runners import STATE_STOPPING, STATE_STOPPED, STATE_CLEANUP
import time
import gevent


class MyUser(HttpUser):
@task
def my_task(self):
for _ in range(10):
self.client.get("http://www.google.com/")
time.sleep(1)
for _ in range(5):
self.client.get("http://www.google.com/error")
time.sleep(1)


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:
print(f"fail ratio was {environment.runner.stats.total.fail_ratio}, quitting")
environment.runner.quit()
return


@events.init.add_listener
def on_locust_init(environment, **_kwargs):
gevent.spawn(checker, environment)

0 comments on commit 55a350d

Please sign in to comment.