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

fix: edit load test missing the userclasses data #2171

Merged
merged 4 commits into from
Aug 25, 2022

Conversation

alterhu2020
Copy link
Contributor

fix the problem userclasses missing issue when edit the load test

@cyberw
Copy link
Collaborator

cyberw commented Aug 23, 2022

Does that work though? If you change the selected Users, that wont affect the ones that are already running, right?

@alterhu2020
Copy link
Contributor Author

alterhu2020 commented Aug 23, 2022

Does that work though? If you change the selected Users, that wont affect the ones that are already running, right?

Yes,I had tested from my machine, It works as expected. It only reused the selected Users we had picked previously.

@cyberw
Copy link
Collaborator

cyberw commented Aug 23, 2022

Nice! Can you add a unit test that validates it?

Here’s some inspiration:

def test_swarm_shape_class_specified(self):

locust/web.py Outdated
@@ -164,7 +164,8 @@ def swarm() -> Response:
user_classes[user_class_name] = user_class_object

else:
user_classes = self.environment.available_user_classes
user_classes = {key: value for (key, value) in self.environment.available_user_classes.items if
Copy link
Contributor

@mikenester mikenester Aug 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried out this change and got

File "/Users/michaelnester/git/locust/locust/web.py", line 167, in swarm
  user_classes = {
TypeError: 'builtin_function_or_method' object is not iterable

I think it needs to be self.environment.available_user_classes.items(), which worked for me (using python 3.9)

Copy link
Contributor Author

@alterhu2020 alterhu2020 Aug 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, sorry. My fault typo error. python 3.10 works well from myside, but I think it's better to take use the code you corrected here.

@mikenester
Copy link
Contributor

mikenester commented Aug 24, 2022

This change breaks test_swarm_defaults_to_all_available_userclasses_when_userclass_picker_is_active_and_no_userclass_in_payload.

The workflow is:

  1. Run Locust w/ class picker and multiple user classes to choose from
  2. Select one user class
    • More specifically: select up to N - 1 user classes where N is the total number of available user classes
  3. Run the test
  4. Stop the test and start a new test, but don't select any user classes
    • This defaults to all available user classes

Only the user class from the first run will be used

@mikenester
Copy link
Contributor

@alterhu2020
Copy link
Contributor Author

@alterhu2020 I think i got a fix for this: master...mikenester:locust:fix-class-picker-bug-edit-running-test

Yes, the code is better than mine, please go ahead merged it. thanks

@mikenester
Copy link
Contributor

@alterhu2020 If you merge this alterhu2020#1, it should automatically update this PR and run the tests

@alterhu2020
Copy link
Contributor Author

BTW, @mikenester
When I run the locust unit test code or run the main.py or _main_.py from pycharm directly, it often returns following error:


Error
Traceback (most recent call last):
  File "C:\Users\Administrator\.conda\envs\locust\lib\unittest\loader.py", line 34, in testFailure
    raise self._exception
ImportError: Failed to import test module: locust
Traceback (most recent call last):
  File "C:\Users\Administrator\.conda\envs\locust\lib\unittest\loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "D:\locust\locust\__init__.py", line 18, in <module>
    from .contrib.fasthttp import FastHttpUser
  File "D:\locust\locust\contrib\fasthttp.py", line 24, in <module>
    from locust.env import Environment
  File "D:\locust\locust\env.py", line 18, in <module>
    from .web import WebUI
  File "D:\locust\locust\web.py", line 6, in <module>
    from html import escape
  File "D:\locust\locust\html.py", line 6, in <module>
    from .stats import sort_stats
ImportError: attempted relative import with no known parent package

I know it related that we use the relative import path, but I don't know how to bypass it? Have we any guideline that can help people quick to setup the develop contribution environment? thanks

…ng-test

Added check to swarm for class picker. Uses pre selected user classes if test is already running
@alterhu2020
Copy link
Contributor Author

@alterhu2020 If you merge this alterhu2020#1, it should automatically update this PR and run the tests

LGTM, Done as well

@cyberw
Copy link
Collaborator

cyberw commented Aug 24, 2022

Nice! But the test doesnt actually validate that the Users changed, does it?

@mikenester
Copy link
Contributor

Nice! But the test doesnt actually validate that the Users changed, does it?

Perhaps I misunderstood, but I thought we don't want the users to change. So an example of the expected workflow should be:

  1. Run locust with --class-picker and available user classes: UserClass1, UserClass2, UserClass3
  2. Run a test with just UserClass1
  3. Edit the running test. User Class Count & Spawn Rate values can be arbitrary
  4. Validate that only UserClass1 is active
    • The reported bug being that instead, UserClass1, UserClass2 & UserClass3 were all active
    • Validated on line 440 of locust/test/test_web.py in this PR

@alterhu2020
Copy link
Contributor Author

Nice! But the test doesnt actually validate that the Users changed, does it?

Perhaps I misunderstood, but I thought we don't want the users to change. So an example of the expected workflow should be:

  1. Run locust with --class-picker and available user classes: UserClass1, UserClass2, UserClass3

  2. Run a test with just UserClass1

  3. Edit the running test. User Class Count & Spawn Rate values can be arbitrary

  4. Validate that only UserClass1 is active

    • The reported bug being that instead, UserClass1, UserClass2 & UserClass3 were all active
    • Validated on line 440 of locust/test/test_web.py in this PR

Yes. I think @mikenester is right. @cyberw for the Users Changed scenario I think It related to another feature I raised in issue: #2169, right? If we can support that I think it's a great improvement.
Thanks Team.

@mikenester
Copy link
Contributor

mikenester commented Aug 24, 2022

BTW, @mikenester When I run the locust unit test code or run the main.py or _main_.py from pycharm directly, it often returns following error:


Error
Traceback (most recent call last):
  File "C:\Users\Administrator\.conda\envs\locust\lib\unittest\loader.py", line 34, in testFailure
    raise self._exception
ImportError: Failed to import test module: locust
Traceback (most recent call last):
  File "C:\Users\Administrator\.conda\envs\locust\lib\unittest\loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "D:\locust\locust\__init__.py", line 18, in <module>
    from .contrib.fasthttp import FastHttpUser
  File "D:\locust\locust\contrib\fasthttp.py", line 24, in <module>
    from locust.env import Environment
  File "D:\locust\locust\env.py", line 18, in <module>
    from .web import WebUI
  File "D:\locust\locust\web.py", line 6, in <module>
    from html import escape
  File "D:\locust\locust\html.py", line 6, in <module>
    from .stats import sort_stats
ImportError: attempted relative import with no known parent package

I know it related that we use the relative import path, but I don't know how to bypass it? Have we any guideline that can help people quick to setup the develop contribution environment? thanks

Not sure if I can help with this one. I don't think you'll be able to successfully run individual files, since relative import in python can get broken in that scenario.

In terms of testing, I personally modify tox.ini in the following way. Change line 30 from

python3 -m coverage run -m unittest discover []

to the intended unittest path:

python3 -m coverage run -m unittest locust.tests.test_web.TestWebUI.test_exceptions_csv

Then you can run:

  • tox -e py37 if you're using python 3.7
  • tox -e py38 if you're using python 3.8
  • tox -e py39 if you're using python 3.9
  • tox -e py310 if you're using python 3.10

@alterhu2020
Copy link
Contributor Author

BTW, @mikenester When I run the locust unit test code or run the main.py or _main_.py from pycharm directly, it often returns following error:


Error
Traceback (most recent call last):
  File "C:\Users\Administrator\.conda\envs\locust\lib\unittest\loader.py", line 34, in testFailure
    raise self._exception
ImportError: Failed to import test module: locust
Traceback (most recent call last):
  File "C:\Users\Administrator\.conda\envs\locust\lib\unittest\loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "D:\locust\locust\__init__.py", line 18, in <module>
    from .contrib.fasthttp import FastHttpUser
  File "D:\locust\locust\contrib\fasthttp.py", line 24, in <module>
    from locust.env import Environment
  File "D:\locust\locust\env.py", line 18, in <module>
    from .web import WebUI
  File "D:\locust\locust\web.py", line 6, in <module>
    from html import escape
  File "D:\locust\locust\html.py", line 6, in <module>
    from .stats import sort_stats
ImportError: attempted relative import with no known parent package

I know it related that we use the relative import path, but I don't know how to bypass it? Have we any guideline that can help people quick to setup the develop contribution environment? thanks

Not sure if I can help with this one. I don't think you'll be able to successfully run individual files, since relative import in python can get broken in that scenario.

In terms of testing, I personally modify tox.ini in the following way. Change line 30 from

python3 -m coverage run -m unittest discover []

to the intended unittest path:

python3 -m coverage run -m unittest locust.tests.test_web.TestWebUI.test_exceptions_csv

Then you can run:

  • tox -e py37 if you're using python 3.7
  • tox -e py38 if you're using python 3.8
  • tox -e py39 if you're using python 3.9
  • tox -e py310 if you're using python 3.10

Thanks the tips, but how we can active the debugger, I want to read the code step by step...

@cyberw
Copy link
Collaborator

cyberw commented Aug 25, 2022

Perhaps I misunderstood, but I thought we don't want the users to change

I misunderstood what you were trying to do, sorry :)

This looks good to me now. Should I merge?

@mikenester
Copy link
Contributor

This looks good to me now. Should I merge?

Yeah, I think it's good to go 👍

@cyberw cyberw merged commit e42f649 into locustio:master Aug 25, 2022
@cyberw
Copy link
Collaborator

cyberw commented Aug 25, 2022

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants