Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overwrite weight by config user before environment weight validation #2971

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions locust/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(
available_user_classes: dict[str, User] | None = None,
available_shape_classes: dict[str, LoadTestShape] | None = None,
available_user_tasks: dict[str, list[TaskSet | Callable]] | None = None,
override_user_weights: list[dict] | None = None,
dispatcher_class: type[UsersDispatcher] = UsersDispatcher,
):
self.runner: Runner | None = None
Expand Down Expand Up @@ -105,6 +106,10 @@ def __init__(
self.worker_logs: dict[str, list[str]] = {}
"""Captured logs from all connected workers"""

if override_user_weights:
for user_dict in override_user_weights:
self.update_user_class(user_dict)

self._remove_user_classes_with_weight_zero()
self._validate_user_class_name_uniqueness()
self._validate_shape_class_instance()
Expand Down
40 changes: 21 additions & 19 deletions locust/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def create_environment(
available_user_classes=None,
available_shape_classes=None,
available_user_tasks=None,
override_user_weights=None,
):
"""
Create an Environment instance from options
Expand All @@ -89,6 +90,7 @@ def create_environment(
available_user_classes=available_user_classes,
available_shape_classes=available_shape_classes,
available_user_tasks=available_user_tasks,
override_user_weights=override_user_weights,
)


Expand Down Expand Up @@ -366,21 +368,7 @@ def kill_workers(children):
See https://github.com/locustio/locust/wiki/Installation#increasing-maximum-number-of-open-files-limit for more info."""
)

# create locust Environment
locustfile_path = None if not locustfile else os.path.basename(locustfile)

environment = create_environment(
user_classes,
options,
events=locust.events,
shape_class=shape_class,
locustfile=locustfile_path,
parsed_locustfiles=locustfiles,
available_user_classes=available_user_classes,
available_shape_classes=available_shape_classes,
available_user_tasks=available_user_tasks,
)

user_new_weights: list[dict] = []
if options.config_users:
for json_user_config in options.config_users:
try:
Expand All @@ -398,12 +386,10 @@ def ensure_user_class_name(config):
if isinstance(user_config, list):
for config in user_config:
ensure_user_class_name(config)

environment.update_user_class(config)
user_new_weights.append(config)
else:
ensure_user_class_name(user_config)

environment.update_user_class(user_config)
user_new_weights.append(user_config)
except json.decoder.JSONDecodeError as e:
logger.error(f"The --config-users argument must be in valid JSON string or file: {e}")
sys.exit(-1)
Expand All @@ -416,6 +402,22 @@ def ensure_user_class_name(config):
logger.exception(e)
sys.exit(-1)

# create locust Environment
locustfile_path = None if not locustfile else os.path.basename(locustfile)

environment = create_environment(
user_classes,
options,
events=locust.events,
shape_class=shape_class,
locustfile=locustfile_path,
parsed_locustfiles=locustfiles,
available_user_classes=available_user_classes,
available_shape_classes=available_shape_classes,
available_user_tasks=available_user_tasks,
override_user_weights=user_new_weights,
)

if (
shape_class
and not shape_class.use_common_options
Expand Down