From bfef114690b0889f61601c86dad91606247427c8 Mon Sep 17 00:00:00 2001 From: Andrew Baldwin Date: Thu, 18 Jan 2024 15:14:29 -0500 Subject: [PATCH] Add --config-users parser argument --- locust/argument_parser.py | 7 +++++++ locust/main.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/locust/argument_parser.py b/locust/argument_parser.py index 2c8f950f14..024df355f2 100644 --- a/locust/argument_parser.py +++ b/locust/argument_parser.py @@ -306,6 +306,13 @@ def setup_parser_arguments(parser): dest="list_commands", help="Show list of possible User classes and exit", ) + parser.add_argument( + "--config-users", + type=str, + nargs="*", + help="User configuration as a JSON string or file. A list of arguments or an Array of JSON configuration may be provided", + env_var="LOCUST_CONFIG_USERS", + ) web_ui_group = parser.add_argument_group("Web UI options") web_ui_group.add_argument( diff --git a/locust/main.py b/locust/main.py index 27eb44ccb1..7855cfb706 100644 --- a/locust/main.py +++ b/locust/main.py @@ -3,6 +3,7 @@ import errno import logging import os +import json import signal import sys import time @@ -358,6 +359,33 @@ def kill_workers(children): available_user_tasks=available_user_tasks, ) + if options.config_users: + for json_user_config in options.config_users: + try: + if json_user_config.endswith(".json"): + with open(json_user_config) as file: + user_config = json.load(file) + else: + user_config = json.loads(json_user_config) + + def ensure_user_class_name(config): + if "user_class_name" not in config: + logger.error("The user config must specify a user_class_name") + sys.exit(-1) + + if isinstance(user_config, list): + for config in user_config: + ensure_user_class_name(config) + + environment.update_user_class(config) + else: + ensure_user_class_name(user_config) + + environment.update_user_class(user_config) + except Exception as e: + logger.error(f"The --config-users arugment must be in valid JSON string or file: {e}") + sys.exit(-1) + if ( shape_class and not shape_class.use_common_options