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

79 lines
2.9 KiB
Python

"""Tests for the seeded swap-pressure workload.
Sizes stay at a few MiB: the anonymous mapping is real memory, but far too
small to push anything into swap, so the tests check determinism and the
output contract, not kernel behaviour.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
from python_pkg.swap_bench.workload import PAGE, Spec, build_pool, run
if TYPE_CHECKING:
from pathlib import Path
def test_pool_is_seeded_and_page_sized() -> None:
"""Same seed gives the same pool; every template is one page."""
a = build_pool(seed=7, pool_pages=64, random_share=0.25)
b = build_pool(seed=7, pool_pages=64, random_share=0.25)
assert a == b
assert len(a) == 64
assert all(len(p) == PAGE for p in a)
assert build_pool(seed=8, pool_pages=64, random_share=0.25) != a
def test_pool_random_share_controls_random_pages() -> None:
"""random_share=0 yields only text pages; 1 yields only random ones."""
text = build_pool(seed=1, pool_pages=16, random_share=0.0)
assert all(p.rstrip(b" ").replace(b" ", b"").isalpha() for p in text)
rand = build_pool(seed=1, pool_pages=16, random_share=1.0)
assert not any(p.replace(b" ", b"").isalpha() for p in rand)
def test_run_is_deterministic() -> None:
"""Two runs with one spec report identical pages and checksums (C7)."""
spec = Spec(size_mib=2, seed=42, pool_pages=32, rounds=2)
first, second = run(spec), run(spec)
assert first.pages == second.pages == 2 * 256
assert first.fill_crc == second.fill_crc
assert first.touch_crc == second.touch_crc
assert first.majflt >= 0
assert first.seconds >= 0
def test_run_seed_changes_checksum() -> None:
"""A different seed is a different workload."""
a = run(Spec(size_mib=1, seed=1, pool_pages=16, rounds=1))
b = run(Spec(size_mib=1, seed=2, pool_pages=16, rounds=1))
assert a.fill_crc != b.fill_crc
def test_run_handles_partial_last_chunk() -> None:
"""A page count that is not a chunk multiple is still filled fully."""
spec = Spec(size_mib=1, seed=3, pool_pages=8, rounds=1, chunk_pages=100)
assert run(spec).pages == 256
def test_spec_rejects_bad_values() -> None:
"""Non-positive sizes and shares outside [0, 1] are refused."""
with pytest.raises(ValueError, match="size_mib"):
Spec(size_mib=0, seed=1)
with pytest.raises(ValueError, match="hot_share"):
Spec(size_mib=1, seed=1, hot_share=1.5)
with pytest.raises(ValueError, match="random_share"):
Spec(size_mib=1, seed=1, random_share=-0.1)
def test_run_copies_snapshots_while_mapped(tmp_path: Path) -> None:
"""Snapshots are copied during the run, creating the destination dir."""
src = tmp_path / "src"
src.write_text("zswap 7\n")
dest = tmp_path / "peak" / "memory.stat"
run(Spec(size_mib=1, seed=1, pool_pages=8, rounds=1), [(src, dest)])
assert dest.read_text() == "zswap 7\n"