CLI_PREFIX = "CLI execution - "
def label_for_command(cmd: str) -> str:
if "retch" in cmd:
if "--short" in cmd:
return f"{CLI_PREFIX}retch --short"
if "--long" in cmd:
return f"{CLI_PREFIX}retch --long"
return f"{CLI_PREFIX}retch"
if "fastfetch" in cmd:
if "-c none" in cmd:
return f"{CLI_PREFIX}fastfetch -c none"
if "-c all" in cmd:
return f"{CLI_PREFIX}fastfetch -c all"
return f"{CLI_PREFIX}fastfetch"
return f"{CLI_PREFIX}{cmd}"
def labelled_means(data: dict):
out = []
for res in data.get("results", []):
mean = res.get("mean")
if mean is None:
continue
out.append((label_for_command(res.get("command", "")), mean * 1_000_000_000))
return out
EXPECTED_LABELS = (
f"{CLI_PREFIX}retch",
f"{CLI_PREFIX}retch --short",
f"{CLI_PREFIX}retch --long",
f"{CLI_PREFIX}fastfetch",
f"{CLI_PREFIX}fastfetch -c none",
f"{CLI_PREFIX}fastfetch -c all",
)
def _self_test() -> int:
import sys
failures = []
def check(name, cond, detail=""):
if not cond:
failures.append(f"{name}: {detail}")
cases = [
("./target/release/retch", f"{CLI_PREFIX}retch"),
("./target/release/retch --short", f"{CLI_PREFIX}retch --short"),
("./target/release/retch --long", f"{CLI_PREFIX}retch --long"),
(r".\target\release\retch.exe", f"{CLI_PREFIX}retch"),
(r".\target\release\retch.exe --short", f"{CLI_PREFIX}retch --short"),
(r".\target\release\retch.exe --long", f"{CLI_PREFIX}retch --long"),
("fastfetch", f"{CLI_PREFIX}fastfetch"),
("fastfetch -c none", f"{CLI_PREFIX}fastfetch -c none"),
("fastfetch -c all", f"{CLI_PREFIX}fastfetch -c all"),
]
for cmd, want in cases:
got = label_for_command(cmd)
check(f"label {cmd!r}", got == want, f"expected {want!r}, got {got!r}")
check("fastfetch is not retch", "retch" not in "fastfetch",
"the substring test this module relies on has stopped holding")
produced = {label_for_command(c) for c, _ in cases}
missing = [l for l in EXPECTED_LABELS if l not in produced]
check("all six series are reachable", not missing, f"unreachable: {missing}")
pairs = labelled_means({"results": [
{"command": "./target/release/retch", "mean": 0.1234},
{"command": "fastfetch", "mean": 1.5},
]})
check("two results in, two out", len(pairs) == 2, str(pairs))
check("seconds become nanoseconds", pairs[0][1] == 123400000.0, f"got {pairs[0][1]}")
check("names are mapped", pairs[1][0] == f"{CLI_PREFIX}fastfetch", str(pairs))
check("missing mean is skipped",
labelled_means({"results": [{"command": "x"}]}) == [], "a null mean leaked")
check("empty document yields nothing", labelled_means({}) == [], "phantom entry")
if failures:
for f in failures:
print(f" FAIL {f}", file=sys.stderr)
print(f"bench_labels.py self-test FAILED ({len(failures)})", file=sys.stderr)
return 1
print("bench_labels.py self-test passed")
return 0
if __name__ == "__main__":
import sys
sys.exit(_self_test())