testsAndMisc/python_pkg/swap_bench/tests/test_summary.py
Krzysztof kuhy Rudnicki 5e0d8727d6 A/B zswap vs zram: zram stays the default
Add setup_zswap.sh and make setup_zram.sh its one-command revert
(shared lib/swap_switch.sh: systemd-boot/GRUB cmdline, live switch or
exit 3 "reboot to apply"). Add swap_bench.sh + python_pkg/swap_bench, a
seeded memory-pressure benchmark. 3 runs x 2 setups in vmbox and on the
host: zram has lower median PSI full in both (VM 5996 vs 10360 ms, host
7427 vs 8973 ms), so the host keeps booting with zram. Results in
linux_configuration/fixes/DOCS-zswap-vs-zram.md.

Bug-class-check: none — new feature and measurement, not a bug fix

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 12:34:25 +01:00

85 lines
2.6 KiB
Python

"""Tests for the per-setup medians and the agreed winner rule."""
from __future__ import annotations
from typing import TYPE_CHECKING
from python_pkg.swap_bench.stats import COLUMNS
from python_pkg.swap_bench.summary import pick_winner, summarize
if TYPE_CHECKING:
from pathlib import Path
def _tsv(tmp_path: Path, rows: list[tuple[str, str, str, str, str]]) -> Path:
"""Rows of (env, setup, psi_full_ms, majflt, ratio)."""
lines = ["\t".join(COLUMNS)]
for env, setup, full, majflt, ratio in rows:
row = dict.fromkeys(COLUMNS, "0")
row.update(env=env, setup=setup, psi_full_ms=full, majflt=majflt, ratio=ratio)
lines.append("\t".join(row[c] for c in COLUMNS))
path = tmp_path / "r.tsv"
path.write_text("\n".join(lines) + "\n")
return path
def test_lower_psi_full_wins() -> None:
"""PSI full decides when it differs by 10% or more."""
assert (
pick_winner(
{"psi_full_ms": 100, "majflt": 1}, {"psi_full_ms": 50, "majflt": 9}
)[0]
== "zswap"
)
assert (
pick_winner(
{"psi_full_ms": 50, "majflt": 9}, {"psi_full_ms": 100, "majflt": 1}
)[0]
== "zram"
)
def test_majflt_breaks_a_psi_tie() -> None:
"""Under 10% PSI difference, major faults decide."""
winner, reason = pick_winner(
{"psi_full_ms": 100, "majflt": 1000}, {"psi_full_ms": 95, "majflt": 500}
)
assert winner == "zswap"
assert "major faults" in reason
def test_double_tie_keeps_zram() -> None:
"""Under 10% on both, the incumbent zram stays."""
winner, reason = pick_winner(
{"psi_full_ms": 100, "majflt": 100}, {"psi_full_ms": 95, "majflt": 95}
)
assert winner == "zram"
assert "incumbent" in reason
def test_zero_values_tie() -> None:
"""Both zero is a tie, not a division by zero."""
assert (
pick_winner({"psi_full_ms": 0, "majflt": 0}, {"psi_full_ms": 0, "majflt": 0})[0]
== "zram"
)
def test_summarize_medians_and_winner(tmp_path: Path) -> None:
"""Medians per env and setup, a winner line per env with both setups."""
path = _tsv(
tmp_path,
[
("host", "zram", "100", "10", "3.00"),
("host", "zram", "300", "30", "3.20"),
("host", "zram", "200", "20", "na"),
("host", "zswap", "50", "10", "3.50"),
("vm", "zram", "10", "1", "3.00"),
],
)
text = summarize(path)
assert "| host | zram | 3 | 3.10 |" in text
assert "| 200 |" in text
assert "host winner: zswap" in text
assert "vm winner: n/a (needs runs of both setups)" in text