Skip to content

Commit

Permalink
chore: make startup and shutdown hooks async (and throwing) (#141)
Browse files Browse the repository at this point in the history
Make benchmark startup and shutdown hook closures async and throwing.

---------

Co-authored-by: Joakim Hassila <[email protected]>
  • Loading branch information
dimlio and hassila authored Mar 30, 2023
1 parent da9cefd commit 9a5cd29
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
9 changes: 4 additions & 5 deletions Plugins/BenchmarkTool/BenchmarkTool+Operations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ extension BenchmarkTool {
benchmarks.append(benchmark)
case .end:
break outerloop
case let .error(description):
failBenchmark(description)
break outerloop
default:
print("Unexpected reply \(benchmarkReply)")
}
Expand All @@ -59,11 +62,7 @@ extension BenchmarkTool {
case .end:
break outerloop
case let .error(description):
print("*****")
print("***** Benchmark '\(benchmark.name)' failed:")
print("***** \(description)")
print("*****")
failBenchmark("")
failBenchmark(description)
break outerloop
default:
print("Unexpected reply \(benchmarkReply)")
Expand Down
2 changes: 1 addition & 1 deletion Sources/Benchmark/Benchmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public final class Benchmark: Codable, Hashable {
public typealias BenchmarkCustomMetricMeasurement = (BenchmarkMetric, Int) -> Void

/// Alias for closures used to hook into setup / teardown
public typealias BenchmarkHook = () -> Void
public typealias BenchmarkHook = () async throws -> Void

/// This closure if set, will be run before a targets benchmarks are run, but after they are registered
public static var startupHook: BenchmarkHook?
Expand Down
15 changes: 13 additions & 2 deletions Sources/Benchmark/BenchmarkRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ public struct BenchmarkRunner: AsyncParsableCommand, BenchmarkRunnerReadWrite {
var benchmark: Benchmark?
var results: [BenchmarkResult] = []

Benchmark.startupHook?()
do {
try await Benchmark.startupHook?()
} catch {
try channel.write(.error("Benchmark.startupHook failed: \(error)"))
return
}

while true {
if debug { // in debug mode we run all benchmarks matching filter/skip specified
Expand Down Expand Up @@ -197,9 +202,15 @@ public struct BenchmarkRunner: AsyncParsableCommand, BenchmarkRunnerReadWrite {
print("Internal error: Couldn't find specified benchmark '\(benchmarkToRun.name)' to run.")
}

do {
try await Benchmark.shutdownHook?()
} catch {
try channel.write(.error("Benchmark.shutdownHook failed: \(error)"))
return
}

try channel.write(.end)
case .end:
Benchmark.shutdownHook?()
return
}
}
Expand Down

0 comments on commit 9a5cd29

Please sign in to comment.