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

Fail to spawn fixed users when run with DoubleWave load shape #2345

Closed
TC-zpei opened this issue May 16, 2023 · 14 comments
Closed

Fail to spawn fixed users when run with DoubleWave load shape #2345

TC-zpei opened this issue May 16, 2023 · 14 comments
Labels

Comments

@TC-zpei
Copy link

TC-zpei commented May 16, 2023

Describe the bug

When running DoubleWave load shape with both fix count user and spawn users, during spawning new users, locust will first check the fixed user count using blow codes(dispatch.py)

              while len(spawned_classes) != len(fixed_users):
                    user_name: Optional[str] = next(cycle_fixed_gen)
                    if not user_name:
                        break

                    if current_fixed_users_count[user_name] < fixed_users[user_name].fixed_count:
                        current_fixed_users_count[user_name] += 1
                        if current_fixed_users_count[user_name] == fixed_users[user_name].fixed_count:
                            spawned_classes.add(user_name)
                        yield user_name

                        # 'self._try_dispatch_fixed' was changed outhere,  we have to recalculate current count
                        if self._try_dispatch_fixed:
                            current_fixed_users_count = {u: self._get_user_current_count(u) for u in fixed_users}
                            spawned_classes.clear()
                            self._try_dispatch_fixed = False

If current_fixed_users_count[user_name] >= fixed_users[user_name].fixed_count:, it will start loop with below warning:
CPU usage above 90%! This may constrain your throughput and may even give inconsistent response time measurements!

Expected behavior

Locust should spawn DoubleWave load shape with fixed count users.
屏幕截图 2023-05-16 140343

Actual behavior

Locust will get stuck when spawning new users if the current fixed user count is equal expected fixed user count.
屏幕截图 2023-05-16 135920

Steps to reproduce

locust -f locustfile.py --host https://www.bing.com

import time
from locust import HttpUser, task
from locust import constant_throughput
from locust import LoadTestShape
import math


class AdminUser(HttpUser):
    wait_time = constant_throughput(1)
    fixed_count = 2

    @task
    def my_task(self):
        self.client.get("", name="admin user")
        time.sleep(1)


class WebUser(HttpUser):
    weight = 3

    @task
    def my_task(self):
        self.client.get("", name="web user")
        time.sleep(1)


class MobileUser(HttpUser):
    weight = 1

    @task
    def my_task(self):
        self.client.get("", name="mobile user")
        time.sleep(1)


class DoubleWave(LoadTestShape):
    min_users = 2
    peak_one_users = 4
    peak_two_users = 6
    time_limit = 150

    def tick(self):
        run_time = round(self.get_run_time())

        if run_time < self.time_limit:
            user_count = (
                (self.peak_one_users - self.min_users)
                * math.e ** -(((run_time / (self.time_limit / 10 * 2 / 3)) - 5) ** 2)
                + (self.peak_two_users - self.min_users)
                * math.e ** -(((run_time / (self.time_limit / 10 * 2 / 3)) - 10) ** 2)
                + self.min_users
            )
            return (round(user_count), round(user_count))
        else:
            return None

Environment

  • OS: WSL2
  • Python version: Python 3.10.11
  • Locust version: Locust 2.15.1
  • Locust command line that you ran: locust -f locustfile.py --host https://www.bing.com
  • Locust file contents
@TC-zpei TC-zpei added the bug label May 16, 2023
@TC-zpei TC-zpei changed the title Fail to spawn fix users when run with DoubleWave load shape Fail to spawn fixed users when run with DoubleWave load shape May 16, 2023
@cyberw
Copy link
Collaborator

cyberw commented May 16, 2023

@EzR1d3r I think this is ”your” code? Could you have a look?

@EzR1d3r
Copy link
Contributor

EzR1d3r commented May 17, 2023

@EzR1d3r I think this is ”your” code? Could you have a look?

Ok, I will when have enought time, may be at weekend.

@EzR1d3r
Copy link
Contributor

EzR1d3r commented May 21, 2023

I have repeated the bug. I guess I know how to fix, but I have to check.

@EzR1d3r
Copy link
Contributor

EzR1d3r commented May 22, 2023

@TC-zpei could you check pls the branch #2350, it should work.

@TC-zpei
Copy link
Author

TC-zpei commented May 23, 2023

@EzR1d3r I have checked out the fixed code from #2350, and it is working as expected. Thank you for the quick fix.

@EzR1d3r
Copy link
Contributor

EzR1d3r commented May 23, 2023

@EzR1d3r I have checked out the fixed code from #2350, and it is working as expected. Thank you for the quick fix.

Good. I also want to write unit test for this case and then it will be ready to merge.

@EzR1d3r
Copy link
Contributor

EzR1d3r commented May 24, 2023

@cyberw I am trying to run tests locally, but I have the error.
I remember I was fighting against this last time.. but it should be simple? What I am doing wrong?

d:\Projects\locust\$ pytest ./locust/test/test_main.py
============ test session starts =============
platform win32 -- Python 3.10.8, pytest-7.3.1, pluggy-1.0.0
rootdir: D:\Projects\locust
collected 0 items / 1 error

=================== ERRORS ======================= 
_____________________ ERROR collecting locust/test/test_main.py ______________________ 
ImportError while importing test module 'D:\Projects\locust\locust\test\test_main.py'. 
Hint: make sure your test modules/packages have valid Python names.
Traceback:
C:\Python310\lib\importlib\__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
locust\__init__.py:13: in <module>
    from ._version import version as __version__
E   ModuleNotFoundError: No module named 'locust._version'
============================== short test summary info =============================== 
ERROR locust/test/test_main.py
!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!! 
================================== 1 error in 0.31s ==================================

@cyberw
Copy link
Collaborator

cyberw commented May 24, 2023

I dont remember that issue. That file should be generated when doing pip install (e.g. pip install -e .). If it isnt then maybe just create the file manually :)

@cyberw
Copy link
Collaborator

cyberw commented May 24, 2023

This is what mine looks like after installing:

# file generated by setuptools_scm
# don't change, don't track in version control
__version__ = version = '2.15.2.dev13'
__version_tuple__ = version_tuple = (2, 15, 2, 'dev13')

@EzR1d3r
Copy link
Contributor

EzR1d3r commented May 25, 2023

pip install -e .

Thx, it works for test_dispatch.py. It is enought for me, but with test_main.py it raises an import error with termios module (on win). I am not sure, but it will work only on linux, or I am wrong?

@cyberw
Copy link
Collaborator

cyberw commented May 25, 2023

On windows it is supposed to import win32console instead of termios. But never mind. Is the change ready for merge?

if os.name == "nt":
    from win32api import STD_INPUT_HANDLE
    from win32console import (
        GetStdHandle,
        KEY_EVENT,
        ENABLE_ECHO_INPUT,
        ENABLE_LINE_INPUT,
        ENABLE_PROCESSED_INPUT,
    )
else:
    import select
    import termios
    import tty

@EzR1d3r
Copy link
Contributor

EzR1d3r commented May 26, 2023

Is the change ready for merge?

No, I still didn't add the test for this case. I will mention when it be ready.

@EzR1d3r
Copy link
Contributor

EzR1d3r commented May 28, 2023

@cyberw it's ready for merge
@TC-zpei thx for the report!

@cyberw
Copy link
Collaborator

cyberw commented Jun 9, 2023

fixed in #2350

@cyberw cyberw closed this as completed Jun 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants