pytest-benchmark
A ``pytest`` fixture for benchmarking code. It will group the tests into rounds that are calibrated to the chosen timer.
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-benchmarkuv
uv add pytest-benchmarkpoetry
poetry add pytest-benchmarkInstalling 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
Tags
More Utilities packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
charset-normalizerDetects and normalizes text encoding from…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
pluggyPluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
PygmentsPygments is a syntax highlighter that colorizes…
permissive · top 100 on PyPI
sixSix provides utility functions to write Python…
permissive · top 100 on PyPI
pytest-codspeedA pytest plugin that integrates performance…
permissive · top 5,000 on PyPI
codeflash-benchmarkA pytest plugin that adds benchmarking…
unclear · top 15,000 on PyPI
asv-runnerProvides core Python benchmark execution code…
permissive · top 15,000 on PyPI
google-benchmarkProvides Python bindings to Google's C++…
permissive · top 15,000 on PyPI
pytest-harvestpytest-harvest captures and stores data…
permissive · top 15,000 on PyPI
pytest-durationsA pytest plugin that measures and reports…
permissive · top 5,000 on PyPI
msbench-utilsProvides utility functions and helpers for…
permissive · top 15,000 on PyPI
asvasv benchmarks Python packages over their…
permissive · top 15,000 on PyPI
pyperfpyperf is a toolkit for writing, running, and…
permissive · top 15,000 on PyPI
pytest-postgresqlA pytest plugin that provides fixtures to…
copyleft · top 5,000 on PyPI