30

I'm currently passing custom parameters to my load test using environment variables. For example, my test class looks like this:

from locust import HttpLocust, TaskSet, task
import os

class UserBehavior(TaskSet):

    @task(1)
    def login(self):
        test_dir = os.environ['BASE_DIR']
        auth=tuple(open(test_dir + '/PASSWORD').read().rstrip().split(':')) 
        self.client.request(
           'GET',
           '/myendpoint',
           auth=auth
        )   

class WebsiteUser(HttpLocust):
    task_set = UserBehavior

Then I'm running my test with:

locust -H https://myserver --no-web --clients=500 --hatch-rate=500 --num-request=15000 --print-stats --only-summary

Is there a more locust way that I can pass custom parameters to the locust command line application?

matusko
  • 3,487
  • 3
  • 20
  • 31
Chris Snow
  • 23,813
  • 35
  • 144
  • 309

3 Answers3

17

You could use like env <parameter>=<value> locust <options> and use <parameter> inside the locust script to use its value

E.g., env IP_ADDRESS=100.0.1.1 locust -f locust-file.py --no-web --clients=5 --hatch-rate=1 --num-request=500 and use IP_ADDRESS inside the locust script to access its value which is 100.0.1.1 in this case.

neonidian
  • 1,221
  • 13
  • 20
9

Nowadays it is possible to add custom parameters to Locust (it wasnt possible when this question was originally asked, at which time using env vars was probably the best option).

Since version 2.2, custom parameters are even forwarded to the workers in a distributed run.

https://docs.locust.io/en/stable/extending-locust.html#custom-arguments

from locust import HttpUser, task, events


@events.init_command_line_parser.add_listener
def _(parser):
    parser.add_argument("--my-argument", type=str, env_var="LOCUST_MY_ARGUMENT", default="", help="It's working")
    # Set `include_in_web_ui` to False if you want to hide from the web UI
    parser.add_argument("--my-ui-invisible-argument", include_in_web_ui=False, default="I am invisible")


@events.test_start.add_listener
def _(environment, **kw):
    print("Custom argument supplied: %s" % environment.parsed_options.my_argument)


class WebsiteUser(HttpUser):
    @task
    def my_task(self):
        print(f"my_argument={self.environment.parsed_options.my_argument}")
        print(f"my_ui_invisible_argument={self.environment.parsed_options.my_ui_invisible_argument}")
Cyberwiz
  • 11,027
  • 3
  • 20
  • 40
-3

It is not recommended to run locust in command line if you want to test in high concurrency. As in --no-web mode, you can only use one CPU core, so that you can not make full use of your test machine.

Back to your question, there is not another way to pass custom parameters to locust in command line.

Leo Lee
  • 5
  • 3