robotframework-locust 1.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- LocustLibrary/__init__.py +143 -0
- LocustLibrary/builder.py +169 -0
- LocustLibrary/calllog.py +115 -0
- LocustLibrary/checks.py +122 -0
- LocustLibrary/distributed.py +52 -0
- LocustLibrary/feed.py +61 -0
- LocustLibrary/k6/__init__.py +1 -0
- LocustLibrary/k6/cli.py +66 -0
- LocustLibrary/k6/emit.py +69 -0
- LocustLibrary/k6/options.py +128 -0
- LocustLibrary/k6/profile.py +130 -0
- LocustLibrary/k6/robot_files.py +127 -0
- LocustLibrary/k6/scan.py +195 -0
- LocustLibrary/parsing.py +80 -0
- LocustLibrary/reporting.py +108 -0
- LocustLibrary/runner.py +135 -0
- LocustLibrary/scenario.py +102 -0
- LocustLibrary/shape.py +33 -0
- LocustLibrary/shared.py +28 -0
- LocustLibrary/stats.py +81 -0
- LocustLibrary/stoprules.py +93 -0
- LocustLibrary/studio/__init__.py +3 -0
- LocustLibrary/studio/app.py +170 -0
- LocustLibrary/studio/build_tab.py +73 -0
- LocustLibrary/studio/fonts/Inter-Medium.ttf +0 -0
- LocustLibrary/studio/fonts/Inter-OFL.txt +92 -0
- LocustLibrary/studio/fonts/Inter-Regular.ttf +0 -0
- LocustLibrary/studio/fonts/Inter-SemiBold.ttf +0 -0
- LocustLibrary/studio/fonts/JetBrainsMono-Medium.ttf +0 -0
- LocustLibrary/studio/fonts/JetBrainsMono-OFL.txt +93 -0
- LocustLibrary/studio/fonts/JetBrainsMono-Regular.ttf +0 -0
- LocustLibrary/studio/fonts.py +17 -0
- LocustLibrary/studio/live.py +131 -0
- LocustLibrary/studio/panels.py +139 -0
- LocustLibrary/studio/report_tab.py +50 -0
- LocustLibrary/studio/run_tab.py +187 -0
- LocustLibrary/studio/sidebar.py +82 -0
- LocustLibrary/studio/themes.py +83 -0
- LocustLibrary/studio/worker.py +39 -0
- LocustLibrary/suiteplan.py +76 -0
- LocustLibrary/users.py +153 -0
- LocustLibrary/validation.py +55 -0
- LocustLibrary/webui.py +32 -0
- robotframework_locust-1.0.1.dist-info/METADATA +178 -0
- robotframework_locust-1.0.1.dist-info/RECORD +49 -0
- robotframework_locust-1.0.1.dist-info/WHEEL +5 -0
- robotframework_locust-1.0.1.dist-info/entry_points.txt +3 -0
- robotframework_locust-1.0.1.dist-info/licenses/LICENSE +21 -0
- robotframework_locust-1.0.1.dist-info/top_level.txt +1 -0
LocustLibrary/k6/scan.py
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"""Find a k6 script's exec functions and the plain HTTP calls inside them.
|
|
2
|
+
|
|
3
|
+
A function is plain when every statement is an http.<verb> call with a string URL
|
|
4
|
+
and literal body and params, a check() or a sleep(). A sleep may name a top-level number constant, or one
|
|
5
|
+
read as Number(__ENV.X || n), which counts as its default n. Anything else needs a hand port,
|
|
6
|
+
and `reason` says which line stopped the scan.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from dataclasses import dataclass, field
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
import tree_sitter_javascript as tsjs
|
|
13
|
+
from tree_sitter import Language, Node, Parser
|
|
14
|
+
|
|
15
|
+
_JS = Language(tsjs.language())
|
|
16
|
+
_HTTP = {"get": "GET", "post": "POST", "put": "PUT", "patch": "PATCH", "del": "DELETE"}
|
|
17
|
+
_WITH_BODY = {"POST", "PUT", "PATCH"}
|
|
18
|
+
_FUNCTIONS = {"function_declaration", "function_expression", "function"}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class _NotPlain(ValueError):
|
|
22
|
+
"""The node needs a hand port; the message says why."""
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass(frozen=True, slots=True)
|
|
26
|
+
class HttpCall:
|
|
27
|
+
method: str
|
|
28
|
+
url: str
|
|
29
|
+
endpoint: str = ""
|
|
30
|
+
body: Any = None
|
|
31
|
+
headers: dict[str, str] = field(default_factory=dict)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass(frozen=True, slots=True)
|
|
35
|
+
class ExecFunction:
|
|
36
|
+
name: str
|
|
37
|
+
line: int
|
|
38
|
+
calls: tuple[HttpCall, ...] = ()
|
|
39
|
+
think_s: float = 0.0
|
|
40
|
+
reason: str = ""
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def simple(self) -> bool:
|
|
44
|
+
return not self.reason
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def scan_source(source: str) -> list[ExecFunction]:
|
|
48
|
+
root = Parser(_JS).parse(source.encode("utf-8")).root_node
|
|
49
|
+
constants = _constants(root)
|
|
50
|
+
found = (_exported(node, constants) for node in root.named_children
|
|
51
|
+
if node.type == "export_statement")
|
|
52
|
+
return [fn for fn in found if fn is not None]
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _constants(root: Node) -> dict[str, float]:
|
|
56
|
+
values: dict[str, float] = {}
|
|
57
|
+
for node in root.named_children:
|
|
58
|
+
if node.type == "lexical_declaration":
|
|
59
|
+
values.update(_declared_numbers(node))
|
|
60
|
+
return values
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _declared_numbers(declaration: Node) -> dict[str, float]:
|
|
64
|
+
values: dict[str, float] = {}
|
|
65
|
+
for declarator in declaration.named_children:
|
|
66
|
+
name = declarator.child_by_field_name("name")
|
|
67
|
+
number = _number(declarator.child_by_field_name("value"))
|
|
68
|
+
if name is not None and number is not None:
|
|
69
|
+
values[_text(name)] = number
|
|
70
|
+
return values
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _number(node: Node | None) -> float | None:
|
|
74
|
+
if node is not None and node.type == "call_expression" and _text(
|
|
75
|
+
node.child_by_field_name("function")
|
|
76
|
+
) in ("Number", "parseFloat", "parseInt"):
|
|
77
|
+
args = node.child_by_field_name("arguments")
|
|
78
|
+
node = args.named_children[0] if args and args.named_children else None
|
|
79
|
+
if node is not None and node.type == "binary_expression" and _text(
|
|
80
|
+
node.child_by_field_name("left")
|
|
81
|
+
).startswith("__ENV."):
|
|
82
|
+
node = node.child_by_field_name("right")
|
|
83
|
+
return float(_text(node)) if node is not None and node.type == "number" else None
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _exported(node: Node, constants: dict[str, float]) -> ExecFunction | None:
|
|
87
|
+
function = next((c for c in node.named_children if c.type in _FUNCTIONS), None)
|
|
88
|
+
if function is None:
|
|
89
|
+
return None
|
|
90
|
+
name_node = function.child_by_field_name("name")
|
|
91
|
+
is_default = any(c.type == "default" for c in node.children)
|
|
92
|
+
name = "default" if is_default or name_node is None else _text(name_node)
|
|
93
|
+
return _scan_body(name, function, constants)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _scan_body(name: str, function: Node, constants: dict[str, float]) -> ExecFunction:
|
|
97
|
+
body = function.child_by_field_name("body")
|
|
98
|
+
statements = body.named_children if body else []
|
|
99
|
+
calls: list[HttpCall] = []
|
|
100
|
+
think_s, reason = 0.0, ""
|
|
101
|
+
for statement in statements:
|
|
102
|
+
try:
|
|
103
|
+
found = _statement(statement, constants)
|
|
104
|
+
except _NotPlain as err:
|
|
105
|
+
reason = reason or f"line {statement.start_point.row + 1}: {err}"
|
|
106
|
+
continue
|
|
107
|
+
if isinstance(found, HttpCall):
|
|
108
|
+
calls.append(found)
|
|
109
|
+
elif isinstance(found, float):
|
|
110
|
+
think_s = found
|
|
111
|
+
return ExecFunction(name, function.start_point.row + 1, tuple(calls), think_s, reason)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _statement(statement: Node, constants: dict[str, float]) -> HttpCall | float | None:
|
|
115
|
+
expr = _expression(statement)
|
|
116
|
+
if expr is None or expr.type != "call_expression":
|
|
117
|
+
raise _NotPlain(statement.type if expr is None else f"{_text(expr)} is not a call")
|
|
118
|
+
callee = _text(expr.child_by_field_name("function"))
|
|
119
|
+
args_node = expr.child_by_field_name("arguments")
|
|
120
|
+
args = args_node.named_children if args_node else []
|
|
121
|
+
if callee == "sleep" and args:
|
|
122
|
+
return _think(args[0], constants)
|
|
123
|
+
if callee == "check":
|
|
124
|
+
return None
|
|
125
|
+
verb = callee.removeprefix("http.")
|
|
126
|
+
if callee.startswith("http.") and verb in _HTTP and args:
|
|
127
|
+
return _http_call(_HTTP[verb], args)
|
|
128
|
+
raise _NotPlain(f"{callee}() is not an HTTP call, check or sleep")
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _think(node: Node, constants: dict[str, float]) -> float:
|
|
132
|
+
if node.type == "identifier" and _text(node) in constants:
|
|
133
|
+
return constants[_text(node)]
|
|
134
|
+
return float(_literal(node))
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _expression(statement: Node) -> Node | None:
|
|
138
|
+
if statement.type == "expression_statement":
|
|
139
|
+
return statement.named_children[0]
|
|
140
|
+
if statement.type == "lexical_declaration":
|
|
141
|
+
return statement.named_children[0].child_by_field_name("value")
|
|
142
|
+
return None
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def _http_call(method: str, args: list[Node]) -> HttpCall:
|
|
146
|
+
if args[0].type != "string":
|
|
147
|
+
raise _NotPlain("URL is not a plain string")
|
|
148
|
+
rest = args[1:]
|
|
149
|
+
body = None
|
|
150
|
+
if method in _WITH_BODY and rest:
|
|
151
|
+
body, rest = _body(rest[0]), rest[1:]
|
|
152
|
+
params = _literal(rest[0]) if rest else {}
|
|
153
|
+
tags = params.get("tags", {})
|
|
154
|
+
endpoint = tags.get("endpoint") or tags.get("name") or ""
|
|
155
|
+
return HttpCall(method, _literal(args[0]), endpoint, body, params.get("headers", {}))
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _body(node: Node) -> Any:
|
|
159
|
+
if node.type == "call_expression" and _text(node.child_by_field_name("function")) == (
|
|
160
|
+
"JSON.stringify"
|
|
161
|
+
):
|
|
162
|
+
args = node.child_by_field_name("arguments")
|
|
163
|
+
node = args.named_children[0] if args and args.named_children else node
|
|
164
|
+
try:
|
|
165
|
+
return _literal(node)
|
|
166
|
+
except _NotPlain as err:
|
|
167
|
+
raise _NotPlain("body is not a literal") from err
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _literal(node: Node) -> Any:
|
|
171
|
+
kind = node.type
|
|
172
|
+
if kind == "string":
|
|
173
|
+
return _text(node)[1:-1]
|
|
174
|
+
if kind == "number":
|
|
175
|
+
text = _text(node)
|
|
176
|
+
return float(text) if "." in text else int(text)
|
|
177
|
+
if kind in ("true", "false", "null"):
|
|
178
|
+
return {"true": True, "false": False, "null": None}[kind]
|
|
179
|
+
if kind == "array":
|
|
180
|
+
return [_literal(item) for item in node.named_children]
|
|
181
|
+
if kind == "object":
|
|
182
|
+
return dict(_pair(pair) for pair in node.named_children)
|
|
183
|
+
raise _NotPlain(f"{_text(node)} is not a literal")
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def _pair(pair: Node) -> tuple[str, Any]:
|
|
187
|
+
key, value = pair.child_by_field_name("key"), pair.child_by_field_name("value")
|
|
188
|
+
if pair.type != "pair" or key is None or value is None:
|
|
189
|
+
raise _NotPlain(f"{_text(pair)} is not a literal pair")
|
|
190
|
+
name = _literal(key) if key.type == "string" else _text(key)
|
|
191
|
+
return name, _literal(value)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _text(node: Node | None) -> str:
|
|
195
|
+
return node.text.decode("utf-8") if node is not None and node.text is not None else ""
|
LocustLibrary/parsing.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"""Parse keyword arguments: durations, stages, status lists, ratios and task targets."""
|
|
2
|
+
|
|
3
|
+
import importlib
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
from typing import Any, cast
|
|
6
|
+
|
|
7
|
+
from .scenario import Stage, StatsRow
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _parse_seconds(value: str) -> float:
|
|
11
|
+
value = value.strip().lower()
|
|
12
|
+
if value.endswith("ms"):
|
|
13
|
+
return float(value[:-2]) / 1000.0
|
|
14
|
+
if value.endswith("s"):
|
|
15
|
+
return float(value[:-1])
|
|
16
|
+
if value.endswith("m"):
|
|
17
|
+
return float(value[:-1]) * 60
|
|
18
|
+
if value.endswith("h"):
|
|
19
|
+
return float(value[:-1]) * 3600
|
|
20
|
+
return float(value)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
_PERCENTILE_FIELDS = {
|
|
24
|
+
"50": "p50",
|
|
25
|
+
"90": "p90",
|
|
26
|
+
"95": "p95",
|
|
27
|
+
"99": "p99",
|
|
28
|
+
"999": "p999",
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _statuses(value: object) -> tuple[int, ...]:
|
|
33
|
+
parts = value if isinstance(value, list | tuple) else str(value).split(",")
|
|
34
|
+
codes = tuple(int(str(p).strip()) for p in parts if str(p).strip())
|
|
35
|
+
if not codes:
|
|
36
|
+
raise ValueError("expected status is required")
|
|
37
|
+
return codes
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _stage(text: str) -> Stage:
|
|
41
|
+
try:
|
|
42
|
+
duration, users = text.split(":")
|
|
43
|
+
stage = Stage(seconds=_parse_seconds(duration), users=int(users))
|
|
44
|
+
except ValueError as err:
|
|
45
|
+
raise ValueError(f"bad stage {text!r}: use duration:users, as 2m:20") from err
|
|
46
|
+
if stage.users < 0 or stage.seconds <= 0:
|
|
47
|
+
raise ValueError(f"bad stage {text!r}: users and duration must be positive")
|
|
48
|
+
return stage
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _python_function(target: str) -> Callable[[Any], None]:
|
|
52
|
+
module_name, _, attr = target.rpartition(".")
|
|
53
|
+
if not module_name:
|
|
54
|
+
raise ValueError(f"use module.function, got {target!r}")
|
|
55
|
+
function = getattr(importlib.import_module(module_name), attr, None)
|
|
56
|
+
if not callable(function):
|
|
57
|
+
raise TypeError(f"{module_name} has no function {attr!r}")
|
|
58
|
+
return cast("Callable[[Any], None]", function)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _keyword_name(name: str) -> str:
|
|
62
|
+
name = name.strip()
|
|
63
|
+
if not name:
|
|
64
|
+
raise ValueError("keyword name is required")
|
|
65
|
+
return name
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _measured_percentile(row: StatsRow, percentile: str) -> float:
|
|
69
|
+
if row.request_count == 0:
|
|
70
|
+
raise RuntimeError("No requests recorded.")
|
|
71
|
+
field = _PERCENTILE_FIELDS.get(percentile.lower().removeprefix("p"))
|
|
72
|
+
if field is None:
|
|
73
|
+
raise RuntimeError(f"Percentile not measured: {percentile}")
|
|
74
|
+
return float(getattr(row, field))
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _ratio(value: object) -> float:
|
|
78
|
+
"""`20%` or `0.2` as a fraction."""
|
|
79
|
+
text = str(value).strip()
|
|
80
|
+
return float(text.rstrip("%")) / 100 if text.endswith("%") else float(text)
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"""Reporting: CSV and HTML export."""
|
|
2
|
+
|
|
3
|
+
import csv
|
|
4
|
+
import html
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from locust.html import get_html_report
|
|
10
|
+
from locust.stats import StatsCSV
|
|
11
|
+
|
|
12
|
+
from .scenario import Stats, StatsRow
|
|
13
|
+
|
|
14
|
+
# Columns of Locust's statistics table; the look is measured from Locust's HTML report.
|
|
15
|
+
HEADINGS = ("Name", "# Requests", "# Fails", "Median (ms)", "95%ile (ms)", "99%ile (ms)",
|
|
16
|
+
"Average (ms)", "Min (ms)", "Max (ms)", "Current RPS", "Failures/s")
|
|
17
|
+
_TABLE = ("border-collapse:collapse;background:#fff;color:rgba(0,0,0,0.87);font-size:14px;"
|
|
18
|
+
"font-family:Roboto,Helvetica,Arial,sans-serif;border-radius:4px;"
|
|
19
|
+
"box-shadow:0 2px 1px -1px rgba(0,0,0,0.2),0 1px 1px 0 rgba(0,0,0,0.14),"
|
|
20
|
+
"0 1px 3px 0 rgba(0,0,0,0.12)")
|
|
21
|
+
_CELL = "padding:16px;border-bottom:1px solid #e0e0e0;text-align:left"
|
|
22
|
+
_HEAD = f"{_CELL};font-weight:500"
|
|
23
|
+
_FAIL = "color:#f64e56"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _write_table(path: Path, write_rows: Callable[[Any], None]) -> None:
|
|
27
|
+
with path.open("w", newline="", encoding="utf-8") as handle:
|
|
28
|
+
write_rows(csv.writer(handle))
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def write_csv_report(
|
|
32
|
+
env: Any,
|
|
33
|
+
base_filepath: str,
|
|
34
|
+
full_history: bool = True,
|
|
35
|
+
output_dir: str | None = None,
|
|
36
|
+
) -> str:
|
|
37
|
+
"""Write one snapshot of Locust CSV stats. Returns the base path."""
|
|
38
|
+
from .validation import validate_output_path
|
|
39
|
+
|
|
40
|
+
base = validate_output_path(base_filepath, output_dir)
|
|
41
|
+
table = StatsCSV(env, [0.5, 0.9, 0.95, 0.99, 0.999])
|
|
42
|
+
_write_table(Path(base + "_stats.csv"), table.requests_csv)
|
|
43
|
+
_write_table(Path(base + "_failures.csv"), table.failures_csv)
|
|
44
|
+
_write_table(Path(base + "_exceptions.csv"), table.exceptions_csv)
|
|
45
|
+
# The streaming history writer never returns, so a one-shot export
|
|
46
|
+
# records only the header.
|
|
47
|
+
if full_history:
|
|
48
|
+
Path(base + "_stats_history.csv").write_text("Timestamp\n", encoding="utf-8")
|
|
49
|
+
return base
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def write_html_report(env: Any) -> str:
|
|
53
|
+
"""Generate the Locust HTML report."""
|
|
54
|
+
report = get_html_report(env) # type: ignore[no-untyped-call]
|
|
55
|
+
return str(report)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def stats_table_html(stats: Stats) -> str:
|
|
59
|
+
"""One row per request name, then the aggregated row, for the Robot log."""
|
|
60
|
+
head = "".join(f'<th style="{_HEAD}">{name}</th>' for name in HEADINGS)
|
|
61
|
+
body = "".join(_table_row(r) for r in stats.rows)
|
|
62
|
+
if stats.aggregated:
|
|
63
|
+
body += _table_row(stats.aggregated)
|
|
64
|
+
return f'<table style="{_TABLE}"><tr>{head}</tr>{body}</table>'
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _table_row(row: StatsRow) -> str:
|
|
68
|
+
cells = [html.escape(row.name), row.request_count, row.failure_count, f"{row.median:.0f}",
|
|
69
|
+
f"{row.p95:.0f}", f"{row.p99:.0f}", f"{row.average:.0f}", f"{row.min:.0f}",
|
|
70
|
+
f"{row.max:.0f}", f"{row.rps:.2f}", f"{row.failures_s:.2f}"]
|
|
71
|
+
styles = [_CELL] * len(cells)
|
|
72
|
+
if row.failure_count:
|
|
73
|
+
styles[2] = f"{_CELL};{_FAIL}"
|
|
74
|
+
tds = "".join(f'<td style="{style}">{cell}</td>' for style, cell in zip(styles, cells, strict=True))
|
|
75
|
+
return f"<tr>{tds}</tr>"
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
# Spreadsheets run a cell that starts with one of these as a formula.
|
|
79
|
+
_FORMULA_START = ("=", "+", "-", "@", "\t", "\r")
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _cell(value: object) -> object:
|
|
83
|
+
if isinstance(value, str) and value.startswith(_FORMULA_START):
|
|
84
|
+
return "'" + value
|
|
85
|
+
return value
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def write_stats_csv(
|
|
89
|
+
stats: Stats, base_filepath: str, output_dir: str | None = None
|
|
90
|
+
) -> str:
|
|
91
|
+
"""Write saved stats to CSV. Returns the validated base path."""
|
|
92
|
+
from .validation import validate_output_path
|
|
93
|
+
|
|
94
|
+
base = validate_output_path(base_filepath, output_dir)
|
|
95
|
+
path = Path(f"{base}_stats.csv")
|
|
96
|
+
fields = (
|
|
97
|
+
"name", "request_count", "failure_count",
|
|
98
|
+
"p50", "p90", "p95", "p99", "p999",
|
|
99
|
+
)
|
|
100
|
+
rows = list(stats.rows)
|
|
101
|
+
if stats.aggregated is not None:
|
|
102
|
+
rows.append(stats.aggregated)
|
|
103
|
+
with path.open("w", newline="", encoding="utf-8") as handle:
|
|
104
|
+
writer = csv.DictWriter(handle, fieldnames=fields)
|
|
105
|
+
writer.writeheader()
|
|
106
|
+
for row in rows:
|
|
107
|
+
writer.writerow({name: _cell(getattr(row, name)) for name in fields})
|
|
108
|
+
return base
|
LocustLibrary/runner.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"""Runner: builds Locust Environment and runs headless."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import time
|
|
5
|
+
import warnings
|
|
6
|
+
from collections.abc import Callable
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from gevent.pool import Group
|
|
10
|
+
from locust.env import Environment
|
|
11
|
+
from locust.stats import stats_history
|
|
12
|
+
from urllib3.exceptions import InsecureRequestWarning
|
|
13
|
+
|
|
14
|
+
from . import feed
|
|
15
|
+
from .calllog import CallLog
|
|
16
|
+
from .reporting import write_html_report
|
|
17
|
+
from .scenario import RunConfig, Scenario, Stats
|
|
18
|
+
from .shape import StageShape
|
|
19
|
+
from .stats import _stats_from_env
|
|
20
|
+
from .stoprules import RuleWatch
|
|
21
|
+
from .users import Budget, build_class
|
|
22
|
+
|
|
23
|
+
_PATCHED = {"done": False}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _patch_gevent() -> None:
|
|
27
|
+
if _PATCHED["done"]:
|
|
28
|
+
return
|
|
29
|
+
os.environ["LOCUST_SKIP_MONKEY_PATCH"] = "1"
|
|
30
|
+
import gevent.monkey
|
|
31
|
+
|
|
32
|
+
if not getattr(gevent.monkey, "_rf_patched", False):
|
|
33
|
+
gevent.monkey.patch_all()
|
|
34
|
+
setattr(gevent.monkey, "_rf_patched", True) # noqa: B010
|
|
35
|
+
_PATCHED["done"] = True
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def fail_breaker(local: Any, limit: float, warmup: float) -> Callable[[float], str]:
|
|
39
|
+
"""A check that names why the run should stop once failures pass the limit after warm-up."""
|
|
40
|
+
def check(elapsed: float) -> str:
|
|
41
|
+
if not limit or elapsed < warmup:
|
|
42
|
+
return ""
|
|
43
|
+
total = local.stats.total
|
|
44
|
+
if total.num_requests and total.fail_ratio > limit:
|
|
45
|
+
return (f"stopped after {elapsed:.0f} s: failure ratio {total.fail_ratio:.1%} "
|
|
46
|
+
f"is over {limit:.1%}")
|
|
47
|
+
return ""
|
|
48
|
+
|
|
49
|
+
return check
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _wait_until(seconds: float, budget: Budget,
|
|
53
|
+
breaker: Callable[[float], str] | None = None) -> str:
|
|
54
|
+
start = time.monotonic()
|
|
55
|
+
while (now := time.monotonic()) - start < seconds and not budget.done:
|
|
56
|
+
reason = breaker(now - start) if breaker else ""
|
|
57
|
+
if reason:
|
|
58
|
+
return reason
|
|
59
|
+
time.sleep(0.5)
|
|
60
|
+
return ""
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def run_load_test(scenarios: list[Scenario], config: RunConfig) -> Stats:
|
|
64
|
+
"""Run a load test headless and return parsed stats."""
|
|
65
|
+
_patch_gevent()
|
|
66
|
+
budget = Budget(config.iterations)
|
|
67
|
+
shape = StageShape(config.stages) if config.stages else None
|
|
68
|
+
env = Environment(user_classes=[build_class(s, budget) for s in scenarios],
|
|
69
|
+
shape_class=shape)
|
|
70
|
+
calls = _observe(env)
|
|
71
|
+
runner = env.create_local_runner()
|
|
72
|
+
history = _start_history(runner)
|
|
73
|
+
# One warning per call drowns the log; the library logs one line per host instead.
|
|
74
|
+
with warnings.catch_warnings():
|
|
75
|
+
warnings.simplefilter("ignore", InsecureRequestWarning)
|
|
76
|
+
if shape is None:
|
|
77
|
+
runner.start(user_count=config.users, spawn_rate=config.spawn_rate)
|
|
78
|
+
seconds = _parse_time(config.run_time)
|
|
79
|
+
else:
|
|
80
|
+
runner.start_shape()
|
|
81
|
+
seconds = shape.total
|
|
82
|
+
watch = RuleWatch(config.stop_rules, runner)
|
|
83
|
+
stopped = _wait_until(seconds, budget, _first_reason(
|
|
84
|
+
fail_breaker(runner, config.stop_on_fail_ratio, config.fail_ratio_warmup), watch.check))
|
|
85
|
+
runner.quit()
|
|
86
|
+
runner.greenlet.join(timeout=10)
|
|
87
|
+
history.kill(block=False)
|
|
88
|
+
stats = _finish(env, calls)
|
|
89
|
+
stats.stopped, stats.stop_failed = stopped, watch.failed
|
|
90
|
+
return stats
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _first_reason(*checks: Callable[[float], str]) -> Callable[[float], str]:
|
|
94
|
+
return lambda elapsed: next((r for check in checks if (r := check(elapsed))), "")
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _start_history(local: Any) -> Any:
|
|
98
|
+
# Locust fills the report charts from this history; only its own CLI starts the recorder.
|
|
99
|
+
group = Group()
|
|
100
|
+
group.spawn(stats_history, local)
|
|
101
|
+
path = os.environ.get(feed.ENV)
|
|
102
|
+
if path:
|
|
103
|
+
group.spawn(feed.follow, local, path)
|
|
104
|
+
return group
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _observe(env: Any) -> CallLog:
|
|
108
|
+
calls = CallLog()
|
|
109
|
+
env.events.request.add_listener(calls.record)
|
|
110
|
+
return calls
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _finish(env: Any, calls: CallLog) -> Stats:
|
|
114
|
+
stats = _stats_from_env(env)
|
|
115
|
+
stats.calls, stats.left_out = calls.entries, calls.left_out
|
|
116
|
+
stats.report_html = write_html_report(env)
|
|
117
|
+
# Locust only prints this to its own log; an overloaded generator inflates response times.
|
|
118
|
+
stats.cpu_busy = bool(getattr(getattr(env, "runner", None), "cpu_warning_emitted", False))
|
|
119
|
+
path = os.environ.get(feed.ENV)
|
|
120
|
+
if path:
|
|
121
|
+
feed.write_final(path, stats)
|
|
122
|
+
return stats
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _parse_time(value: str) -> float:
|
|
126
|
+
value = value.strip().lower()
|
|
127
|
+
if value.endswith("ms"):
|
|
128
|
+
return float(value[:-2]) / 1000.0
|
|
129
|
+
if value.endswith("s"):
|
|
130
|
+
return float(value[:-1])
|
|
131
|
+
if value.endswith("m"):
|
|
132
|
+
return float(value[:-1]) * 60
|
|
133
|
+
if value.endswith("h"):
|
|
134
|
+
return float(value[:-1]) * 3600
|
|
135
|
+
return float(value)
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""Scenario data shapes for LocustLibrary."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Callable
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from .stoprules import StopRule
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass
|
|
11
|
+
class Task:
|
|
12
|
+
"""A single load-test task."""
|
|
13
|
+
|
|
14
|
+
name: str
|
|
15
|
+
kind: str = "http" # "http" or "rf"
|
|
16
|
+
weight: int = 1
|
|
17
|
+
method: str = "GET"
|
|
18
|
+
path: str = "/"
|
|
19
|
+
headers: dict[str, str] = field(default_factory=dict)
|
|
20
|
+
body: Any = None
|
|
21
|
+
json: Any = None
|
|
22
|
+
expected: tuple[int, ...] = (200,)
|
|
23
|
+
keyword_name: str = ""
|
|
24
|
+
keyword_args: tuple[Any, ...] = field(default_factory=tuple)
|
|
25
|
+
function: Callable[[Any], None] | None = None
|
|
26
|
+
expected_text: str = ""
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass
|
|
30
|
+
class Scenario:
|
|
31
|
+
"""A load-test scenario."""
|
|
32
|
+
|
|
33
|
+
host: str
|
|
34
|
+
name: str = "RFUser"
|
|
35
|
+
weight: int = 1
|
|
36
|
+
fixed_count: int = 0
|
|
37
|
+
verify_tls: bool = True
|
|
38
|
+
proxy: str = ""
|
|
39
|
+
wait_min: float = 1.0
|
|
40
|
+
wait_max: float = 3.0
|
|
41
|
+
pacing: float = 0.0
|
|
42
|
+
on_start: str = ""
|
|
43
|
+
on_stop: str = ""
|
|
44
|
+
tasks: list[Task] = field(default_factory=list)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass(frozen=True, slots=True)
|
|
48
|
+
class Stage:
|
|
49
|
+
"""Reach `users` over `seconds`."""
|
|
50
|
+
|
|
51
|
+
seconds: float
|
|
52
|
+
users: int
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@dataclass
|
|
56
|
+
class RunConfig:
|
|
57
|
+
"""Run configuration. `iterations` 0 means no task limit."""
|
|
58
|
+
|
|
59
|
+
users: int = 10
|
|
60
|
+
spawn_rate: float = 1.0
|
|
61
|
+
run_time: str = "30s"
|
|
62
|
+
stages: list[Stage] = field(default_factory=list)
|
|
63
|
+
iterations: int = 0
|
|
64
|
+
stop_on_fail_ratio: float = 0.0
|
|
65
|
+
fail_ratio_warmup: float = 10.0
|
|
66
|
+
stop_rules: list[StopRule] = field(default_factory=list)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
@dataclass
|
|
70
|
+
class StatsRow:
|
|
71
|
+
"""Per-endpoint stats."""
|
|
72
|
+
|
|
73
|
+
name: str
|
|
74
|
+
request_count: int = 0
|
|
75
|
+
failure_count: int = 0
|
|
76
|
+
median: float = 0.0
|
|
77
|
+
average: float = 0.0
|
|
78
|
+
min: float = 0.0
|
|
79
|
+
max: float = 0.0
|
|
80
|
+
rps: float = 0.0
|
|
81
|
+
failures_s: float = 0.0
|
|
82
|
+
p50: float = 0.0
|
|
83
|
+
p90: float = 0.0
|
|
84
|
+
p95: float = 0.0
|
|
85
|
+
p99: float = 0.0
|
|
86
|
+
p999: float = 0.0
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@dataclass
|
|
90
|
+
class Stats:
|
|
91
|
+
"""Aggregated stats."""
|
|
92
|
+
|
|
93
|
+
rows: list[StatsRow] = field(default_factory=list)
|
|
94
|
+
aggregated: StatsRow | None = None
|
|
95
|
+
errors: list[dict[str, Any]] = field(default_factory=list)
|
|
96
|
+
fail_ratio: float = 0.0
|
|
97
|
+
calls: list[str] = field(default_factory=list, repr=False)
|
|
98
|
+
left_out: int = 0
|
|
99
|
+
report_html: str = field(default="", repr=False)
|
|
100
|
+
stopped: str = ""
|
|
101
|
+
stop_failed: bool = False
|
|
102
|
+
cpu_busy: bool = False
|
LocustLibrary/shape.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""k6 ramp stages as a Locust load shape."""
|
|
2
|
+
|
|
3
|
+
from locust import LoadTestShape
|
|
4
|
+
|
|
5
|
+
from .scenario import Stage
|
|
6
|
+
|
|
7
|
+
__all__ = ["Stage", "StageShape"]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class StageShape(LoadTestShape):
|
|
11
|
+
"""Move linearly to each stage's user count over that stage, as k6 ramping-vus does."""
|
|
12
|
+
|
|
13
|
+
def __init__(self, stages: list[Stage]) -> None:
|
|
14
|
+
super().__init__() # type: ignore[no-untyped-call]
|
|
15
|
+
self._ends: list[tuple[float, int, float]] = []
|
|
16
|
+
elapsed, previous = 0.0, 0
|
|
17
|
+
for stage in stages:
|
|
18
|
+
elapsed += stage.seconds
|
|
19
|
+
change = abs(stage.users - previous)
|
|
20
|
+
rate = change / stage.seconds if change and stage.seconds else 1.0
|
|
21
|
+
self._ends.append((elapsed, stage.users, rate))
|
|
22
|
+
previous = stage.users
|
|
23
|
+
|
|
24
|
+
@property
|
|
25
|
+
def total(self) -> float:
|
|
26
|
+
return self._ends[-1][0] if self._ends else 0.0
|
|
27
|
+
|
|
28
|
+
def tick(self) -> tuple[int, float] | None:
|
|
29
|
+
now = self.get_run_time() # type: ignore[no-untyped-call]
|
|
30
|
+
for end, users, rate in self._ends:
|
|
31
|
+
if now < end:
|
|
32
|
+
return users, rate
|
|
33
|
+
return None
|
LocustLibrary/shared.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""Values one load test step hands to another during a run, like rfswarm's TestDataTable."""
|
|
2
|
+
|
|
3
|
+
from gevent.queue import Empty, Queue
|
|
4
|
+
from robot.api.deco import keyword
|
|
5
|
+
|
|
6
|
+
from .parsing import _parse_seconds
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class SharedKeywords:
|
|
10
|
+
_shared: dict[str, Queue]
|
|
11
|
+
|
|
12
|
+
@keyword
|
|
13
|
+
def put_shared_value(self, name: str, value: object) -> None:
|
|
14
|
+
"""Add `value` under `name` for another user's step to take, such as a created id."""
|
|
15
|
+
self._shared.setdefault(name, Queue()).put(value)
|
|
16
|
+
|
|
17
|
+
@keyword
|
|
18
|
+
def take_shared_value(self, name: str, timeout: str = "10s") -> object:
|
|
19
|
+
"""Take the oldest value under `name`, waiting up to `timeout` for one to be put.
|
|
20
|
+
|
|
21
|
+
Each value goes to one taker only. With none in time the keyword fails, so the task
|
|
22
|
+
counts as a failure. Run Load Test starts with no values.
|
|
23
|
+
"""
|
|
24
|
+
seconds = _parse_seconds(timeout)
|
|
25
|
+
try:
|
|
26
|
+
return self._shared.setdefault(name, Queue()).get(timeout=seconds)
|
|
27
|
+
except Empty:
|
|
28
|
+
raise AssertionError(f"No shared value '{name}' within {seconds:g} s") from None
|