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

values used to calculate user distribution can contain variables #100

Merged
merged 3 commits into from
Oct 4, 2024
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
56 changes: 47 additions & 9 deletions grizzly_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,8 @@ class ScenarioProperties:
identifier: str
user: Optional[str]
weight: float
iterations: int
user_count: int
_iterations: Optional[int]
_user_count: Optional[int]

def __init__(
self,
Expand All @@ -687,10 +687,34 @@ def __init__(
self.name = name
self.index = index
self.user = user
self.iterations = iterations or 1
self._iterations = iterations
self.weight = weight or 1.0
self.identifier = f'{index:03}'
self.user_count = user_count or 0
self._user_count = user_count

@property
def iterations(self) -> int:
if self._iterations is None:
raise ValueError('iterations has not been set')

return self._iterations

@iterations.setter
def iterations(self, value: int) -> None:
self._iterations = value

@property
def user_count(self) -> int:
if self._user_count is None:
raise ValueError('user count has not been set')
return self._user_count

@user_count.setter
def user_count(self, value: int) -> None:
self._user_count = value

def is_fulfilled(self) -> bool:
return self.user is not None and self._iterations is not None and self._user_count is not None

distribution: Dict[str, ScenarioProperties] = {}
variables = {key.replace('TESTDATA_VARIABLE_', ''): _guess_datatype(value) for key, value in environ.items() if key.startswith('TESTDATA_VARIABLE_')}
Expand All @@ -709,6 +733,7 @@ def _pre_populate_scenario(scenario: Scenario, index: int) -> None:
use_weights = True

for index, scenario in enumerate(grizzly_cli.SCENARIOS):
scenario_variables: Dict[str, Any] = {}
if len(scenario.steps) < 1:
raise ValueError(f'scenario "{scenario.name}" does not have any steps')

Expand All @@ -725,25 +750,38 @@ def _pre_populate_scenario(scenario: Scenario, index: int) -> None:
use_weights = False
scenario_user_count_total = 0

for step in scenario.steps:
if step.name.startswith('a user of type'):
for step in (scenario.background_steps or []) + scenario.steps:
if step.name.startswith('value for variable'):
match = re.match(r'value for variable "([^"]*)" is "([^"]*)"', step.name)
if match:
try:
variable_name = match.group(1)
variable_value = Template(match.group(2)).render(**variables, **scenario_variables)
scenario_variables.update({variable_name: variable_value})
except:
continue
elif step.name.startswith('a user of type'):
match = re.match(r'a user of type "([^"]*)" (with weight "([^"]*)")?.*', step.name)
if match:
distribution[scenario.name].user = match.group(1)
distribution[scenario.name].weight = int(float(Template(match.group(3) or '1.0').render(**variables)))
distribution[scenario.name].weight = int(float(Template(match.group(3) or '1.0').render(**variables, **scenario_variables)))
elif step.name.startswith('repeat for'):
match = re.match(r'repeat for "([^"]*)" iteration[s]?', step.name)
if match:
distribution[scenario.name].iterations = int(round(float(Template(match.group(1)).render(**variables)), 0))
distribution[scenario.name].iterations = int(round(float(Template(match.group(1)).render(**variables, **scenario_variables)), 0))
(distribution[scenario.name].iterations)
elif any([pattern in step.name for pattern in ['users of type', 'user of type']]):
match = re.match(r'"([^"]*)" user[s]? of type "([^"]*)".*', step.name)
if match:
scenario_user_count = int(round(float(Template(match.group(1)).render(**variables)), 0))
scenario_user_count = int(round(float(Template(match.group(1)).render(**variables, **scenario_variables)), 0))
scenario_user_count_total += scenario_user_count

distribution[scenario.name].user_count = scenario_user_count
distribution[scenario.name].user = match.group(2)

if distribution[scenario.name].is_fulfilled():
break

scenario_count = len(distribution.keys())
assert scenario_user_count_total is not None
if scenario_count > scenario_user_count_total:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies = [
"behave ==1.2.6",
"Jinja2 ==3.1.4",
"requests ==2.32.3",
"packaging ==24.0",
"packaging ==24.1",
"chardet ==5.2.0",
"tomli ==2.0.1",
"pyotp ==2.9.0",
Expand Down
25 changes: 1 addition & 24 deletions tests/e2e/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from tempfile import NamedTemporaryFile
from typing import Optional
from os import path, pathsep
from datetime import datetime

import pytest
import yaml
Expand Down Expand Up @@ -123,7 +122,7 @@ def test_e2e_run_example(e2e_fixture: End2EndFixture) -> None:
feature_file,
env_conf_file.name.replace(f'{str(example_root)}{pathsep}', ''),
cwd=str(example_root),
arguments=['--csv-prefix', '-l', 'test_run.log'],
arguments=['-l', 'test_run.log'],
)

# problems with a locust DEBUG log message containing ERROR in the message on macos-latest
Expand Down Expand Up @@ -153,28 +152,6 @@ def test_e2e_run_example(e2e_fixture: End2EndFixture) -> None:

assert 'compose.yaml: `version` is obsolete' not in result

datestamp = datetime.now().astimezone().strftime('%Y%m%dT')

csv_file_exceptions = list(example_root.glob('*_exceptions.csv'))
assert len(csv_file_exceptions) == 1
assert csv_file_exceptions[0].read_text().strip() == 'Count,Message,Traceback,Nodes'
assert csv_file_exceptions[0].name.startswith(f'grizzly_example_{datestamp}')

csv_file_failures = list(example_root.glob('*_failures.csv'))
assert len(csv_file_failures) == 1
assert csv_file_failures[0].read_text().strip() == 'Method,Name,Error,Occurrences'
assert csv_file_failures[0].name.startswith(f'grizzly_example_{datestamp}')

csv_file_stats = list(example_root.glob('*_stats.csv'))
assert len(csv_file_stats) == 1
assert csv_file_stats[0].read_text().strip() != ''
assert csv_file_stats[0].name.startswith(f'grizzly_example_{datestamp}')

csv_file_stats_history = list(example_root.glob('*_stats_history.csv'))
assert len(csv_file_stats_history) == 1
assert csv_file_stats_history[0].read_text().strip() != ''
assert csv_file_stats_history[0].name.startswith(f'grizzly_example_{datestamp}')

log_file_result = (example_root / 'test_run.log').read_text()

# problems with a locust DEBUG log message containing ERROR in the message on macos-latest
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ def test_distribution_of_users_per_scenario(capsys: CaptureFixture, mocker: Mock
],
[
'Given a user of type "RestApi" load testing "https://localhost"',
'And repeat for "1" iteration',
'And ask for value of variable "test_variable_2"',
'And ask for value of variable "test_variable_1"',
],
Expand All @@ -437,6 +438,7 @@ def test_distribution_of_users_per_scenario(capsys: CaptureFixture, mocker: Mock
],
[
'Given a user of type "MessageQueueUser" load testing "mqs://localhost"',
'And repeat for "1" iteration',
'And ask for value of variable "foo"',
],
)
Expand All @@ -456,6 +458,7 @@ def test_distribution_of_users_per_scenario(capsys: CaptureFixture, mocker: Mock
],
[
'Given a user of type "RestApi" load testing "https://localhost"',
'And repeat for "1" iteration',
'And ask for value of variable "test_variable_2"',
'And ask for value of variable "test_variable_1"',
],
Expand All @@ -467,6 +470,7 @@ def test_distribution_of_users_per_scenario(capsys: CaptureFixture, mocker: Mock
],
[
'Given a user of type "MessageQueueUser" load testing "mqs://localhost"',
'And repeat for "1" iteration',
'And ask for value of variable "foo"',
],
)
Expand Down
Loading