testsAndMisc/python_pkg/swap_bench/tests/test_cli.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

92 lines
2.8 KiB
Python

"""Tests for the swap_bench command line."""
from __future__ import annotations
import runpy
from typing import TYPE_CHECKING
import pytest
from python_pkg.swap_bench import cli
from python_pkg.swap_bench.stats import COLUMNS
if TYPE_CHECKING:
from pathlib import Path
def _out(capsys: pytest.CaptureFixture[str]) -> str:
return capsys.readouterr().out
def test_run_prints_key_value_lines(
capsys: pytest.CaptureFixture[str], tmp_path: Path
) -> None:
"""``run`` prints one key=value line per result field."""
src = tmp_path / "s"
src.write_text("x")
argv = [
"run",
"--size-mib",
"1",
"--seed",
"5",
"--rounds",
"1",
"--snapshot",
f"{src}={tmp_path / 'd'}",
]
assert cli.main(argv) == 0
lines = dict(line.split("=", 1) for line in _out(capsys).splitlines())
assert lines["pages"] == "256"
assert set(lines) == {"pages", "fill_crc", "touch_crc", "majflt", "seconds"}
assert (tmp_path / "d").read_text() == "x"
def test_run_rejects_bad_spec(capsys: pytest.CaptureFixture[str]) -> None:
"""A bad spec exits 2 with the reason on stderr."""
assert cli.main(["run", "--size-mib", "0", "--seed", "5"]) == 2
assert "size_mib" in capsys.readouterr().err
def test_run_rejects_bad_snapshot() -> None:
"""A snapshot without ``=`` is a usage error."""
with pytest.raises(SystemExit):
cli.main(["run", "--size-mib", "1", "--seed", "1", "--snapshot", "nope"])
def test_row_with_and_without_header(
capsys: pytest.CaptureFixture[str], run_dir: Path
) -> None:
"""``row`` prints the TSV row, optionally after the header."""
base = ["row", "--dir", str(run_dir), "--env", "host", "--setup", "zram"]
base += ["--seed", "1", "--size-mib", "1", "--date", "d"]
assert cli.main([*base, "--header"]) == 0
header, row = _out(capsys).splitlines()
assert header.split("\t") == list(COLUMNS)
assert row.startswith("d\thost\tzram\t")
assert cli.main(base) == 0
assert len(_out(capsys).splitlines()) == 1
def test_summary(capsys: pytest.CaptureFixture[str], tmp_path: Path) -> None:
"""``summary`` prints the medians table."""
path = tmp_path / "r.tsv"
path.write_text("\t".join(COLUMNS) + "\n")
assert cli.main(["summary", str(path)]) == 0
assert "| env | setup |" in _out(capsys)
def test_module_entry_point(monkeypatch: pytest.MonkeyPatch) -> None:
"""``python -m python_pkg.swap_bench`` delegates to cli.main."""
calls: list[int] = []
def fake_main() -> int:
calls.append(1)
return 0
monkeypatch.setattr(cli, "main", fake_main)
with pytest.raises(SystemExit) as exc:
runpy.run_module("python_pkg.swap_bench", run_name="__main__")
assert exc.value.code == 0
assert calls == [1]