I have a custom fork of locust to which I am adding custom UI fields to the start and edit screens on locust. I have added the fields to the existing form in index.html
. On web.py
, in the /swarm method, I added the request results from the new UI fields to variables on the environment.
Now in my locust file I can access these variables from the environment in an event class like this:
@events.test_start.add_listener
def test_start(environment):
print(environment.variable1)
print(environment.host)
The problem with this is it only runs on the master instance of locust. I need the data inside of the on_start method of my HTTP user class. I think this is possible because inside of the HHTP user class I can get the self.environment.host
and it will populate correctly but if I do self.environment.variable1
it always comes back at None
.
I added the new variable to the top of the env class in env.py
and set the variables in web.py
in the /swarm
block with something like:
environment.variable1 = request.form["variable1"]
I think the problem is the WebUI class is only spawned on the master instance but this problem is still bypasses somehow with host. Thanks in advance for your help.
First of all, instead of maintaining your own fork of Locust, you could consider adding your own web routes and extending the web UI to add the custom logic and data that you need. Could be better for you long-term from a maintenance perspective.
As far as getting that data to the workers, you could either create a web route that they could call, which you could do on test start, or you could send a message from the master to the workers. Example from the Locust docs:
from locust import events
from locust.runners import MasterRunner, WorkerRunner
# Fired when the worker recieves a message of type 'test_users'
def setup_test_users(environment, msg, **kwargs):
for user in msg.data:
print(f"User {user['name']} recieved")
environment.runner.send_message('acknowledge_users', f"Thanks for the {len(msg.data)} users!")
# Fired when the master recieves a message of type 'acknowledge_users'
def on_acknowledge(msg, **kwargs):
print(msg.data)
@events.init.add_listener
def on_locust_init(environment, **_kwargs):
if not isinstance(environment.runner, MasterRunner):
environment.runner.register_message('test_users', setup_test_users)
if not isinstance(environment.runner, WorkerRunner):
environment.runner.register_message('acknowledge_users', on_acknowledge)
@events.test_start.add_listener
def on_test_start(environment, **_kwargs):
if not isinstance(environment.runner, MasterRunner):
users = [
{"name": "User1"},
{"name": "User2"},
{"name": "User3"},
]
environment.runner.send_message('test_users', users)