Skip to main content

Pytest plugin to create CodSpeed benchmarks

Project description

pytest-codspeed

CI PyPi Version Python Version Discord CodSpeed Badge

Pytest plugin to create CodSpeed benchmarks

Requirements

Python: 3.8 and later

pytest: any recent version

Installation

pip install pytest-codspeed

Usage

Creating benchmarks

Creating benchmarks with pytest-codspeed is compatible with the standard pytest-benchmark API. So if you already have benchmarks written with it, you can start using pytest-codspeed right away.

Marking a whole test function as a benchmark with pytest.mark.benchmark

import pytest
from statistics import median

@pytest.mark.benchmark
def test_median_performance():
    return median([1, 2, 3, 4, 5])

Benchmarking selected lines of a test function with the benchmark fixture

import pytest
from statistics import mean

def test_mean_performance(benchmark):
    # Precompute some data useful for the benchmark but that should not be
    # included in the benchmark time
    data = [1, 2, 3, 4, 5]

    # Benchmark the execution of the function
    benchmark(lambda: mean(data))


def test_mean_and_median_performance(benchmark):
    # Precompute some data useful for the benchmark but that should not be
    # included in the benchmark time
    data = [1, 2, 3, 4, 5]

    # Benchmark the execution of the function:
    # The `@benchmark` decorator will automatically call the function and
    # measure its execution
    @benchmark
    def bench():
        mean(data)
        median(data)

Running benchmarks

Testing the benchmarks locally

If you want to run only the benchmarks tests locally, you can use the --codspeed pytest flag:

pytest tests/ --codspeed

Note: Running pytest-codspeed locally will not produce any performance reporting. It's only useful for making sure that your benchmarks are working as expected. If you want to get performance reporting, you should run the benchmarks in your CI.

In your CI

You can use the CodSpeedHQ/action to run the benchmarks in Github Actions and upload the results to CodSpeed.

Example workflow:

name: benchmarks

on:
  push:
    branches:
      - "main" # or "master"
  pull_request:

jobs:
  benchmarks:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - name: Install dependencies
        run: pip install -r requirements.txt
      - name: Run benchmarks
        uses: CodSpeedHQ/action@v3
        with:
          token: ${{ secrets.CODSPEED_TOKEN }}
          run: pytest tests/ --codspeed

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_codspeed-3.0.0b3.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

pytest_codspeed-3.0.0b3-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file pytest_codspeed-3.0.0b3.tar.gz.

File metadata

  • Download URL: pytest_codspeed-3.0.0b3.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for pytest_codspeed-3.0.0b3.tar.gz
Algorithm Hash digest
SHA256 4f1cc7238894a24ba22b62585391445bba898a01f2097935829257ebbb90d909
MD5 11e58d7773679e76ede66f4b8da1be1f
BLAKE2b-256 7169f1c096b9473d0e8beed5029cffb2cefee048a28eaa6798bf5482abb04948

See more details on using hashes here.

File details

Details for the file pytest_codspeed-3.0.0b3-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_codspeed-3.0.0b3-py3-none-any.whl
Algorithm Hash digest
SHA256 be5715332d21c39c2481fc60097ddf7ed8653e6c2bc511fad1b9e3211af69896
MD5 40227f923a8249faf87acf743ef61f1e
BLAKE2b-256 891a920c26036fbbea8aead48a06a097ac61db737edb3ae1cdc8c540e00c5a07

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page