Skip to content

About

Catch performance regressions beside your tests with JSON baselines and metric budgets.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

1 star

Watchers

0 watching

Forks

swift-benchmark logo

swift-benchmark

Catch performance regressions beside your tests with JSON baselines and metric budgets.

CI Swift 6.3+ Platforms: iOS 18+ | macOS 15+ | Linux License: Apache-2.0 WITH Swift-exception

Overview · Quick look · Install · Usage · Requirements · Documentation · Contributing · License

Overview

Each benchmark step is timed in calibrated batches for wall-clock and CPU time, instructions and cycles, then counted. The package workflow measures each step in a process of its own, which records its peak memory, and counts it in a second process that sees every heap allocation, from Swift, C, C++ and Objective-C, and can show where they come from, alongside Swift retains and releases. Choose the metric and statistic that define a regression and compare runs with a saved baseline: a change fails only past its threshold and beyond what noise explains, a p95 budget can fail even when the median passes, the report records a typed verdict for CI and a Markdown summary for pull requests, and it warns when a comparison is unreliable, such as a debug build, a baseline from another machine, outliers, or samples too noisy for the threshold. The usage manual covers measurement policies, instrumentation, and platform limits.

Quick look

import Benchmark
import BenchmarkTesting
import Testing

@Suite(.benchmark(configuration: .init(warmup: .iterations(2), iterations: .iterations(20))))
struct SortingBenchmarks {
  @Test(arguments: [1_000, 10_000])
  func sort(count: Int) {
    blackHole((0..<count).reversed().sorted())
  }
}

Install

Add the package and the products you use to Package.swift:

dependencies: [
  .package(
    url: "https://github.com/swift-library/swift-benchmark.git",
    .upToNextMinor(from: "0.3.0")
  ),
],
targets: [
  .target(
    name: "YourLibrary",
    dependencies: [
      .product(name: "Instruments", package: "swift-benchmark"),
    ]
  ),
  .testTarget(
    name: "YourLibraryTests",
    dependencies: [
      "YourLibrary",
      .product(name: "Benchmark", package: "swift-benchmark"),
      .product(name: "BenchmarkTesting", package: "swift-benchmark"),
    ]
  ),
]

The package also provides the swift package benchmark command plugin. It is available in any package that depends on swift-benchmark.

The Instruments macros build swift-syntax. A target that only calls the runtime API, such as Instruments.current.span(_:attributes:_:), can depend on the package with traits: [] to leave the macros and swift-syntax out. SwiftPM merges traits across the dependency graph, so this takes effect only when no package in the graph depends on swift-benchmark with its default traits. Swift 6.3 still builds the macros and swift-syntax; the saving needs Swift 6.4 or later.

Note

swift-benchmark is pre-1.0. Minor releases may include source-breaking changes, so depend on it with .upToNextMinor(from:).

Usage

Run swift package benchmark list to discover declarations, then swift package --configuration release benchmark run to measure them. Use benchmark check with a saved JSON baseline and the metric or budget that matters to your application; it refuses to judge a debug build unless you pass --allow-debug.

The usage manual covers native and Swift Testing declarations, metrics, products, command options, baselines, trace attachments, and Instruments integration.

Requirements

  • Swift 6.3 or later; Swift 6.0 through 6.2 use 0.1.x
  • iOS 18 or later, macOS 15 or later
  • Linux, for the libraries and the command plugin; heap counting there needs glibc
  • With Swift 6.3 (Xcode 26), run the command plugin as swift package --disable-experimental-prebuilts benchmark .... SwiftPM 6.3 cannot build the plugin's SwiftSyntax tool beside macros that use SwiftSyntax prebuilts; Swift 6.4 needs no flag.

Each release supports the three most recent major versions of each declared platform, currently iOS 18, 26, and 27, and macOS 15, 26, and 27. The release reference describes compatibility and maintenance. swift-benchmark-cli --version and swift package benchmark --version print the installed version.

Documentation

Contributing

Read CONTRIBUTING.md and run its local validation commands before opening a pull request. Report vulnerabilities through the private route in SECURITY.md.

License

swift-benchmark is available under the Apache License 2.0 with the Swift Runtime Library Exception. See LICENSE.txt and NOTICE.

About

Catch performance regressions beside your tests with JSON baselines and metric budgets.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages