Skip to content

feu.benchmark

The trend of the pytest-benchmark results: load the runs, find the regressions, make a report and prune the old results. See the benchmark-trend command and the usage guide.

feu.benchmark

Contain functions to follow the trend of the benchmarks.

load_runs reads the results saved by pytest-benchmark as Run values, find_regressions compares the latest run of each machine with the median of its previous runs, make_report renders a Markdown report with GitHub Actions annotations, and prune_runs deletes the old results. This package only uses the standard library. The feu benchmark-trend command is built on these functions.

feu.benchmark.Regression dataclass

Store a benchmark that is slower than its recent median.

Parameters:

Name Type Description Default
machine str

The machine of the runs, see Run.

required
label str

The label of the benchmark.

required
latest float

The time in seconds of the latest run.

required
baseline float

The median time in seconds of the previous runs.

required
change float

The slowdown in percent.

required
Example
>>> from feu.benchmark import Regression
>>> Regression("", "sort", 0.002, 0.001, 100.0).change
100.0

feu.benchmark.Run dataclass

Store the times of the benchmarks of one run.

Parameters:

Name Type Description Default
date datetime

The date of the run, with a time zone.

required
machine str

The directory of the result below the storage (pytest-benchmark saves the runs of one machine in one directory), or an empty string if there is none.

required
times Mapping[str, float]

The time in seconds of each benchmark, by label. The times are finite and greater than zero. The mapping is read-only.

required

Raises:

Type Description
ValueError

If a time is not a finite number greater than zero.

Example
>>> from datetime import UTC, datetime
>>> from feu.benchmark import Run
>>> run = Run(datetime(2026, 1, 1, tzinfo=UTC), "linux", {"sort": 0.001})
>>> run.times["sort"]
0.001

feu.benchmark.TrendReport dataclass

Store the report of the trend of the benchmarks.

Parameters:

Name Type Description Default
markdown str

The Markdown report. It is never empty.

required
annotations tuple[str, ...]

The GitHub Actions ::warning:: commands of the regressions, already escaped.

required
regressions tuple[Regression, ...]

The regressions of every machine.

required
Example
>>> from feu.benchmark import TrendReport
>>> TrendReport("## Benchmarks", (), ()).annotations
()

feu.benchmark.find_regressions

find_regressions(
    runs: Sequence[Run],
    *,
    baseline_runs: int = 7,
    threshold: float = 50.0,
) -> list[Regression]

Find the benchmarks slower than their recent median.

The latest run is compared with the median of the previous runs (at most baseline_runs), which is more stable than the comparison with one run because the runners are noisy. The runs of different machines are never compared with each other: each machine has its own latest run and its own baseline.

Parameters:

Name Type Description Default
runs Sequence[Run]

The runs returned by load_runs. They must be in chronological order: the last run of a machine is its latest run.

required
baseline_runs int

The maximum number of previous runs used for the median. It must be at least 1.

7
threshold float

The slowdown in percent above which a benchmark is a regression. It must be greater than 0.

50.0

Returns:

Type Description
list[Regression]

The regressions sorted by decreasing change, then by label and machine. It is empty if no machine has two runs.

Raises:

Type Description
ValueError

If baseline_runs is less than 1 or threshold is not greater than 0.

Example
>>> from datetime import UTC, datetime
>>> from feu.benchmark import Run, find_regressions
>>> date = datetime(2026, 1, 1, tzinfo=UTC)
>>> runs = [Run(date, "", {"sort": 0.001}), Run(date, "", {"sort": 0.003})]
>>> find_regressions(runs)
[Regression(machine='', label='sort', latest=0.003, baseline=0.001, change=200.0)]

feu.benchmark.load_runs

load_runs(
    storage: Path,
    *,
    days: int = 90,
    kind: str = "benchmark",
    stat: str = "median",
    label: str = "name",
) -> list[Run]

Load the results of the runs in the window.

Only the files that pytest-benchmark names (NNNN_*.json) are read, the other files are ignored. A run without time is dropped.

Parameters:

Name Type Description Default
storage Path

The pytest-benchmark storage directory.

required
days int

The size of the window in days. It must be at least 1.

90
kind str

The kind of results, "benchmark" or "import". The benchmark kind reads the statistic from the stats of pytest-benchmark; the import kind reads import_seconds_<stat> of the extra_info, and ignores the benchmarks that do not have it.

'benchmark'
stat str

The statistic of the times: "min", "mean", "median" or "max".

'median'
label str

The rule of the label of a benchmark. "name" is the name without the test_ prefix and the _benchmark suffix (test_sort_benchmark[10] is sort[10]), or the parameter for the import kind. "fullname" is the full name of the benchmark.

'name'

Returns:

Type Description
list[Run]

The runs sorted by date then by machine.

Raises:

Type Description
ValueError

If an argument is invalid, if a result file is invalid (the message has its path), or if two benchmarks of a run have the same label.

Example
>>> import json
>>> import tempfile
>>> from pathlib import Path
>>> from feu.benchmark import load_runs
>>> with tempfile.TemporaryDirectory() as tmp:
...     result = {
...         "datetime": "2999-01-01T00:00:00+00:00",
...         "benchmarks": [{"name": "test_sort_benchmark", "stats": {"median": 0.001}}],
...     }
...     _ = Path(tmp, "0001_run.json").write_text(json.dumps(result))
...     runs = load_runs(Path(tmp))
...
>>> runs[0].times["sort"]
0.001

feu.benchmark.make_report

make_report(
    runs: Sequence[Run],
    *,
    kind: str = "benchmark",
    stat: str = "median",
    unit: str | None = None,
    days: int = 90,
    baseline_runs: int = 7,
    threshold: float = 50.0,
) -> TrendReport

Make the report of the trend of the benchmarks.

The report has a table of the trend of each benchmark and, if there are regressions, a regression block. If the runs come from several machines, the report has one section per machine, because the runs of different machines are never compared. In the table, the change is between the first and the latest run of the benchmark, whereas the change of a regression is relative to the median of the previous runs. The rows are sorted by latest time then by label.

Parameters:

Name Type Description Default
runs Sequence[Run]

The runs returned by load_runs, in chronological order. There must be at least one.

required
kind str

The kind of results, "benchmark" or "import".

'benchmark'
stat str

The statistic of the times, it is shown in the title.

'median'
unit str | None

The unit of the times: "s", "ms", "us" or "ns". If None, "us" is used for the benchmark kind and "ms" for the import kind. Times are shown with three significant digits.

None
days int

The size of the window in days, shown in the title.

90
baseline_runs int

The maximum number of previous runs used to detect the regressions, see find_regressions.

7
threshold float

The slowdown in percent above which a benchmark is a regression.

50.0

Returns:

Type Description
TrendReport

The report.

Raises:

Type Description
ValueError

If there is no run, if kind, stat or unit is unknown, or if baseline_runs or threshold is invalid.

Example
>>> from datetime import UTC, datetime
>>> from feu.benchmark import Run, make_report
>>> date = datetime(2026, 1, 1, tzinfo=UTC)
>>> runs = [Run(date, "", {"sort": 0.001}), Run(date, "", {"sort": 0.003})]
>>> report = make_report(runs)
>>> report.markdown.splitlines()[0]
'## Benchmarks (median) over the last 90 days (2 runs)'
>>> report.regressions[0].change
200.0
>>> report.annotations[0].startswith("::warning title=Regression::sort is 200.0%25 slower")
True

feu.benchmark.prune_runs

prune_runs(storage: Path, *, days: int = 90) -> list[Path]

Delete the results older than the window.

Parameters:

Name Type Description Default
storage Path

The pytest-benchmark storage directory.

required
days int

The size of the window in days. It must be at least 1.

90

Returns:

Type Description
list[Path]

The paths of the deleted files, sorted.

Raises:

Type Description
ValueError

If days is invalid or if a result file has no valid date. The files that were sorted before it may be deleted.

Example
>>> import json
>>> import tempfile
>>> from pathlib import Path
>>> from feu.benchmark import prune_runs
>>> with tempfile.TemporaryDirectory() as tmp:
...     result = {"datetime": "2000-01-01T00:00:00+00:00", "benchmarks": []}
...     _ = Path(tmp, "0001_run.json").write_text(json.dumps(result))
...     [path.name for path in prune_runs(Path(tmp))]
...
['0001_run.json']

feu.benchmark.runs

Contain functions to load and prune the pytest-benchmark results.

This module is the only one of feu.benchmark that reads and deletes files. The other modules are pure functions of the values it returns.

feu.benchmark.runs.Run dataclass

Store the times of the benchmarks of one run.

Parameters:

Name Type Description Default
date datetime

The date of the run, with a time zone.

required
machine str

The directory of the result below the storage (pytest-benchmark saves the runs of one machine in one directory), or an empty string if there is none.

required
times Mapping[str, float]

The time in seconds of each benchmark, by label. The times are finite and greater than zero. The mapping is read-only.

required

Raises:

Type Description
ValueError

If a time is not a finite number greater than zero.

Example
>>> from datetime import UTC, datetime
>>> from feu.benchmark import Run
>>> run = Run(datetime(2026, 1, 1, tzinfo=UTC), "linux", {"sort": 0.001})
>>> run.times["sort"]
0.001

feu.benchmark.runs.load_runs

load_runs(
    storage: Path,
    *,
    days: int = 90,
    kind: str = "benchmark",
    stat: str = "median",
    label: str = "name",
) -> list[Run]

Load the results of the runs in the window.

Only the files that pytest-benchmark names (NNNN_*.json) are read, the other files are ignored. A run without time is dropped.

Parameters:

Name Type Description Default
storage Path

The pytest-benchmark storage directory.

required
days int

The size of the window in days. It must be at least 1.

90
kind str

The kind of results, "benchmark" or "import". The benchmark kind reads the statistic from the stats of pytest-benchmark; the import kind reads import_seconds_<stat> of the extra_info, and ignores the benchmarks that do not have it.

'benchmark'
stat str

The statistic of the times: "min", "mean", "median" or "max".

'median'
label str

The rule of the label of a benchmark. "name" is the name without the test_ prefix and the _benchmark suffix (test_sort_benchmark[10] is sort[10]), or the parameter for the import kind. "fullname" is the full name of the benchmark.

'name'

Returns:

Type Description
list[Run]

The runs sorted by date then by machine.

Raises:

Type Description
ValueError

If an argument is invalid, if a result file is invalid (the message has its path), or if two benchmarks of a run have the same label.

Example
>>> import json
>>> import tempfile
>>> from pathlib import Path
>>> from feu.benchmark import load_runs
>>> with tempfile.TemporaryDirectory() as tmp:
...     result = {
...         "datetime": "2999-01-01T00:00:00+00:00",
...         "benchmarks": [{"name": "test_sort_benchmark", "stats": {"median": 0.001}}],
...     }
...     _ = Path(tmp, "0001_run.json").write_text(json.dumps(result))
...     runs = load_runs(Path(tmp))
...
>>> runs[0].times["sort"]
0.001

feu.benchmark.runs.prune_runs

prune_runs(storage: Path, *, days: int = 90) -> list[Path]

Delete the results older than the window.

Parameters:

Name Type Description Default
storage Path

The pytest-benchmark storage directory.

required
days int

The size of the window in days. It must be at least 1.

90

Returns:

Type Description
list[Path]

The paths of the deleted files, sorted.

Raises:

Type Description
ValueError

If days is invalid or if a result file has no valid date. The files that were sorted before it may be deleted.

Example
>>> import json
>>> import tempfile
>>> from pathlib import Path
>>> from feu.benchmark import prune_runs
>>> with tempfile.TemporaryDirectory() as tmp:
...     result = {"datetime": "2000-01-01T00:00:00+00:00", "benchmarks": []}
...     _ = Path(tmp, "0001_run.json").write_text(json.dumps(result))
...     [path.name for path in prune_runs(Path(tmp))]
...
['0001_run.json']

feu.benchmark.trend

Contain functions to detect the regressions of the benchmarks.

The functions are pure: they take the runs returned by load_runs.

feu.benchmark.trend.Regression dataclass

Store a benchmark that is slower than its recent median.

Parameters:

Name Type Description Default
machine str

The machine of the runs, see Run.

required
label str

The label of the benchmark.

required
latest float

The time in seconds of the latest run.

required
baseline float

The median time in seconds of the previous runs.

required
change float

The slowdown in percent.

required
Example
>>> from feu.benchmark import Regression
>>> Regression("", "sort", 0.002, 0.001, 100.0).change
100.0

feu.benchmark.trend.find_regressions

find_regressions(
    runs: Sequence[Run],
    *,
    baseline_runs: int = 7,
    threshold: float = 50.0,
) -> list[Regression]

Find the benchmarks slower than their recent median.

The latest run is compared with the median of the previous runs (at most baseline_runs), which is more stable than the comparison with one run because the runners are noisy. The runs of different machines are never compared with each other: each machine has its own latest run and its own baseline.

Parameters:

Name Type Description Default
runs Sequence[Run]

The runs returned by load_runs. They must be in chronological order: the last run of a machine is its latest run.

required
baseline_runs int

The maximum number of previous runs used for the median. It must be at least 1.

7
threshold float

The slowdown in percent above which a benchmark is a regression. It must be greater than 0.

50.0

Returns:

Type Description
list[Regression]

The regressions sorted by decreasing change, then by label and machine. It is empty if no machine has two runs.

Raises:

Type Description
ValueError

If baseline_runs is less than 1 or threshold is not greater than 0.

Example
>>> from datetime import UTC, datetime
>>> from feu.benchmark import Run, find_regressions
>>> date = datetime(2026, 1, 1, tzinfo=UTC)
>>> runs = [Run(date, "", {"sort": 0.001}), Run(date, "", {"sort": 0.003})]
>>> find_regressions(runs)
[Regression(machine='', label='sort', latest=0.003, baseline=0.001, change=200.0)]

feu.benchmark.report

Contain functions to make the report of the trend of the benchmarks.

The functions are pure: they take the runs returned by load_runs and return a Markdown report and the GitHub Actions annotations. They do not read or write any file.

feu.benchmark.report.TrendReport dataclass

Store the report of the trend of the benchmarks.

Parameters:

Name Type Description Default
markdown str

The Markdown report. It is never empty.

required
annotations tuple[str, ...]

The GitHub Actions ::warning:: commands of the regressions, already escaped.

required
regressions tuple[Regression, ...]

The regressions of every machine.

required
Example
>>> from feu.benchmark import TrendReport
>>> TrendReport("## Benchmarks", (), ()).annotations
()

feu.benchmark.report.make_report

make_report(
    runs: Sequence[Run],
    *,
    kind: str = "benchmark",
    stat: str = "median",
    unit: str | None = None,
    days: int = 90,
    baseline_runs: int = 7,
    threshold: float = 50.0,
) -> TrendReport

Make the report of the trend of the benchmarks.

The report has a table of the trend of each benchmark and, if there are regressions, a regression block. If the runs come from several machines, the report has one section per machine, because the runs of different machines are never compared. In the table, the change is between the first and the latest run of the benchmark, whereas the change of a regression is relative to the median of the previous runs. The rows are sorted by latest time then by label.

Parameters:

Name Type Description Default
runs Sequence[Run]

The runs returned by load_runs, in chronological order. There must be at least one.

required
kind str

The kind of results, "benchmark" or "import".

'benchmark'
stat str

The statistic of the times, it is shown in the title.

'median'
unit str | None

The unit of the times: "s", "ms", "us" or "ns". If None, "us" is used for the benchmark kind and "ms" for the import kind. Times are shown with three significant digits.

None
days int

The size of the window in days, shown in the title.

90
baseline_runs int

The maximum number of previous runs used to detect the regressions, see find_regressions.

7
threshold float

The slowdown in percent above which a benchmark is a regression.

50.0

Returns:

Type Description
TrendReport

The report.

Raises:

Type Description
ValueError

If there is no run, if kind, stat or unit is unknown, or if baseline_runs or threshold is invalid.

Example
>>> from datetime import UTC, datetime
>>> from feu.benchmark import Run, make_report
>>> date = datetime(2026, 1, 1, tzinfo=UTC)
>>> runs = [Run(date, "", {"sort": 0.001}), Run(date, "", {"sort": 0.003})]
>>> report = make_report(runs)
>>> report.markdown.splitlines()[0]
'## Benchmarks (median) over the last 90 days (2 runs)'
>>> report.regressions[0].change
200.0
>>> report.annotations[0].startswith("::warning title=Regression::sort is 200.0%25 slower")
True