Skip to content

Commit

Permalink
Log a message if total fixed_count is higher than number of users to …
Browse files Browse the repository at this point in the history
…spawn (#2793)

* Log a message if total fixed_count is higher than number of users to spawn (--users)

* test_warning_with_lower_user_count_than_fixed_count: dont use shell=True

* only check fixed count total against options.num_users if it is set.

* Better error message handling for test_distributed

---------

Co-authored-by: Lars Holmberg <[email protected]>
  • Loading branch information
cyberw and Lars Holmberg authored Aug 2, 2024
1 parent 3a21ef8 commit 0523b51
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
7 changes: 7 additions & 0 deletions locust/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,13 @@ def kill_workers(children):
# list() call is needed to consume the dict_view object in Python 3
user_classes = list(user_classes.values())

if not shape_class and options.num_users:
fixed_count_total = sum([user_class.fixed_count for user_class in user_classes])
if fixed_count_total > options.num_users:
logger.info(
f"Total fixed_count of User classes ({fixed_count_total}) is greater than the specified number of users ({options.num_users}), so not all will be spawned."
)

if os.name != "nt" and not options.master:
try:
import resource
Expand Down
50 changes: 50 additions & 0 deletions locust/test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,48 @@ def test_spawing_with_fixed_multiple_locustfiles(self):
self.assertIn("Shutting down (exit code 0)", output)
self.assertEqual(0, proc.returncode)

def test_warning_with_lower_user_count_than_fixed_count(self):
LOCUSTFILE_CONTENT = textwrap.dedent(
"""
from locust import User, task, constant
class User1(User):
fixed_count = 2
wait_time = constant(1)
@task
def t(self):
pass
class User2(User):
fixed_count = 2
wait_time = constant(1)
@task
def t(self):
pass
"""
)
with mock_locustfile(content=LOCUSTFILE_CONTENT) as mocked:
proc = subprocess.Popen(
[
"locust",
"-f",
mocked.file_path,
"--headless",
"--run-time",
"1s",
"-u",
"3",
],
stderr=STDOUT,
stdout=PIPE,
text=True,
)

output = proc.communicate()[0]
self.assertIn("Total fixed_count of User classes (4) is greater than ", output)

def test_with_package_as_locustfile(self):
with TemporaryDirectory() as temp_dir:
with open(f"{temp_dir}/__init__.py", mode="w"):
Expand Down Expand Up @@ -1612,6 +1654,14 @@ def t(self):
stdout=PIPE,
text=True,
)
try:
stdout = proc.communicate(timeout=9)[0]
except Exception:
proc.kill()
proc_worker.kill()
stdout = proc.communicate()[0]
worker_stdout = proc_worker.communicate()[0]
assert False, f"master never finished: {stdout}, worker output: {worker_stdout}"
stdout = proc.communicate()[0]
proc_worker.communicate()

Expand Down

0 comments on commit 0523b51

Please sign in to comment.