pybench


Overview

The **pybench** file is a simple Bash script designed to run Python benchmark tests using the `pytest` framework with the `pytest-benchmark` plugin. It provides a standardized way to execute and measure performance of benchmark test scripts located in the `bench/` directory. The script takes a single argument that identifies which benchmark test file to run and configures pytest with specific benchmarking options to control the test duration, garbage collection behavior, data saving, and test execution order.


Detailed Explanation

Script Purpose


How to Use

./pybench <benchmark_name>

**Example:**

./pybench sorting

This will run:

pytest --verbose \
    --benchmark-min-time=1 \
    --benchmark-max-time=5 \
    --benchmark-disable-gc \
    --benchmark-autosave \
    --benchmark-save-data \
    --random-order \
    bench/benchmark_sorting.py

Script Breakdown

#!/usr/bin/env bash
set -eou pipefail

This combination ensures the script halts on errors or usage of undefined variables, improving safety and debugging.


Command Details

The script runs `pytest` with the following options:

Option

Description

`--verbose`

Enables verbose output for clearer test progress visibility.

`--benchmark-min-time=1`

Each benchmark runs for at least 1 second to gather sufficient data for accuracy.

`--benchmark-max-time=5`

Benchmarks run no longer than 5 seconds to limit total test time.

`--benchmark-disable-gc`

Garbage collection is disabled during benchmarks to reduce noise and improve consistency.

`--benchmark-autosave`

Automatically saves benchmark results after the run completes.

`--benchmark-save-data`

Saves detailed benchmark data for future comparisons or analysis.

`--random-order`

Randomizes the order of test execution to avoid order-dependent side effects.

`"bench/benchmark_$1.py"`

Runs the benchmark test file whose name is constructed dynamically from the provided argument.


Implementation Details


Interaction with Other System Components


Summary

Aspect

Description

File Type

Bash script

Purpose

Run Python benchmark tests with predefined options

Input

Benchmark test name (string)

Output

Benchmark results printed and saved automatically

Dependencies

pytest, pytest-benchmark plugin, benchmark test files

Usage Context

Performance testing and benchmarking automation


Mermaid Diagram

flowchart TD
    A[Start: Run pybench <benchmark_name>] --> B[Construct filename: bench/benchmark_<name>.py]
    B --> C[Invoke pytest with benchmark options]
    C --> D{pytest executes benchmark tests}
    D --> E[Run each benchmark for 1-5 seconds]
    E --> F[Disable garbage collection during tests]
    F --> G[Randomize test order]
    G --> H[Autosave benchmark results and data]
    H --> I[Display verbose output]
    I --> J[Exit with status code]

    style A fill:#f9f,stroke:#333,stroke-width:1px
    style J fill:#bbf,stroke:#333,stroke-width:1px

Summary

The `pybench` script is a utility for running Python benchmarking tests in a controlled and consistent manner. By wrapping pytest with specific options, it ensures benchmarks are run with reliable timing, garbage collection disabled for consistency, and results saved automatically. This helps developers and teams easily measure and track performance changes across different benchmark scripts within a project.