Skip to content

pytest-benchmem

The memory companion to pytest-benchmark. It times your code; pytest-benchmem adds a memray peak-memory pass to the same test, in the same run — one node id, one JSON file, both metrics. Plus dims-aware plots and cross-version sweeps.

import pytest


@pytest.mark.parametrize("n", [10_000, 100_000, 1_000_000])
def test_sort(benchmark_memory, n):          # was: benchmark
    benchmark_memory(sorted, list(range(n, 0, -1)))
pytest --benchmark-only --benchmark-json=run.json   # timing + memory, one file

Both metrics land in pytest-benchmark's own table — its timing columns, with peak / allocated / allocs appended on the right. Already have a benchmark suite? Don't swap anything — add --benchmark-memory and every benchmark(...) call records memory too:

pytest --benchmark-only --benchmark-memory

Its reason to exist is the gap nothing else fills cleanly: memray-precision memory benchmarking of your own code, right where you already benchmark. ASV's peakmem is coarse RSS sampling; CodSpeed covers CI timing. pytest-benchmem is the thin memray layer on top of pytest-benchmark. See the README for where it sits relative to those tools.

Install

uv add pytest-benchmem            # the benchmark_memory fixture + memray engine
uv add "pytest-benchmem[plot]"    # + the plot/compare CLI (pandas, plotly, typer)

pytest-benchmark and memray are core deps; memray (the memory pass) is Linux/macOS only — timing works everywhere.

The docs

Status

Early. Extracted from the linopy internal benchmark suite, where it's the local memory-profiling layer. API may move before 1.0.