fix: count bg lines in k6 so SIGINT-interrupted iterations do not fail the check

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-23 07:55:42 +03:00
co-authored by Claude Opus 5.5
parent aaf3fa3b29
commit a42dd27c24
6 changed files with 157 additions and 18 deletions
+90
View File
@@ -531,6 +531,54 @@ def parse_background(text: str) -> list[Request]:
return out
def summary_count(k6_data: dict[str, Any], name: str) -> int | None:
value = k6_data.get("metrics", {}).get(name, {}).get("values", {})
count = value.get("count")
return None if count is None else int(count)
def line_problems(
requests: list[Request],
k6_data: dict[str, Any],
*,
max_vus: int,
) -> tuple[list[str], dict[str, int]]:
"""Полнота строк `bg,…` по итогу k6 (design, «Уточнения apply», п.
15): строк каждого сценария ровно столько, сколько насчитал счётчик
`bg_lines_<сценарий>` (его увеличивает тот же код, что пишет строку, —
пропавшая строка видна точно), и не меньше итераций. Строк больше
итераций — итерации, прерванные остановкой SIGINT уже после строки:
их не больше VU сценария. Возвращает расхождения и число прерванных по
сценариям."""
problems = []
interrupted: dict[str, int] = {}
for kind, scenario in SCENARIOS.items():
lines = sum(1 for r in requests if r.kind == kind)
counted = summary_count(k6_data, f"bg_lines_{scenario}")
iterations = summary_count(
k6_data,
f"iterations{{scenario:{scenario}}}",
)
if counted is None or iterations is None:
problems.append(
f"{scenario}: no bg_lines or iterations in the k6 summary",
)
continue
if lines != counted:
problems.append(
f"{scenario}: {lines} k6 lines, {counted} counted by "
"bg_lines (lines lost)",
)
extra = lines - iterations
interrupted[scenario] = extra
if not 0 <= extra <= max_vus:
problems.append(
f"{scenario}: {lines} k6 lines, {iterations} iterations "
f"(interrupted must be 0–{max_vus})",
)
return problems, interrupted
def percentile(values: list[float], q: float) -> float | None:
"""Перцентиль ближайшего ранга, как у k6 (p(95))."""
if not values:
@@ -1723,6 +1771,48 @@ def selftest() -> int:
)
parsed = parse_background(console + "\nnoise\nbg,x,1,2,3\n")
expect("bg parsed", len(parsed), len(reqs))
# полнота строк: точное равенство со счётчиком, прерванные — сверху
counts = {
kind: sum(1 for r in parsed if r.kind == kind) for kind in SCENARIOS
}
def summary(extra: int, lost: int) -> dict[str, Any]:
metrics = {}
for kind, scenario in SCENARIOS.items():
metrics[f"bg_lines_{scenario}"] = {
"values": {
"count": counts[kind] + (lost if kind == "d" else 0),
},
}
metrics[f"iterations{{scenario:{scenario}}}"] = {
"values": {
"count": counts[kind] - (extra if kind == "d" else 0),
},
}
return {"metrics": metrics}
expect("lines ok", line_problems(parsed, summary(0, 0), max_vus=5)[0], [])
problems, interrupted = line_problems(parsed, summary(1, 0), max_vus=5)
expect(
"lines interrupted ok",
(problems, interrupted["download"]),
([], 1),
)
expect(
"lines lost",
len(line_problems(parsed, summary(0, 1), max_vus=5)[0]),
1,
)
expect(
"lines too many interrupted",
len(line_problems(parsed, summary(6, 0), max_vus=5)[0]),
1,
)
expect(
"lines no counter",
len(line_problems(parsed, {"metrics": {}}, max_vus=5)[0]),
3,
)
bounds = {"before": (0, 10), "pass": (10, 20), "after": (20, 30)}
rows, ratios_one = windows_of(parsed, bounds)
expect("window rows", len(rows), 12)