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/cli.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""rflocust-k6: convert one k6 script into a Robot project for LocustLibrary.
|
|
2
|
+
|
|
3
|
+
It reads options through `k6 inspect`, which runs the script's init code as the caller, so
|
|
4
|
+
convert only trusted scripts. It never runs k6 load.
|
|
5
|
+
Files land in the four-layer house layout; the shared entry point is rebuilt from the
|
|
6
|
+
collections present, and an environment file is written only when it does not exist yet.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import argparse
|
|
10
|
+
import re
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
from .emit import VARIABLES, convert, entry_point, env_variables
|
|
14
|
+
from .options import parse_options, run_inspect
|
|
15
|
+
from .scan import scan_source
|
|
16
|
+
|
|
17
|
+
# The env name becomes a file name and a Robot Settings line.
|
|
18
|
+
_ENV_NAME = re.compile(r"[A-Za-z0-9_.-]+", re.ASCII)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _env_name(text: str) -> str:
|
|
22
|
+
if not _ENV_NAME.fullmatch(text):
|
|
23
|
+
raise argparse.ArgumentTypeError(f"use letters, digits, _ . or -, got {text!r}")
|
|
24
|
+
return text
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _env_pair(text: str) -> tuple[str, str]:
|
|
28
|
+
key, sep, value = text.partition("=")
|
|
29
|
+
if not sep or not key:
|
|
30
|
+
raise argparse.ArgumentTypeError(f"use KEY=VALUE, got {text!r}")
|
|
31
|
+
return key, value
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _parser() -> argparse.ArgumentParser:
|
|
35
|
+
parser = argparse.ArgumentParser(prog="rflocust-k6", description=__doc__)
|
|
36
|
+
parser.add_argument("script", type=Path, help="k6 entry script")
|
|
37
|
+
parser.add_argument("--out", type=Path, default=Path("robot"), help="project folder")
|
|
38
|
+
parser.add_argument("-e", dest="env", type=_env_pair, action="append", default=[],
|
|
39
|
+
help="k6 -e KEY=VALUE passed to k6 inspect")
|
|
40
|
+
parser.add_argument("--max-users", type=int, default=100, help="user ceiling, default 100")
|
|
41
|
+
parser.add_argument("--env-name", type=_env_name, default="dev", help="environment file to create, default dev")
|
|
42
|
+
return parser
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _write_once(path: Path, text: str) -> None:
|
|
46
|
+
if not path.exists():
|
|
47
|
+
path.write_text(text, encoding="utf-8")
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def main(argv: list[str] | None = None) -> int:
|
|
51
|
+
args = _parser().parse_args(argv)
|
|
52
|
+
script: Path = args.script
|
|
53
|
+
opts = parse_options(run_inspect(script, dict(args.env)))
|
|
54
|
+
done = convert(script.name, opts, scan_source(script.read_text(encoding="utf-8")),
|
|
55
|
+
args.max_users)
|
|
56
|
+
out: Path = args.out
|
|
57
|
+
for relative, text in done.files.items():
|
|
58
|
+
(out / relative).parent.mkdir(parents=True, exist_ok=True)
|
|
59
|
+
(out / relative).write_text(text, encoding="utf-8")
|
|
60
|
+
collections = sorted(p.name for p in (out / "collections").glob("*_test_keywords.resource"))
|
|
61
|
+
(out / "resources" / "resources.resource").write_text(entry_point(collections), encoding="utf-8")
|
|
62
|
+
_write_once(out / "resources" / "variables.resource", VARIABLES)
|
|
63
|
+
_write_once(out / "resources" / f"{args.env_name}_variables.resource",
|
|
64
|
+
env_variables(args.env_name, done.host))
|
|
65
|
+
print(f"wrote {len(done.files)} files for {script.name} to {out}")
|
|
66
|
+
return 0
|
LocustLibrary/k6/emit.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"""Turn k6 options and scanned functions into a Robot project, following the four-layer
|
|
2
|
+
house layout: a thin suite in tests/, its steps in collections/, one entry resource, the
|
|
3
|
+
Python tasks under resources/python/, and a report of what needs a hand port.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import re
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from pathlib import PurePath
|
|
9
|
+
from urllib.parse import urlsplit
|
|
10
|
+
|
|
11
|
+
from . import profile, robot_files
|
|
12
|
+
from .options import K6Options
|
|
13
|
+
from .robot_files import VARIABLES, entry_point, env_variables
|
|
14
|
+
from .scan import ExecFunction
|
|
15
|
+
|
|
16
|
+
__all__ = ["VARIABLES", "Conversion", "convert", "entry_point", "env_variables"]
|
|
17
|
+
|
|
18
|
+
_DEFAULT_HOST = "http://127.0.0.1"
|
|
19
|
+
# HOST is written unquoted into a Robot Variables table, where ${{...}} would run as Python.
|
|
20
|
+
# The script name becomes file names, Robot cells and tags.
|
|
21
|
+
_SCRIPT_NAME = re.compile(r"[A-Za-z0-9_.-]+", re.ASCII)
|
|
22
|
+
_PLAIN_HOST = re.compile(r"https?://[A-Za-z0-9.-]+(:\d{1,5})?", re.ASCII)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass(frozen=True, slots=True)
|
|
26
|
+
class Conversion:
|
|
27
|
+
"""Files to write, keyed by their path under the output folder."""
|
|
28
|
+
|
|
29
|
+
files: dict[str, str]
|
|
30
|
+
host: str
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def convert(script: str, opts: K6Options, functions: list[ExecFunction],
|
|
34
|
+
max_users: int = 100) -> Conversion:
|
|
35
|
+
"""Map what k6 and LocustLibrary share; list the rest as hand port or unsupported."""
|
|
36
|
+
if not _SCRIPT_NAME.fullmatch(PurePath(script).name):
|
|
37
|
+
raise ValueError(f"unsupported script name {script!r}")
|
|
38
|
+
report = profile.Report()
|
|
39
|
+
stem = PurePath(script).stem
|
|
40
|
+
by_name = {f.name: f for f in functions}
|
|
41
|
+
load, condition = profile.load(opts.scenarios, max_users, report)
|
|
42
|
+
load.append(f"Export Report{profile.SEP}{stem}")
|
|
43
|
+
checks, lists = profile.checks(opts.thresholds, stem.upper(), report)
|
|
44
|
+
classes = profile.classes(opts.scenarios, by_name, f"{stem}_tasks", report)
|
|
45
|
+
steps = profile.Steps(classes, load, checks, condition, lists)
|
|
46
|
+
host = _host(functions, report)
|
|
47
|
+
names = list(dict.fromkeys(s.exec_name for s in opts.scenarios))
|
|
48
|
+
files = {
|
|
49
|
+
f"tests/{stem}.robot": robot_files.suite(script, steps.condition),
|
|
50
|
+
f"collections/{stem}_test_keywords.resource": robot_files.keywords(script, opts.insecure, steps),
|
|
51
|
+
f"resources/python/{stem}_tasks.py": robot_files.tasks_module(
|
|
52
|
+
script, [(n, by_name.get(n)) for n in names]),
|
|
53
|
+
f"reports/{stem}.k6-report.md": report.markdown(script),
|
|
54
|
+
}
|
|
55
|
+
return Conversion(files, host)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _host(functions: list[ExecFunction], report: profile.Report) -> str:
|
|
59
|
+
for call in (c for f in functions for c in f.calls):
|
|
60
|
+
parts = urlsplit(call.url)
|
|
61
|
+
if parts.scheme and parts.netloc:
|
|
62
|
+
host = f"{parts.scheme}://{parts.netloc}"
|
|
63
|
+
if _PLAIN_HOST.fullmatch(host):
|
|
64
|
+
return host
|
|
65
|
+
report.add("host", "set by hand",
|
|
66
|
+
f"script host is not a plain host[:port]; HOST defaults to {_DEFAULT_HOST}")
|
|
67
|
+
return _DEFAULT_HOST
|
|
68
|
+
report.add("host", "set by hand", f"no absolute URL found; HOST defaults to {_DEFAULT_HOST}")
|
|
69
|
+
return _DEFAULT_HOST
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"""Read a k6 script's resolved options through `k6 inspect`, never `k6 run`."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import re
|
|
6
|
+
import shutil
|
|
7
|
+
import subprocess
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from ..scenario import Stage
|
|
13
|
+
|
|
14
|
+
_DURATION_PART = re.compile(r"(\d+(?:\.\d+)?)(ms|h|m|s)")
|
|
15
|
+
_UNIT_SECONDS = {"ms": 0.001, "s": 1.0, "m": 60.0, "h": 3600.0}
|
|
16
|
+
# Names and tags end up in generated Robot and Python source, so only plain words pass.
|
|
17
|
+
_METRIC = re.compile(r"^(\w+)(?:\{endpoint:([\w.:/-]*(?: [\w.:/-]+)*)\})?$", re.ASCII)
|
|
18
|
+
_EXEC_NAME = re.compile(r"[A-Za-z_]\w*", re.ASCII)
|
|
19
|
+
_SCENARIO_NAME = re.compile(r"[\w-]+", re.ASCII)
|
|
20
|
+
# init code can read __ENV, so k6 gets only what it needs to start.
|
|
21
|
+
_INSPECT_ENV = ("PATH", "SYSTEMROOT", "HOME", "USERPROFILE", "TEMP", "TMP", "TMPDIR")
|
|
22
|
+
_EXPR = re.compile(r"^(p\((\d+(?:\.\d+)?)\)|avg|med|min|max|rate)\s*<=?\s*(\d+(?:\.\d+)?)$")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass(frozen=True, slots=True)
|
|
26
|
+
class K6Scenario:
|
|
27
|
+
"""One k6 scenario as `k6 inspect` resolves it."""
|
|
28
|
+
|
|
29
|
+
name: str
|
|
30
|
+
executor: str
|
|
31
|
+
exec_name: str = "default"
|
|
32
|
+
stages: tuple[Stage, ...] = ()
|
|
33
|
+
vus: int = 0
|
|
34
|
+
iterations: int = 0
|
|
35
|
+
duration_s: float = 0.0
|
|
36
|
+
rate: float = 0.0
|
|
37
|
+
max_vus: int = 0
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass(frozen=True, slots=True)
|
|
41
|
+
class Threshold:
|
|
42
|
+
"""`stat` is p95, p999, avg, med, min, max or rate; `endpoint` "" means every request."""
|
|
43
|
+
|
|
44
|
+
metric: str
|
|
45
|
+
endpoint: str
|
|
46
|
+
stat: str
|
|
47
|
+
limit: float
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@dataclass(frozen=True, slots=True)
|
|
51
|
+
class K6Options:
|
|
52
|
+
scenarios: tuple[K6Scenario, ...]
|
|
53
|
+
thresholds: tuple[Threshold, ...]
|
|
54
|
+
insecure: bool
|
|
55
|
+
total_s: float
|
|
56
|
+
max_vus: int
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def go_seconds(text: str) -> float:
|
|
60
|
+
"""Seconds in a Go duration as k6 prints it, such as 2m0s or 500ms."""
|
|
61
|
+
parts = _DURATION_PART.findall(text)
|
|
62
|
+
if not parts or "".join(n + u for n, u in parts) != text:
|
|
63
|
+
raise ValueError(f"bad duration {text!r}")
|
|
64
|
+
return sum(float(n) * _UNIT_SECONDS[u] for n, u in parts)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _threshold(metric: str, expr: str) -> Threshold:
|
|
68
|
+
name, expr_match = _METRIC.match(metric), _EXPR.match(expr.strip())
|
|
69
|
+
if name is None or expr_match is None:
|
|
70
|
+
raise ValueError(f"unsupported threshold {metric}: {expr}")
|
|
71
|
+
percentile = expr_match.group(2)
|
|
72
|
+
stat = "p" + percentile.replace(".", "") if percentile else expr_match.group(1)
|
|
73
|
+
return Threshold(name.group(1), name.group(2) or "", stat, float(expr_match.group(3)))
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def parse_thresholds(raw: dict[str, list[str]] | None) -> tuple[Threshold, ...]:
|
|
77
|
+
return tuple(_threshold(m, e) for m, exprs in sorted((raw or {}).items()) for e in exprs)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _rate(raw: dict[str, Any]) -> float:
|
|
81
|
+
if not raw.get("rate"):
|
|
82
|
+
return 0.0
|
|
83
|
+
return float(raw["rate"]) / go_seconds(raw.get("timeUnit") or "1s")
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _scenario(name: str, raw: dict[str, Any]) -> K6Scenario:
|
|
87
|
+
exec_name = raw.get("exec") or "default"
|
|
88
|
+
if not _SCENARIO_NAME.fullmatch(name):
|
|
89
|
+
raise ValueError(f"unsupported scenario name {name!r}")
|
|
90
|
+
if not _EXEC_NAME.fullmatch(exec_name):
|
|
91
|
+
raise ValueError(f"unsupported exec name {exec_name!r} in scenario {name}")
|
|
92
|
+
stages = tuple(Stage(go_seconds(s["duration"]), int(s["target"]))
|
|
93
|
+
for s in raw.get("stages") or [])
|
|
94
|
+
return K6Scenario(
|
|
95
|
+
name=name,
|
|
96
|
+
executor=raw["executor"],
|
|
97
|
+
exec_name=exec_name,
|
|
98
|
+
stages=stages,
|
|
99
|
+
vus=int(raw.get("vus") or 0),
|
|
100
|
+
iterations=int(raw.get("iterations") or 0),
|
|
101
|
+
duration_s=go_seconds(raw["duration"]) if raw.get("duration") else 0.0,
|
|
102
|
+
rate=_rate(raw),
|
|
103
|
+
max_vus=int(raw.get("maxVUs") or 0),
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def parse_options(data: dict[str, Any]) -> K6Options:
|
|
108
|
+
scenarios = data.get("scenarios") or {}
|
|
109
|
+
return K6Options(
|
|
110
|
+
scenarios=tuple(_scenario(n, scenarios[n]) for n in sorted(scenarios)),
|
|
111
|
+
thresholds=parse_thresholds(data.get("thresholds")),
|
|
112
|
+
insecure=bool(data.get("insecureSkipTLSVerify")),
|
|
113
|
+
total_s=go_seconds(data["totalDuration"]) if data.get("totalDuration") else 0.0,
|
|
114
|
+
max_vus=int(data.get("maxVUs") or 0),
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def run_inspect(script: Path, env: dict[str, str], timeout: int = 60) -> dict[str, Any]:
|
|
119
|
+
"""k6 runs the script's init code here as the caller; only convert trusted scripts."""
|
|
120
|
+
k6 = shutil.which("k6")
|
|
121
|
+
if k6 is None:
|
|
122
|
+
raise FileNotFoundError("k6 is not on PATH; k6 inspect is needed to read options")
|
|
123
|
+
env_args = [arg for key, value in sorted(env.items()) for arg in ("-e", f"{key}={value}")]
|
|
124
|
+
done = subprocess.run([k6, "inspect", "--execution-requirements", *env_args, str(script)],
|
|
125
|
+
capture_output=True, text=True, timeout=timeout, check=True,
|
|
126
|
+
env={k: os.environ[k] for k in _INSPECT_ENV if k in os.environ})
|
|
127
|
+
result: dict[str, Any] = json.loads(done.stdout)
|
|
128
|
+
return result
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""Map k6 scenarios and thresholds onto LocustLibrary keyword lines, noting each
|
|
2
|
+
choice in the conversion report.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
|
|
7
|
+
from .options import K6Scenario, Threshold
|
|
8
|
+
from .scan import ExecFunction
|
|
9
|
+
|
|
10
|
+
SEP = " "
|
|
11
|
+
_PERCENTILES = {"p50", "p90", "p95", "p99", "p999"}
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass
|
|
15
|
+
class Report:
|
|
16
|
+
rows: list[tuple[str, str, str]] = field(default_factory=list)
|
|
17
|
+
|
|
18
|
+
def add(self, item: str, status: str, note: str) -> None:
|
|
19
|
+
self.rows.append((item, status, note.replace("|", "/")))
|
|
20
|
+
|
|
21
|
+
def markdown(self, script: str) -> str:
|
|
22
|
+
lines = [f"# k6 conversion report: {script}", "", "| Item | Status | Note |", "|---|---|---|"]
|
|
23
|
+
return "\n".join([*lines, *(f"| {a} | {b} | {c} |" for a, b, c in self.rows)]) + "\n"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True, slots=True)
|
|
27
|
+
class Steps:
|
|
28
|
+
classes: list[str]
|
|
29
|
+
load: list[str]
|
|
30
|
+
checks: list[str]
|
|
31
|
+
condition: str
|
|
32
|
+
lists: dict[str, list[str]]
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _peak(scenario: K6Scenario) -> int:
|
|
36
|
+
if scenario.stages:
|
|
37
|
+
return max(stage.users for stage in scenario.stages)
|
|
38
|
+
return scenario.vus or scenario.max_vus
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def classes(scenarios: tuple[K6Scenario, ...], by_name: dict[str, ExecFunction],
|
|
42
|
+
module: str, report: Report) -> list[str]:
|
|
43
|
+
lines: list[str] = []
|
|
44
|
+
for s in scenarios:
|
|
45
|
+
fn = by_name.get(s.exec_name)
|
|
46
|
+
think = f"{fn.think_s if fn else 0:g}s"
|
|
47
|
+
lines += [f"Add User Class{SEP}{s.name}{SEP}weight={max(1, _peak(s))}",
|
|
48
|
+
f"Set Wait Time{SEP}{think}{SEP}{think}",
|
|
49
|
+
f"Add Python Task{SEP}{s.exec_name}{SEP}{module}.{s.exec_name}"]
|
|
50
|
+
if fn is None:
|
|
51
|
+
report.add(f"scenario {s.name}", "hand port", f"exec function {s.exec_name} not found")
|
|
52
|
+
elif fn.simple:
|
|
53
|
+
report.add(f"scenario {s.name}", "mapped", f"{len(fn.calls)} HTTP calls")
|
|
54
|
+
else:
|
|
55
|
+
report.add(f"scenario {s.name}", "hand port", fn.reason)
|
|
56
|
+
return lines
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def load(scenarios: tuple[K6Scenario, ...], max_users: int,
|
|
60
|
+
report: Report) -> tuple[list[str], str]:
|
|
61
|
+
kinds = {s.executor for s in scenarios}
|
|
62
|
+
if kinds == {"ramping-vus"}:
|
|
63
|
+
return _staged(scenarios, max_users, report)
|
|
64
|
+
users = sum(_peak(s) for s in scenarios)
|
|
65
|
+
seconds = max((s.duration_s for s in scenarios), default=0.0) or 600.0
|
|
66
|
+
iterations = sum(s.iterations for s in scenarios) if kinds <= {"shared-iterations"} else 0
|
|
67
|
+
if not kinds <= {"shared-iterations", "constant-vus"}:
|
|
68
|
+
report.add("executors", "approximate",
|
|
69
|
+
f"{', '.join(sorted(kinds))} run as {users} looping users; Locust has no arrival rate")
|
|
70
|
+
if users > max_users:
|
|
71
|
+
report.add("users", "clamped", f"{users} users cut to max_users={max_users}")
|
|
72
|
+
users = max_users
|
|
73
|
+
args = [f"users={users}", f"spawn_rate={users}", f"run_time={seconds:g}s"]
|
|
74
|
+
args += [f"iterations={iterations}"] if iterations else []
|
|
75
|
+
condition = (f"{_count(iterations, 'Iteration')} By {_count(users, 'User')}" if iterations
|
|
76
|
+
else f"{_count(users, 'User')} For {seconds:g}s")
|
|
77
|
+
return [SEP.join(["Run Load Test", *args, f"max_users={max_users}"])], condition
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _count(number: int, noun: str) -> str:
|
|
81
|
+
return f"{number} {noun}{'' if number == 1 else 's'}"
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _staged(scenarios: tuple[K6Scenario, ...], max_users: int,
|
|
85
|
+
report: Report) -> tuple[list[str], str]:
|
|
86
|
+
first = scenarios[0].stages
|
|
87
|
+
timings = [st.seconds for st in first]
|
|
88
|
+
if any([st.seconds for st in s.stages] != timings for s in scenarios):
|
|
89
|
+
report.add("stages", "approximate", "stage timings differ; the first scenario's are used")
|
|
90
|
+
totals = [sum(s.stages[i].users for s in scenarios if i < len(s.stages))
|
|
91
|
+
for i in range(len(first))]
|
|
92
|
+
peak = max(totals, default=0)
|
|
93
|
+
scale = min(1.0, max_users / peak) if peak else 1.0
|
|
94
|
+
if scale < 1.0:
|
|
95
|
+
report.add("users", "clamped", f"{peak} users cut to max_users={max_users}")
|
|
96
|
+
scaled = [round(t * scale) for t in totals]
|
|
97
|
+
stages = SEP.join(f"{st.seconds:g}s:{t}" for st, t in zip(first, scaled, strict=True))
|
|
98
|
+
lines = [f"Set Load Stages{SEP}{stages}", f"Run Load Test{SEP}max_users={max_users}"]
|
|
99
|
+
return lines, f"Ramp To {_count(max(scaled, default=0), 'User')}"
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def checks(thresholds: tuple[Threshold, ...], prefix: str,
|
|
103
|
+
report: Report) -> tuple[list[str], dict[str, list[str]]]:
|
|
104
|
+
groups: dict[tuple[str, ...], list[str]] = {}
|
|
105
|
+
for t in thresholds:
|
|
106
|
+
item = f"threshold {t.metric} {t.stat}"
|
|
107
|
+
if t.metric == "http_req_duration" and t.stat in _PERCENTILES:
|
|
108
|
+
key: tuple[str, ...] = ("Should Meet Response Time", "${endpoint}", t.stat, f"{t.limit:g}ms")
|
|
109
|
+
elif t.metric == "http_req_failed" and t.stat == "rate":
|
|
110
|
+
key = ("Should Meet Error Rate", f"{t.limit * 100:g}%", "endpoint=${endpoint}")
|
|
111
|
+
else:
|
|
112
|
+
report.add(item, "unsupported", "no LocustLibrary check for this metric")
|
|
113
|
+
continue
|
|
114
|
+
report.add(item, "mapped", t.endpoint or "all requests")
|
|
115
|
+
groups.setdefault(key, []).append(t.endpoint or "aggregated")
|
|
116
|
+
if not groups:
|
|
117
|
+
report.add("checks", "approximate", "k6 had no thresholds; the suite checks no request failed")
|
|
118
|
+
return ["Should Have No Failures"], {}
|
|
119
|
+
lines: list[str] = []
|
|
120
|
+
lists: dict[str, list[str]] = {}
|
|
121
|
+
for number, (key, endpoints) in enumerate(groups.items(), 1):
|
|
122
|
+
call = SEP.join(key)
|
|
123
|
+
if len(endpoints) == 1:
|
|
124
|
+
single = call.replace("${endpoint}", endpoints[0])
|
|
125
|
+
lines.append(single.replace(f"{SEP}endpoint=aggregated", ""))
|
|
126
|
+
continue
|
|
127
|
+
name = f"{prefix}_CHECK_{number}_ENDPOINTS"
|
|
128
|
+
lists[name] = endpoints
|
|
129
|
+
lines += [SEP.join(["FOR", "${endpoint}", "IN", f"@{{{name}}}"]), SEP + call, "END"]
|
|
130
|
+
return lines, lists
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"""Write the Robot suite, keyword resource, entry resources and Python tasks module
|
|
2
|
+
of a converted k6 project.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from pathlib import PurePath
|
|
6
|
+
|
|
7
|
+
from robot.utils import escape
|
|
8
|
+
|
|
9
|
+
from .profile import SEP, Steps
|
|
10
|
+
from .scan import ExecFunction, HttpCall
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _title(script: str) -> str:
|
|
14
|
+
return " ".join(PurePath(script).stem.replace("_", " ").split()).title()
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _variables(lists: dict[str, list[str]]) -> list[str]:
|
|
18
|
+
if not lists:
|
|
19
|
+
return []
|
|
20
|
+
lines = ["*** Variables ***"]
|
|
21
|
+
for name, items in lists.items():
|
|
22
|
+
lines.append(f"@{{{name}}}" + SEP + SEP.join(items[:2]))
|
|
23
|
+
lines += ["..." + SEP + SEP.join(items[i:i + 3]) for i in range(2, len(items), 3)]
|
|
24
|
+
return [*lines, "", ""]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def suite(script: str, condition: str) -> str:
|
|
28
|
+
stem, title = PurePath(script).stem, _title(script)
|
|
29
|
+
return "\n".join([
|
|
30
|
+
"*** Settings ***",
|
|
31
|
+
f"Documentation Load test converted from {script} by the LocustLibrary k6 converter.",
|
|
32
|
+
f"... Every step lives in collections/{stem}_test_keywords.resource;",
|
|
33
|
+
f"... reports/{stem}.k6-report.md lists what needs a hand port.",
|
|
34
|
+
"Resource ../resources/resources.resource", "",
|
|
35
|
+
f"Test Tags {stem} load slow", "", "",
|
|
36
|
+
"*** Test Cases ***",
|
|
37
|
+
f"{title} Load - {condition} - Thresholds Met",
|
|
38
|
+
f"{SEP}[Documentation] Runs the {script} profile and passes when every converted check holds.",
|
|
39
|
+
f"{SEP}Test {title} Load",
|
|
40
|
+
]) + "\n"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _keyword(name: str, doc: str, lines: list[str]) -> list[str]:
|
|
44
|
+
return [name, f"{SEP}[Documentation] {doc}", *(SEP + line for line in lines), ""]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def keywords(script: str, insecure: bool, steps: Steps) -> str:
|
|
48
|
+
title = _title(script)
|
|
49
|
+
create = SEP.join(["Create Load Test", "${HOST}", f"verify_tls=${{{not insecure}}}",
|
|
50
|
+
"proxy=${PROXY}"])
|
|
51
|
+
head = ["*** Settings ***",
|
|
52
|
+
f"Documentation Scenario keywords for the {script} load test.",
|
|
53
|
+
f"... The suite calls only Test {title} Load.",
|
|
54
|
+
"Resource ../resources/resources.resource", "", "",
|
|
55
|
+
*_variables(steps.lists),
|
|
56
|
+
"*** Keywords ***"]
|
|
57
|
+
body = [
|
|
58
|
+
*_keyword(f"Test {title} Load", "Builds the users, runs the load, then checks it.",
|
|
59
|
+
[f"Build {title} Users", f"Run {title} Load", f"Check {title} Load"]),
|
|
60
|
+
*_keyword(f"Build {title} Users", "One user class per k6 scenario, weighted by its peak users.",
|
|
61
|
+
[create, *steps.classes]),
|
|
62
|
+
*_keyword(f"Run {title} Load", "Runs the converted k6 load profile headless.", steps.load),
|
|
63
|
+
*_keyword(f"Check {title} Load", "The converted k6 thresholds.", steps.checks),
|
|
64
|
+
]
|
|
65
|
+
return "\n".join([*head, *body]).rstrip() + "\n"
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def entry_point(collections: list[str]) -> str:
|
|
69
|
+
"""resources/resources.resource: the one file every suite and collection imports."""
|
|
70
|
+
lines = ["*** Settings ***",
|
|
71
|
+
"Documentation Single entry point. Every suite and collection imports this file only.",
|
|
72
|
+
"Library LocustLibrary python_path=${CURDIR}/python",
|
|
73
|
+
"Resource variables.resource",
|
|
74
|
+
"Resource ${ENV}_variables.resource",
|
|
75
|
+
*(f"Resource ../collections/{c}" for c in collections)]
|
|
76
|
+
return "\n".join(lines) + "\n"
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
VARIABLES = """*** Settings ***
|
|
80
|
+
Documentation Variables shared by every environment. Pick the environment with -v ENV:<name>.
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
*** Variables ***
|
|
84
|
+
${ENV} dev
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def env_variables(env: str, host: str) -> str:
|
|
89
|
+
"""resources/<env>_variables.resource with the host the scripts call."""
|
|
90
|
+
return "\n".join([
|
|
91
|
+
"*** Settings ***",
|
|
92
|
+
f"Documentation Values for the {escape(env)} environment.", "", "",
|
|
93
|
+
"*** Variables ***",
|
|
94
|
+
f"${{HOST}} {escape(host)}",
|
|
95
|
+
"${PROXY} ${EMPTY}",
|
|
96
|
+
]) + "\n"
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def tasks_module(script: str, wanted: list[tuple[str, ExecFunction | None]]) -> str:
|
|
100
|
+
parts = [repr(f"Tasks converted from {script}. Each function gets the Locust user."),
|
|
101
|
+
"", "from typing import Any"]
|
|
102
|
+
for name, fn in wanted:
|
|
103
|
+
parts += ["", "", f"def {name}(user: Any) -> None:", *_function_body(script, name, fn)]
|
|
104
|
+
return "\n".join(parts) + "\n"
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _function_body(script: str, name: str, fn: ExecFunction | None) -> list[str]:
|
|
108
|
+
if fn is None or not fn.simple:
|
|
109
|
+
why = fn.reason if fn else f"exec function {name} not found"
|
|
110
|
+
line = fn.line if fn else 0
|
|
111
|
+
# The reason quotes k6 source, so repr keeps it a plain string literal.
|
|
112
|
+
return [f" {f'Hand port from {script} line {line}: {why}.'!r}",
|
|
113
|
+
f" raise NotImplementedError({f'hand port: {script} line {line}'!r})"]
|
|
114
|
+
return [_call_line(call) for call in fn.calls] or [" del user"]
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _call_line(call: HttpCall) -> str:
|
|
118
|
+
args = [repr(call.url)]
|
|
119
|
+
if isinstance(call.body, str):
|
|
120
|
+
args.append(f"data={call.body!r}")
|
|
121
|
+
elif call.body is not None:
|
|
122
|
+
args.append(f"json={call.body!r}")
|
|
123
|
+
if call.headers:
|
|
124
|
+
args.append(f"headers={call.headers!r}")
|
|
125
|
+
args.append(f"name={(call.endpoint or call.url)!r}")
|
|
126
|
+
verb = "delete" if call.method == "DELETE" else call.method.lower()
|
|
127
|
+
return f" user.client.{verb}({', '.join(args)})"
|