skillfed

pytest-benchmark

A ``pytest`` fixture for benchmarking code. It will group the tests into rounds that are calibrated to the chosen timer.

pytest-benchmark v5.2.3 14.2M downloads/30d#1,243 on PyPI1,444
Permissive license BSD-2-Clause Active released

What it is and what it does

pytest-benchmark is a pytest plugin that turns test functions into performance benchmarks by wrapping code execution in a calibrated timing harness. It groups runs into rounds, automatically adjusting iteration counts to produce stable measurements, and generates comparison reports and histograms. The package integrates directly into pytest's test discovery and reporting, so you write benchmarks as normal test functions that receive a `benchmark` fixture.

The plugin is designed for function-level micro-benchmarking and provides both simple mode (pass a function and arguments) and pedantic mode with explicit control over setup, iterations, rounds, and per-round teardown. It collects CPU info via py-cpuinfo and supports profiling, tracing, and cProfile integration. Results can be compared across runs and exported to CSV.

Use it for:

  • Measure and track performance regressions in a CI/CD pipeline by comparing benchmark results across commits.
  • Profile and optimize hot-path functions by running repeated timed iterations with automatic calibration.
  • Generate performance reports and histograms to document the speed of critical algorithms or library functions.
  • Set up parametrized benchmarks to test performance across different input sizes or configurations.
  • Integrate cProfile or tracing into benchmarks to identify bottlenecks in slow functions.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

A pytest fixture that benchmarks Python functions by running them in calibrated rounds and generating performance reports with timing statistics.

Yes. pytest-benchmark is actively maintained, has no known vulnerabilities, low install friction, and a permissive license. It is the standard tool for pytest-based performance testing and is well-suited for any project that needs to track or optimize function-level performance.

Install

pytest-benchmark on PyPI

pip

pip install pytest-benchmark

uv

uv add pytest-benchmark

poetry

poetry add pytest-benchmark

Installing pytest-benchmark

Before you install

Low friction install with only two runtime dependencies (pytest and py-cpuinfo). Actively maintained with recent updates for pytest 9.0 compatibility.

License in practice

BSD-2-Clause is a permissive license with minimal restrictions; you can use this package in commercial and proprietary projects with only attribution requirements.

Quickstart

pip install pytest-benchmark

import time

def test_my_function(benchmark):
    def work():
        time.sleep(0.000001)
    result = benchmark(work)
    assert result is None

Requires Python 3.9 or later and pytest to be installed; benchmarks run via pytest command, not standalone.

Verify before relying

  • Exact overhead or accuracy impact of the calibration mechanism on very fast functions.
  • Whether py-cpuinfo is always required or only for CPU-specific timing modes.
  • Performance characteristics when benchmarking code blocks versus individual functions.

Package facts

License BSD-2-Clause (permissive)
Python support supports the current Python release (>=3.9)
Install friction low — pure-Python wheel
Runtime dependencies 2 — pytest, py-cpuinfo
Maintenance actively maintained — 278 days since the last release
Last repo commit
First released
Downloads 14,224,897/month — #1,243 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: pytest_benchmark-5.2.3-py3-none-any.whl

Keywords: pytest, benchmark

Development Status :: 5 - Production/StableIntended Audience :: DevelopersOperating System :: Microsoft :: WindowsOperating System :: POSIXOperating System :: UnixProgramming Language :: PythonProgramming Language :: Python :: 3Programming Language :: Python :: 3 :: OnlyProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: 3.9Programming Language :: Python :: Implementation :: CPythonProgramming Language :: Python :: Implementation :: PyPyTopic :: Utilities

Tags

pytest benchmarking fixtureperformance testing pythoncode timing and profilingbenchmark test runnerfunction performance measurementpytest performance plugintiming calibration tool
performance-testingpytest-pluginprofiling

More Utilities packages

Further reading