mirror of
https://github.com/apple/swift-log
synced 2026-11-24 03:47:49 +00:00
### Motivation: Currently the benchmark machinery overhead is causing a lot of noise in benchmarks of very efficient/fast code. ### Modifications: Use the scaling factor to amortize the fixed overhead and let the actual tested code account for most of the measured metrics. ### Result: More reliable benchmarks that don't fail 20% of the time.
72 lines
2.2 KiB
Swift
72 lines
2.2 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Logging API open source project
|
|
//
|
|
// Copyright (c) 2025 Apple Inc. and the Swift Logging API project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of Swift Logging API project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Benchmark
|
|
import Foundation
|
|
import Logging
|
|
|
|
public func makeBenchmark(
|
|
loggerLevel: Logger.Level,
|
|
logLevel: Logger.Level,
|
|
setScopedLogger: Bool = false,
|
|
_ suffix: String = "",
|
|
_ body: @escaping (Logger) -> Void
|
|
) {
|
|
let iterations = 1_000_000
|
|
let metrics: [BenchmarkMetric] = [.instructions, .objectAllocCount]
|
|
|
|
var logger = Logger(label: "BenchmarkRunner_\(loggerLevel)_\(logLevel)")
|
|
|
|
// Use a NoOpLogHandler to avoid polluting the logs
|
|
logger.handler = NoOpLogHandler(label: "NoOpLogHandler")
|
|
logger.logLevel = loggerLevel
|
|
|
|
Benchmark(
|
|
"\(logLevel)_log_with_\(loggerLevel)_log_level\(suffix)",
|
|
configuration: .init(
|
|
metrics: metrics,
|
|
scalingFactor: .kilo,
|
|
maxIterations: iterations,
|
|
thresholds: [
|
|
.instructions: BenchmarkThresholds(
|
|
relative: [
|
|
.p90: 10.0 // we only record p90
|
|
]
|
|
),
|
|
.objectAllocCount: BenchmarkThresholds(
|
|
absolute: [
|
|
.p90: 0 // we only record p90
|
|
]
|
|
),
|
|
]
|
|
)
|
|
) { benchmark in
|
|
if setScopedLogger {
|
|
withLogger(logger) { logger in
|
|
benchmark.startMeasurement()
|
|
for _ in benchmark.scaledIterations {
|
|
body(logger)
|
|
}
|
|
benchmark.stopMeasurement()
|
|
}
|
|
} else {
|
|
benchmark.startMeasurement()
|
|
for _ in benchmark.scaledIterations {
|
|
body(logger)
|
|
}
|
|
benchmark.stopMeasurement()
|
|
}
|
|
}
|
|
}
|