Skip to main content

Snapshot Testing Tool for ASV Benchmarks

A tool for capturing and comparing function outputs from ASV benchmarks to verify correctness after optimizations.

Overview

This tool integrates with ASV (airspeed-velocity) benchmarks to:

  • Capture function return values before optimization
  • Compare outputs after optimization to ensure correctness
  • Use execution tracing to capture the deepest function call's return value
  • Support both function-level and class-based parameterized benchmarks

Installation

Using uv (recommended)

# Install with uv
uv pip install -e .

# Or install directly from repository
uv pip install git+https://github.com/formula-code/snapshot-tester.git

Using pip

# Install in development mode
pip install -e .

# Or install directly
pip install git+https://github.com/formula-code/snapshot-tester.git

Usage

Command Line Interface

After installation, use the snapshot-tool command:

List Benchmarks

snapshot-tool list <benchmark_dir> [--filter PATTERN]

Example:

snapshot-tool list examples/astropy-benchmarks/benchmarks --filter time_angle

Capture Snapshots

snapshot-tool capture <benchmark_dir> [--filter PATTERN] [--snapshot-dir DIR]

This captures baseline snapshots of all benchmark outputs.

Verify Against Snapshots

snapshot-tool verify <benchmark_dir> [--filter PATTERN] [--snapshot-dir DIR] [--tolerance RTOL ATOL]

This runs benchmarks and compares outputs against stored snapshots.

Configuration

snapshot-tool config --init    # Create default config
snapshot-tool config --show    # Show current config

Running with uv

You can also run the tool without installation using uv run:

uv run snapshot-tool list <benchmark_dir>
uv run snapshot-tool capture <benchmark_dir>
uv run snapshot-tool verify <benchmark_dir>

Programmatic Usage

from snapshot_tool import BenchmarkDiscovery, BenchmarkRunner, SnapshotManager, Comparator

# Discover benchmarks
discovery = BenchmarkDiscovery("benchmarks/")
benchmarks = discovery.discover_all()

# Run benchmarks with tracing
runner = BenchmarkRunner("benchmarks/")
result = runner.run_benchmark(benchmark)

# Store snapshots
storage = SnapshotManager(".snapshots/")
storage.store_snapshot(
    benchmark_name="my_benchmark",
    module_path="my_module", 
    parameters=(),
    param_names=None,
    return_value=result.return_value
)

# Compare outputs
comparator = Comparator()
comparison = comparator.compare(actual_value, expected_value)

Architecture

Core Components

  1. BenchmarkDiscovery: Parses ASV benchmark files to find benchmark classes and functions
  2. ExecutionTracer: Uses sys.settrace() to capture the deepest function call's return value
  3. BenchmarkRunner: Executes benchmarks with tracing enabled
  4. SnapshotManager: Stores and retrieves snapshots using pickle files
  5. Comparator: Compares outputs using numpy.allclose for numerical data
  6. CLI: Command-line interface for easy usage

Key Features

  • Hierarchical Discovery: Supports nested benchmark directories
  • Parameter Handling: Automatically generates parameter combinations for parameterized benchmarks
  • Global Variables: Handles benchmarks that use global variables
  • Setup Methods: Supports class-based benchmarks with setup methods
  • Flexible Comparison: Uses numpy.allclose for numerical data with configurable tolerances
  • Metadata Tracking: Stores git commit, timestamp, and other metadata with snapshots

Configuration

Create a snapshot_config.json file to customize behavior:

{
  "benchmark_dir": "benchmarks/",
  "snapshot_dir": ".snapshots/",
  "tolerance": {
    "rtol": 1e-5,
    "atol": 1e-8,
    "equal_nan": false
  },
  "exclude_benchmarks": [],
  "trace_depth_limit": 100,
  "verbose": false,
  "quiet": false
}

Example Workflow

  1. Initial Setup: Capture baseline snapshots

    python snapshot_test.py capture benchmarks/
    
  2. Make Optimizations: Modify your code to improve performance

  3. Verify Correctness: Compare outputs against snapshots

    python snapshot_test.py verify benchmarks/
    
  4. Review Results: The tool will report which benchmarks passed/failed

Limitations

  • Requires the benchmarked project to be importable
  • Some benchmarks may not return meaningful values to capture
  • Execution tracing adds overhead to benchmark runs
  • Pickle format may not be compatible across Python versions

Testing

Run the test script to verify the tool works:

python test_snapshot_tool.py

This will test discovery, storage, comparison, and basic benchmark execution.

Contributing

The tool is designed to be extensible. Key areas for enhancement:

  • Support for more data types in comparison
  • Integration with CI/CD systems
  • Web-based reporting interface
  • Support for distributed snapshot storage

Metadata

Release files for snapshot-tool 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for snapshot-tool 0.2.1
File Size Uploaded Provenance
snapshot_tool-0.2.1.tar.gz 40.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for snapshot-tool 0.2.1
File Interpreter ABI Platform
snapshot_tool-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 84.5 kB

Release files / snapshot_tool-0.2.1.tar.gz

Download URL snapshot_tool-0.2.1.tar.gz
Size 40.1 kB
Tags Source
SHA-256 checksum
How to use checksums
7a9aa5c55cc41ada2bd5a8c5a5b35937ffad32c522fb97cf1350c0a3443fe386
BLAKE2b-256 checksum
How to use checksums
b54ba38c4c809a150139e2292fc5b5dfbc45a20adad1f1e7839b2d93dd39cffe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / snapshot_tool-0.2.1-py3-none-any.whl

Download URL snapshot_tool-0.2.1-py3-none-any.whl
Size 44.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
047c4bb7349abf05747a9a73ea0780c828f59a7e393c39a4a24c14c2c63e9fc2
BLAKE2b-256 checksum
How to use checksums
56f2d003815a34ef68b3112580b49fd87942b633991ceca85da5e6eb3620f9cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for snapshot-tool 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page