//===----------------------------------------------------------------------===// // // 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() } } }