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 for UserClass picker not loading all available Shape Classes #2441

Merged
merged 5 commits into from
Oct 31, 2023
Merged
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
17 changes: 10 additions & 7 deletions locust/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,20 @@ def main():
user_classes: Dict[str, locust.User] = {}
available_user_classes = {}
available_shape_classes = {}
shape_class = None
for _locustfile in locustfiles:
docstring, _user_classes, shape_class = load_locustfile(_locustfile)
docstring, _user_classes, shape_classes = load_locustfile(_locustfile)

# Setting Available Shape Classes
if shape_class:
shape_class_name = type(shape_class).__name__
if shape_class_name in available_shape_classes.keys():
sys.stderr.write(f"Duplicate shape classes: {shape_class_name}\n")
sys.exit(1)
if shape_classes:
shape_class = shape_classes[0]
for shape_class in shape_classes:
shape_class_name = type(shape_class).__name__
if shape_class_name in available_shape_classes.keys():
sys.stderr.write(f"Duplicate shape classes: {shape_class_name}\n")
sys.exit(1)

available_shape_classes[shape_class_name] = shape_class
available_shape_classes[shape_class_name] = shape_class

# Setting Available User Classes
for key, value in _user_classes.items():
Expand Down
17 changes: 17 additions & 0 deletions locust/static/css/application.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion locust/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ <h2>Start new load test</h2>
{% for user in available_user_classes %}
<option value="{{user}}" selected>{{user}}</option>
{% endfor %}
</select><br>range
</select><br>
<label for="available_shape_classes">ShapeClass </label>
<select name="shape_class" id="shape-classes" class="shapeClass">
{% for shape_class in available_shape_classes %}
Expand Down
42 changes: 31 additions & 11 deletions locust/test/test_load_locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@ class ThriftLocust(User):

def test_load_locust_file_from_absolute_path(self):
with mock_locustfile() as mocked:
docstring, user_classes, shape_class = main.load_locustfile(mocked.file_path)
docstring, user_classes, shape_classes = main.load_locustfile(mocked.file_path)
self.assertIn("UserSubclass", user_classes)
self.assertNotIn("NotUserSubclass", user_classes)
self.assertNotIn("LoadTestShape", user_classes)
self.assertIsNone(shape_class)
self.assertEqual(shape_classes, [])

def test_load_locust_file_from_relative_path(self):
with mock_locustfile() as mocked:
docstring, user_classes, shape_class = main.load_locustfile(
docstring, user_classes, shape_classes = main.load_locustfile(
os.path.join(os.path.relpath(mocked.directory, os.getcwd()), mocked.filename)
)

def test_load_locust_file_with_a_dot_in_filename(self):
with mock_locustfile(filename_prefix="mocked.locust.file") as mocked:
docstring, user_classes, shape_class = main.load_locustfile(mocked.file_path)
docstring, user_classes, shape_classes = main.load_locustfile(mocked.file_path)

def test_return_docstring_and_user_classes(self):
with mock_locustfile() as mocked:
docstring, user_classes, shape_class = main.load_locustfile(mocked.file_path)
docstring, user_classes, shape_classes = main.load_locustfile(mocked.file_path)
self.assertEqual("This is a mock locust file for unit testing", docstring)
self.assertIn("UserSubclass", user_classes)
self.assertNotIn("NotUserSubclass", user_classes)
Expand All @@ -70,11 +70,31 @@ def tick(self):
"""
)
with mock_locustfile(content=content) as mocked:
docstring, user_classes, shape_class = main.load_locustfile(mocked.file_path)
docstring, user_classes, shape_classes = main.load_locustfile(mocked.file_path)
self.assertEqual("This is a mock locust file for unit testing", docstring)
self.assertIn("UserSubclass", user_classes)
self.assertNotIn("NotUserSubclass", user_classes)
self.assertEqual(shape_class.__class__.__name__, "LoadTestShape")
self.assertEqual(shape_classes[0].__class__.__name__, "LoadTestShape")

def test_with_multiple_shape_classes(self):
content = MOCK_LOCUSTFILE_CONTENT + textwrap.dedent(
"""\
class LoadTestShape1(LoadTestShape):
def tick(self):
pass

class LoadTestShape2(LoadTestShape):
def tick(self):
pass
"""
)
with mock_locustfile(content=content) as mocked:
docstring, user_classes, shape_classes = main.load_locustfile(mocked.file_path)
self.assertEqual("This is a mock locust file for unit testing", docstring)
self.assertIn("UserSubclass", user_classes)
self.assertNotIn("NotUserSubclass", user_classes)
self.assertEqual(shape_classes[0].__class__.__name__, "LoadTestShape1")
self.assertEqual(shape_classes[1].__class__.__name__, "LoadTestShape2")

def test_with_abstract_shape_class(self):
content = MOCK_LOCUSTFILE_CONTENT + textwrap.dedent(
Expand All @@ -92,10 +112,10 @@ class UserLoadTestShape(UserBaseLoadTestShape):
)

with mock_locustfile(content=content) as mocked:
_, user_classes, shape_class = main.load_locustfile(mocked.file_path)
_, user_classes, shape_classes = main.load_locustfile(mocked.file_path)
self.assertNotIn("UserBaseLoadTestShape", user_classes)
self.assertNotIn("UserLoadTestShape", user_classes)
self.assertEqual(shape_class.__class__.__name__, "UserLoadTestShape")
self.assertEqual(shape_classes[0].__class__.__name__, "UserLoadTestShape")

def test_with_not_imported_shape_class(self):
content = MOCK_LOCUSTFILE_CONTENT + textwrap.dedent(
Expand All @@ -107,9 +127,9 @@ def tick(self):
)

with mock_locustfile(content=content) as mocked:
_, user_classes, shape_class = main.load_locustfile(mocked.file_path)
_, user_classes, shape_classes = main.load_locustfile(mocked.file_path)
self.assertNotIn("UserLoadTestShape", user_classes)
self.assertEqual(shape_class.__class__.__name__, "UserLoadTestShape")
self.assertEqual(shape_classes[0].__class__.__name__, "UserLoadTestShape")

def test_create_environment(self):
options = parse_options(
Expand Down
18 changes: 13 additions & 5 deletions locust/test/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,15 @@ class User2(User):
def t(self):
pass

class TestShape(LoadTestShape):
class TestShape1(LoadTestShape):
def tick(self):
run_time = self.get_run_time()
if run_time < 10:
return 4, 4
else:
return None

class TestShape2(LoadTestShape):
def tick(self):
run_time = self.get_run_time()
if run_time < 10:
Expand All @@ -573,8 +581,8 @@ def tick(self):

self.environment.web_ui.userclass_picker_is_active = True
self.environment.available_user_classes = {"User1": User1, "User2": User2}
self.environment.available_shape_classes = {"TestShape": TestShape()}
self.environment.shape_class = None
self.environment.available_shape_classes = {"TestShape1": TestShape1(), "TestShape2": TestShape2()}
self.environment.shape_class = TestShape1()

response = requests.post(
"http://127.0.0.1:%i/swarm" % self.web_port,
Expand All @@ -583,14 +591,14 @@ def tick(self):
"spawn_rate": 5,
"host": "https://localhost",
"user_classes": "User1",
"shape_class": "TestShape",
"shape_class": "TestShape2",
},
)

self.assertEqual(200, response.status_code)
self.assertEqual("https://localhost", response.json()["host"])
self.assertEqual(self.environment.host, "https://localhost")
assert isinstance(self.environment.shape_class, TestShape)
assert isinstance(self.environment.shape_class, TestShape2)

# stop
gevent.sleep(1)
Expand Down
12 changes: 4 additions & 8 deletions locust/util/load_locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import inspect
import os
import sys
from typing import Dict, Optional, Tuple
from typing import Dict, List, Optional, Tuple
from ..shape import LoadTestShape
from ..user import User

Expand All @@ -21,7 +21,7 @@ def is_shape_class(item):
return bool(inspect.isclass(item) and issubclass(item, LoadTestShape) and not getattr(item, "abstract", True))


def load_locustfile(path) -> Tuple[Optional[str], Dict[str, User], Optional[LoadTestShape]]:
def load_locustfile(path) -> Tuple[Optional[str], Dict[str, User], List[LoadTestShape]]:
"""
Import given locustfile path and return (docstring, callables).

Expand Down Expand Up @@ -65,10 +65,6 @@ def load_locustfile(path) -> Tuple[Optional[str], Dict[str, User], Optional[Load
user_classes = {name: value for name, value in vars(imported).items() if is_user_class(value)}

# Find shape class, if any, return it
shape_classes = [value for name, value in vars(imported).items() if is_shape_class(value)]
if shape_classes:
shape_class = shape_classes[0]()
else:
shape_class = None
shape_classes = [value() for name, value in vars(imported).items() if is_shape_class(value)]

return imported.__doc__, user_classes, shape_class
return imported.__doc__, user_classes, shape_classes
6 changes: 5 additions & 1 deletion locust/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,11 @@ def swarm() -> Response:
if environment.shape_class and environment.runner is not None:
environment.runner.start_shape()
return jsonify(
{"success": True, "message": "Swarming started using shape class", "host": environment.host}
{
"success": True,
"message": f"Swarming started using shape class '{type(environment.shape_class).__name__}'",
"host": environment.host,
}
)

if self._swarm_greenlet is not None:
Expand Down