0

I need to run locust in distributed mode. I'd like to use custom arguments, and it is needed to add specific value of argument to each worker.

Here is sample python code:


Locust test runner
"""
from locust import HttpUser, task, events, constant_pacing, tag


@events.init_command_line_parser.add_listener
def add_custom_parameters(parser):
    """Set arguments which can be passed also via web ui"""

    parser.add_argument(
        "--property",
        type=str,
        env_var="PROPERTY",
        default="",
        help="set name or id",
    )


class AwesomeUser(HttpUser):
    """
    One AwesomeUser class to rule them all...
    """

    host = "EMPTY"
    wait_time = constant_pacing(1)


    def on_start(self):
        """
        On start procedure.
        """

        print(f"HERE: {self.environment.parsed_options.property}")

    @task(10)
    @tag("test_it")
    def test_it(self):
        """
        Test if custom parameters can be used in that way.
        """

        print(f"property: {self.environment.parsed_options.property}")

if __name__ == "__main__":
    AwesomeUser.tasks = [AwesomeUser.test_it]

I'd like to use docker-compose.yaml, there were many attempts, but it looks that I cananot manage it. Sample code that is not working:

version: '3'
services:
  master:
    build:
      context: .
    volumes:
      - type: bind
        source: "./tests"
        target: "/home/locust/tests"

    ports:
     - "8089:8089"
    command: -f /home/locust/tests/load_test.py --master -u 3 -r 1

  worker:
    build:
      context: .
    volumes:
      - type: bind
        source: "./tests"
        target: "/home/locust/tests"
    command: -f /home/locust/tests/load_test.py --worker --master-host master --property "SOSN_1"

  worker2:
    build:
      context: .
    volumes:
      - type: bind
        source: "./tests"
        target: "/home/locust/tests"
    command: -f /home/locust/tests/load_test.py --worker --master-host master --property "SOSN_2"

  worker3:
    build:
      context: .
    volumes:
      - type: bind
        source: "./tests"
        target: "/home/locust/tests"
    command: -f /home/locust/tests/load_test.py --worker --master-host master --property "SOSN_3"

There is workaround - run it in each screen, each worker as a master (in that case I'm able to run parallel many locust scripts):

killall screen
source venv/bin/activate

for i in {1..3}; do
    sleep 2
    echo create worker screen worker_$i
    screen -dmS "worker_$i" locust -f tests/load_test.py --property "SOSN_$i" -t 10m --headless
done

However I hope that it can be done via docker-compose. An ideal it will be if I can use command like it: docker-compose up --scale worker=3 and as a result of it it will be run locust in distributed mode with 3 workers, each will use different value of my custom argument.

Is it possible?

Bartek Z
  • 11
  • 2

1 Answers1

0

Is it possible?

Here's an example docker-compose.yaml file:

version: '3'
services:
  master:
    build:
      context: .
    volumes:
      - type: bind
        source: "./tests"
        target: "/home/locust/tests"
    ports:
      - "8089:8089"
    command: -f /home/locust/tests/load_test.py --master -u 3 -r 1

  worker:
    build:
      context: .
    volumes:
      - type: bind
        source: "./tests"
        target: "/home/locust/tests"
    command: >
      sh -c "locust -f /home/locust/tests/load_test.py --worker --master-host master --property $PROPERTY"

    environment:
      - PROPERTY=SOSN_1

  worker2:
    build:
      context: .
    volumes:
      - type: bind
        source: "./tests"
        target: "/home/locust/tests"
    command: >
      sh -c "locust -f /home/locust/tests/load_test.py --worker --master-host master --property $PROPERTY"

    environment:
      - PROPERTY=SOSN_2

  worker3:
    build:
      context: .
    volumes:
      - type: bind
        source: "./tests"
        target: "/home/locust/tests"
    command: >
      sh -c "locust -f /home/locust/tests/load_test.py --worker --master-host master --property $PROPERTY"

    environment:
      - PROPERTY=SOSN_3

Now, when you run it as:

$ docker-compose up --scale worker=3

Docker Compose will start three worker containers, each with a different PROPERTY value, which will be passed to the Locust worker. The Locust workers will connect to the master container to run the load test in distributed mode.

Jishan Shaikh
  • 1,572
  • 2
  • 13
  • 31