Overwriting weight by config-users may lead to crash #2852
Closed
Description
Prerequisites
- I am using the latest version of Locust
- I am reporting a bug, not asking a question
Description
In the attached locustfile, there are no "weight" defined for both user classes, which works fine.
However, if I overwrite weights with config-users options, it will crash:
$ locust -u 2 --config-users '[{"user_class_name": "CBotApiUser", "weight": 0}]'
[2024-08-15 11:15:41,053] C02GM2X6MD6R/INFO/locust.main: Starting web interface at http://0.0.0.0:8089
init: RevisionApiUser, 1
init: CBotApiUser, 0
[2024-08-15 11:15:41,071] C02GM2X6MD6R/INFO/locust.main: Starting Locust 2.31.2
[2024-08-15 11:15:47,674] C02GM2X6MD6R/INFO/locust.runners: Ramping to 2 users at a rate of 1.00 per second
Traceback (most recent call last):
File "src/gevent/greenlet.py", line 908, in gevent._gevent_cgreenlet.Greenlet.run
File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/runners.py", line 544, in <lambda>
lambda: self._start(user_count, spawn_rate, wait=wait, user_classes=user_classes)
File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/runners.py", line 493, in _start
for dispatched_users in self._users_dispatcher:
File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 133, in __next__
users_on_workers = next(self._dispatcher_generator)
File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 168, in _dispatcher
yield self._add_users_on_workers()
File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 300, in _add_users_on_workers
for user in self._user_generator:
File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 388, in _user_gen
yield next(weighted_users_gen)
File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 32, in _kl_generator
heap = [(x * log2(x / (x + 1.0)), x + 1.0, x, name) for name, x in users]
File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 32, in <listcomp>
heap = [(x * log2(x / (x + 1.0)), x + 1.0, x, name) for name, x in users]
ValueError: math domain error
2024-08-15T03:15:47Z <Greenlet at 0x10ff7d900: <lambda>> failed with ValueError
[2024-08-15 11:15:47,694] C02GM2X6MD6R/CRITICAL/locust.runners: Unhandled exception in greenlet: <Greenlet at 0x10ff7d900: <lambda>>
Traceback (most recent call last):
File "src/gevent/greenlet.py", line 908, in gevent._gevent_cgreenlet.Greenlet.run
File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/runners.py", line 544, in <lambda>
lambda: self._start(user_count, spawn_rate, wait=wait, user_classes=user_classes)
File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/runners.py", line 493, in _start
for dispatched_users in self._users_dispatcher:
File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 133, in __next__
users_on_workers = next(self._dispatcher_generator)
File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 168, in _dispatcher
yield self._add_users_on_workers()
File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 300, in _add_users_on_workers
for user in self._user_generator:
File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 388, in _user_gen
yield next(weighted_users_gen)
File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 32, in _kl_generator
heap = [(x * log2(x / (x + 1.0)), x + 1.0, x, name) for name, x in users]
File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 32, in <listcomp>
heap = [(x * log2(x / (x + 1.0)), x + 1.0, x, name) for name, x in users]
ValueError: math domain error
KeyboardInterrupt
As you can see, I use --config-users
to overwrite the second user's weight to 0, then it crashed.
Command line
$ locust -u 2 --config-users '[{"user_class_name": "CBotApiUser", "weight": 0}]'
Locustfile contents
from locust import HttpUser, task, tag, constant, events, constant_pacing
@events.init.add_listener
def on_locust_init(environment, **kwargs):
for n, u in environment.available_user_classes.items():
print(f'init: {n}, {u.weight}')
class RevisionApiUser(HttpUser):
wait_time = constant_pacing(5)
def __init__(self, environment):
super().__init__(environment)
self.first = True
def on_start(self) -> None:
print(f"revision: {self.weight}")
@task
def get_commit_list(self):
if self.first:
print(f"revision: {self.weight}")
self.first = False
class CBotApiUser(HttpUser):
wait_time = constant_pacing(5)
def on_start(self) -> None:
print(f"cbot: {self.weight}")
def __init__(self, environment):
super().__init__(environment)
self.first = True
@task
def get_commits(self):
if self.first:
print(f"cbot: {self.weight}")
self.first = False
Python version
3.9.19
Locust version
locust 2.31.2 from /Users/chaol/venv/3.9/lib/python3.9/site-packages/locust (Python 3.9.19, OpenSSL 3.3.0)
Operating system
MacOS