Pytest plugin to create CodSpeed benchmarks
—
Command-line options and marker parameters for controlling benchmark behavior. pytest-codspeed provides extensive configuration through CLI flags, environment variables, and marker options.
Options for controlling pytest-codspeed behavior from the command line.
# Enable CodSpeed benchmarking
--codspeed
# Set measurement mode
--codspeed-mode {instrumentation,walltime}
# Walltime mode timing options
--codspeed-warmup-time FLOAT # Warmup time in seconds
--codspeed-max-time FLOAT # Maximum benchmark time in seconds
--codspeed-max-rounds INT # Maximum number of rounds# Basic benchmarking with walltime mode
pytest tests/ --codspeed
# Use instrumentation mode for more precise measurement
pytest tests/ --codspeed --codspeed-mode instrumentation
# Customize timing for walltime mode
pytest tests/ --codspeed --codspeed-warmup-time 2.0 --codspeed-max-time 10.0 --codspeed-max-rounds 50Environment variables that control pytest-codspeed behavior, primarily used in CI/CD environments.
# Core environment variables
CODSPEED_ENV # Enables CodSpeed when set (any value)
CODSPEED_RUNNER_MODE # Sets default mode: "walltime" or other
CODSPEED_PROFILE_FOLDER # Directory for results output
# Extension build control
PYTEST_CODSPEED_FORCE_EXTENSION_BUILD # Force native extension compilation
PYTEST_CODSPEED_SKIP_EXTENSION_BUILD # Skip native extension compilation# Enable CodSpeed in CI environment
export CODSPEED_ENV=1
pytest tests/
# Force walltime mode in CI
export CODSPEED_ENV=1
export CODSPEED_RUNNER_MODE=walltime
pytest tests/
# Custom results directory
export CODSPEED_ENV=1
export CODSPEED_PROFILE_FOLDER=/custom/results/path
pytest tests/Parameters that can be passed to benchmark markers to customize individual test behavior.
@pytest.mark.benchmark(
group: str = None, # Group name for benchmark organization
min_time: float = None, # Minimum time per round (seconds, walltime only)
max_time: float = None, # Maximum total benchmark time (seconds, walltime only)
max_rounds: int = None # Maximum number of rounds (walltime only)
)
@pytest.mark.codspeed_benchmark(
group: str = None, # Same options as benchmark marker
min_time: float = None,
max_time: float = None,
max_rounds: int = None
)import pytest
# Group related benchmarks
@pytest.mark.benchmark(group="string_operations")
def test_string_join():
result = "".join(str(i) for i in range(1000))
assert len(result) > 0
@pytest.mark.benchmark(group="string_operations")
def test_string_format():
result = " ".join(f"item_{i}" for i in range(1000))
assert len(result) > 0
# Control timing for slow operations
@pytest.mark.benchmark(max_time=10.0, max_rounds=20)
def test_expensive_computation():
result = sum(i ** 2 for i in range(10000))
assert result > 0
# Ensure minimum measurement time for fast operations
@pytest.mark.benchmark(min_time=0.1)
def test_fast_operation():
result = 1 + 1
assert result == 2pytest-codspeed supports two measurement approaches with different accuracy/overhead tradeoffs.
# Measurement modes
MeasurementMode.WallTime # Wall-clock time measurement (default locally)
MeasurementMode.Instrumentation # Instruction-based measurement (default in CI)Configuration values are resolved in this order (highest to lowest priority):
# Force native extension build (raises error if impossible)
export PYTEST_CODSPEED_FORCE_EXTENSION_BUILD=1
# Skip native extension (falls back to walltime mode)
export PYTEST_CODSPEED_SKIP_EXTENSION_BUILD=1# Default local results location
.codspeed/results_{timestamp}.json
# CI results location (when CODSPEED_PROFILE_FOLDER set)
{CODSPEED_PROFILE_FOLDER}/results/{pid}.jsonpytest-codspeed automatically handles compatibility with other benchmark plugins:
pytest-benchmark when activepytest-speed when active__benchmark if neededInstall with Tessl CLI
npx tessl i tessl/pypi-pytest-codspeed