pytest-benchmark

5.2.3 · active · verified Sun Apr 05

pytest-benchmark is a plugin for pytest that provides a `benchmark` fixture for benchmarking code. It automatically groups tests into rounds calibrated to the chosen timer, offering sensible defaults and automatic calibration for micro-benchmarks. As of version 5.2.3, it is actively maintained with regular major releases approximately once a year and several minor/patch releases.

Warnings

Install

Imports

Quickstart

Define a test function that accepts the `benchmark` fixture. Pass the function you wish to benchmark as the first argument to `benchmark()`. You can also pass positional and keyword arguments to the benchmarked function. Running `pytest` will execute the benchmarks and display a summary table.

import time

def something(duration=0.000001):
    """ Function that needs some serious benchmarking. """
    time.sleep(duration)
    return 123

def test_my_stuff(benchmark):
    # benchmark something
    result = benchmark(something)
    assert result == 123

# To run: save the above as a Python file (e.g., test_benchmarks.py) and run `pytest` from your terminal.
# Use `pytest --benchmark-autosave --benchmark-json=report.json` for advanced features.

view raw JSON →