Skip to content

1.5.0

Compare
Choose a tag to compare
@github-actions github-actions released this 21 Apr 08:55
· 111 commits to main since this release
eb5c4b2

Added additional ways to customize startup/teardown code for benchmarks

There are now multiple ways to setup shared benchmark setup/teardown code (the old startupHook/shutdownHooks are deprecated and will be removed in a future major release, please use Benchmark.setup and Benchmark.teardown instead).

First off, one can easily setup global shared state that can be reused by just defining it in the closure:

import Benchmark

let benchmarks = {
    let mySharedSetup = [1, 2, 3, 4]

    Benchmark("Minimal benchmark") { benchmark in
    // Some work to measure here, use mySharedSetup
    }

    Benchmark("Minimal benchmark 2") { benchmark in
    // Some work to measure here, use mySharedSetup here too
    }
}

Secondly, one can have setup/teardown closures that are shared across all benchmarks:

import Benchmark

let benchmarks = {
    Benchmark.setup = { print("global setup closure, used for all benchmarks") }
    Benchmark.teardown = { print("global teardown closure, used for all benchmarks") }

    Benchmark("Minimal benchmark") { benchmark in
    // Some work to measure here, use mySharedSetup
    }

    Benchmark("Minimal benchmark 2") { benchmark in
    // Some work to measure here, use mySharedSetup here too
    }
}

Thirdly, one can have setup/teardown closures as part of the configuration for a subset of benchmarks

import Benchmark

func setupFunction() {
}

func teardownFunction() {
}

let benchmarks = {

// only shared setup
Benchmark("Minimal benchmark", 
    configuration: .init(setup: setupFunction, teardown: teardownFunction)) { benchmark in 
    // Some work to measure here
}

Benchmark("Minimal benchmark 2", 
    configuration: .init(setup: setupFunction, teardown: teardownFunction)) { benchmark in
    // Some work to measure here
    }
}

Finally, one can have setup/teardown closures that are specific to a given benchmark

import Benchmark

let benchmarks = {

Benchmark("Minimal benchmark") { benchmark in 
    } setup: {
     // do setup for this benchmark here
    } teardown: {
     // do teardown here
    }
}

All of these setup/teardown hooks can be combined, the order of execution is:

  • Global setup
  • Configuration provided setup
  • Closure provided setup

with teardown in reverse order.

So to use all hooks at the same time:

import Benchmark

func sharedSetup() {
}

func sharedTeardown() {
}

let benchmarks = {

    Benchmark.setup = { print("global setup closure, used for all benchmarks") }
    Benchmark.teardown = { print("global teardown closure, used for all benchmarks") }


    Benchmark("Minimal benchmark", 
    configuration: .init(setup: setupFunction, teardown: teardownFunction)) { benchmark in 
    } setup: {
    // do setup for this benchmark here
    } teardown: {
    // do teardown here
    }
}

1.5.0 (2023-04-21)

Features

  • minor: Add more thorough setup/teardown hooks (#153) (eb5c4b2)