Skip to main content

Snapshot testing tool for ASV benchmarks - captures and compares function outputs to verify correctness after optimizations

Project description

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

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

snapshot_tool-0.2.1.tar.gz (40.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

snapshot_tool-0.2.1-py3-none-any.whl (44.4 kB view details)

Uploaded Python 3

File details

Details for the file snapshot_tool-0.2.1.tar.gz.

File metadata

  • Download URL: snapshot_tool-0.2.1.tar.gz
  • Upload date:
  • Size: 40.1 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for snapshot_tool-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7a9aa5c55cc41ada2bd5a8c5a5b35937ffad32c522fb97cf1350c0a3443fe386
MD5 7fb7ce9e267c0ae8d8aae7651b1a3849
BLAKE2b-256 b54ba38c4c809a150139e2292fc5b5dfbc45a20adad1f1e7839b2d93dd39cffe

See more details on using hashes here.

File details

Details for the file snapshot_tool-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: snapshot_tool-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 44.4 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for snapshot_tool-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 047c4bb7349abf05747a9a73ea0780c828f59a7e393c39a4a24c14c2c63e9fc2
MD5 7beece5b15a66204e38f4a911a128162
BLAKE2b-256 56f2d003815a34ef68b3112580b49fd87942b633991ceca85da5e6eb3620f9cb

See more details on using hashes here.

Supported by

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