Locust – How to pass new configuration through code to locust
Question:
I am trying to run locust as a library from python code and along with that I wanted to use locust-plugins library. The main problem I am facing is that I am not able to find how to pass additional command line arguments from code to locust ? locust-plugins library is providing command line arguments like timescale and pguser. but I am not able to pass it to locust through code.
If I use locust as a command line tool, I can run it as following command:
locust –headless -f backend_test.py –host="" –timescale –pguser postgres
For running locust as a library, I followed this guide from official documentation: https://docs.locust.io/en/stable/use-as-lib.html
But Environment class does not take any additional arguments. I wanted to pass timescale and pguser argument to locust from code.
Any help or guidance is appreciated. Thanks !!
Answers:
I’m not sure anyone has done this before so it is kind of expected that you run in to some issues 🙂
What I would try is parsing a "fake" command line, and passing it to the Environment constructor.
You’ll need locust-plugins 2.6.12 or later (I renamed the add_arguments
method just now)
parser = locust.argument_parser.get_parser()
p = parser.parse_options("--headless -f backend_test.py --host="" --timescale --pguser postgres")
env = Environment(parsed_options=p, ...)
runner = env.create_local_runner()
env.events.init.fire(environment=env, runner=runner)
I am trying to run locust as a library from python code and along with that I wanted to use locust-plugins library. The main problem I am facing is that I am not able to find how to pass additional command line arguments from code to locust ? locust-plugins library is providing command line arguments like timescale and pguser. but I am not able to pass it to locust through code.
If I use locust as a command line tool, I can run it as following command:
locust –headless -f backend_test.py –host="" –timescale –pguser postgres
For running locust as a library, I followed this guide from official documentation: https://docs.locust.io/en/stable/use-as-lib.html
But Environment class does not take any additional arguments. I wanted to pass timescale and pguser argument to locust from code.
Any help or guidance is appreciated. Thanks !!
I’m not sure anyone has done this before so it is kind of expected that you run in to some issues 🙂
What I would try is parsing a "fake" command line, and passing it to the Environment constructor.
You’ll need locust-plugins 2.6.12 or later (I renamed the add_arguments
method just now)
parser = locust.argument_parser.get_parser()
p = parser.parse_options("--headless -f backend_test.py --host="" --timescale --pguser postgres")
env = Environment(parsed_options=p, ...)
runner = env.create_local_runner()
env.events.init.fire(environment=env, runner=runner)