I'd like to annotate a load test in grafana via the API, and for that I need a way to access the number of connected slaves as well as the number of simulated users from the master (in order to then send that as an annotation to grafana). How can I access these numbers in the master's python code only?
Asked
Active
Viewed 2,275 times
1 Answers
0
In the current locust version (0.13.5) you can access runners.locust_runner.num_clients to get the number of simulated users.
Starting with the next release it will be renamed user_count (and also become more reliable). So if you can, wait for 0.14 to be released (or install locust directly from a git clone).
Edit (responding to your comment here):
There probably is a way to count the slaves, but I dont know and dont have time to figure it out right now, sorry :)
I use these functions to detect if I'm on slave or master:
def is_slave():
return "--slave" in sys.argv
def is_master():
return "--master" in sys.argv

Cyberwiz
- 11,027
- 3
- 20
- 40
-
Thanks! Is there a way to access the number of slaves too? Also, is there a way to run code only on the master, not on any of the slaves? – yawdQuejos Jan 29 '20 at 07:25
-
1You can get the number of connected slaves using `len(runners.locust_runner.clients.all)` (or `len(runners.locust_runner.clients.running)` or `len(runners.locust_runner.clients.hatching)` or `len(runners.locust_runner.clients.ready)`). – heyman Jan 29 '20 at 11:02
-
Has it been removed in locust 1.2.3? `>>> locust.__version__ '1.2.3'` When I import, it doesn't exist - `>>> from locust import runners >>> runners.locust_runner.clients Traceback (most recent call last): File "
", line 1, in – Nazil Khan Sep 06 '20 at 04:57AttributeError: module 'locust.runners' has no attribute 'locust_runner'` -
Got it. If you want count of users inside the method of a class inheriting `TaskSet` , then use `self.user.environment.runner.user_count`. – Nazil Khan Sep 06 '20 at 05:15