I am running Locust tests using distributed load generation with the flag:
--processes -1
I used to get the stats while the tests were running from
from locust.stats import get_percentile_stats_summary, get_stats_summary
This does not work any more, and it is just getting stats from different workers. How can I get the combined stats directly from the master node while the tests are running?
Here is the code to access the stats:
class MyUser(FastHttpUser):
wait_time = between(0.1, 0.5)
def on_start(self):
self._start_background_task()
def _background_task(self):
while True:
print("Background task running...")
request_stats = self.environment.stats
# Calculate total requests
num_requests = request_stats.num_requests
2
Use the test_start
event. I havent actually run this, but it should be close enough 🙂
bg_task = None
def background_task(environment):
while True:
print("Background task running...")
request_stats = environment.stats
...
@events.test_start.add_listener
def test_start(environment: Environment, **_kwargs) -> None:
if not isinstance(environment.runner, locust.runners.WorkerRunner):
bg_task = gevent.spawn(background_task, environment)
@events.test_stop.add_listener
def test_stop(environment: Environment, **_kwargs) -> None:
bg_task.kill()
Check out https://github.com/locustio/locust/blob/7c85b7988c643ee8f688c8bb6919272c38fbcf7b/examples/test_data_management.py for more examples.