Repolode
ionelmc

ionelmc/pytest-benchmark

A pytest plugin that provides a benchmark fixture for timing Python functions in pytest test suites.

Stars
1,459
Forks
139
Open issues
125
Last commit
6 days ago

What is it?

pytest-benchmark adds a benchmark fixture to pytest. Passing a function to the fixture runs it repeatedly, grouping runs into rounds calibrated to the chosen timer, and reports the timing results in pytest's summary output.

The fixture can be called with positional or keyword arguments, and a pedantic mode gives explicit control over setup functions, iteration counts and round counts. Results can be compared against earlier runs, and the plugin can generate histograms of the timing distribution.

Because it integrates with pytest, benchmarks are written as ordinary test functions and run with the usual pytest command. The plugin benchmarks single functions, so measuring a larger block of code requires wrapping it in a function.

Key features

  • 01Provides a benchmark fixture that times a function call inside a pytest test.
  • 02Groups measurements into calibrated rounds for the chosen timer.
  • 03Offers a pedantic mode for custom setup, iterations and rounds.
  • 04Supports comparing results against previous benchmark runs.
  • 05Can generate histograms of timing results.

Use cases

  • Tracking performance regressions of Python functions across commits.
  • Comparing two implementations of the same function within a test suite.
  • Measuring hot paths in a library while keeping benchmarks in the existing pytest workflow.

Quick start

  1. 1

    Install the plugin from PyPI

    pip install pytest-benchmark

Things to know

  • It benchmarks only single functions, so whole programs or code blocks need a wrapper function.

Topics

FAQ

What is pytest-benchmark?
A pytest plugin that provides a benchmark fixture for timing Python functions in pytest test suites. pytest-benchmark adds a benchmark fixture to pytest. Passing a function to the fixture runs it repeatedly, grouping runs into rounds calibrated to the chosen timer, and reports the timing results in pytest's summary output.
Is pytest-benchmark free to use? What license does it have?
pytest-benchmark is open source under the BSD-2-Clause license. Check the license terms before using it in commercial products.
Is pytest-benchmark still maintained?
Actively maintained. The last commit to pytest-benchmark was on Nov 1, 2026.
What can I use pytest-benchmark for?
Tracking performance regressions of Python functions across commits. Comparing two implementations of the same function within a test suite. Measuring hot paths in a library while keeping benchmarks in the existing pytest workflow.
How do I install pytest-benchmark?
Install the plugin from PyPI: pip install pytest-benchmark
Where can I get pytest-benchmark?
The source code and installation instructions are on GitHub: https://github.com/ionelmc/pytest-benchmark

Summary generated by AI from the project's README. GitHub stats refresh daily.

Related projects