diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5c2c79e..1ca7345 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -30,6 +30,14 @@ jobs: windows_nightly_next_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable" windows_nightly_main_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable" + benchmarks: + name: Benchmarks + uses: apple/swift-nio/.github/workflows/benchmarks.yml@main + with: + benchmark_package_path: "Benchmarks" + macos_xcode_16_4_enabled: true + macos_xcode_26_1_enabled: true + cxx-interop: name: Cxx interop uses: apple/swift-nio/.github/workflows/cxx_interop.yml@main @@ -45,6 +53,7 @@ jobs: with: runner_pool: nightly build_scheme: swift-log + swift_test_enabled: true release-builds: name: Release builds diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 7941ef0..79429ca 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -34,6 +34,15 @@ jobs: windows_nightly_next_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable" windows_nightly_main_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable" + benchmarks: + name: Benchmarks + uses: apple/swift-nio/.github/workflows/benchmarks.yml@main + with: + benchmark_package_path: "Benchmarks" + macos_runner_pool: general + macos_xcode_16_4_enabled: true + macos_xcode_26_1_enabled: true + cxx-interop: name: Cxx interop uses: apple/swift-nio/.github/workflows/cxx_interop.yml@main @@ -49,6 +58,8 @@ jobs: with: runner_pool: general build_scheme: swift-log + swift_test_enabled: true + release-builds: name: Release builds diff --git a/Benchmarks/.gitignore b/Benchmarks/.gitignore new file mode 100644 index 0000000..0023a53 --- /dev/null +++ b/Benchmarks/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +/.build +/Packages +xcuserdata/ +DerivedData/ +.swiftpm/configuration/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc diff --git a/Benchmarks/Benchmarks/SwiftLogBenchmarks/NoOpLogHandler.swift b/Benchmarks/Benchmarks/SwiftLogBenchmarks/NoOpLogHandler.swift new file mode 100644 index 0000000..4fc9fe3 --- /dev/null +++ b/Benchmarks/Benchmarks/SwiftLogBenchmarks/NoOpLogHandler.swift @@ -0,0 +1,78 @@ +//===----------------------------------------------------------------------===// +// +// 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 Logging + +struct NoOpLogHandler: LogHandler { + let label: String + public var metadataProvider: Logger.MetadataProvider? + + init(label: String, metadataProvider: Logger.MetadataProvider?) { + self.label = label + self.metadataProvider = metadataProvider + } + + init(label: String) { + self.label = label + self.metadataProvider = LoggingSystem.metadataProvider + } + + func log( + level: Logger.Level, + message: Logger.Message, + metadata explicitMetadata: Logger.Metadata?, + source: String, + file: String, + function: String, + line: UInt + ) { + // Do nothing + } + + private var _logLevel: Logger.Level? + var logLevel: Logger.Level { + get { + self._logLevel ?? .debug + } + set { + self._logLevel = newValue + } + } + + private var _metadataSet = false + private var _metadata = Logger.Metadata() { + didSet { + self._metadataSet = true + } + } + + public var metadata: Logger.Metadata { + get { + self._metadata + } + set { + self._metadata = newValue + } + } + + // TODO: would be nice to delegate to local copy of logger but StdoutLogger is a reference type. why? + subscript(metadataKey metadataKey: Logger.Metadata.Key) -> Logger.Metadata.Value? { + get { + self._metadata[metadataKey] + } + set { + self._metadata[metadataKey] = newValue + } + } +} diff --git a/Benchmarks/Benchmarks/SwiftLogBenchmarks/SwiftLogBenchmarks.swift b/Benchmarks/Benchmarks/SwiftLogBenchmarks/SwiftLogBenchmarks.swift new file mode 100644 index 0000000..9af4eac --- /dev/null +++ b/Benchmarks/Benchmarks/SwiftLogBenchmarks/SwiftLogBenchmarks.swift @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +let benchmarks: @Sendable () -> Void = { + let iterations = 1_000_000 + let metrics: [BenchmarkMetric] = [.instructions, .objectAllocCount] + + // Take a pair of levels to cover all condition ranges between the active level and logged level. + let logLevelParameterization: [Logger.Level] = [.debug, .error] + for logLevel in logLevelParameterization { + for logLevelUsed in logLevelParameterization { + var logger = Logger(label: "BenchmarkRunner_\(logLevel)_\(logLevelUsed)") + + // Use a NoOpLogHandler to avoid polluting the logs + logger.handler = NoOpLogHandler(label: "NoOpLogHandler") + logger.logLevel = logLevel + + Benchmark( + "\(logLevelUsed)_log_with_\(logLevel)_log_level", + configuration: .init( + metrics: metrics, + maxIterations: iterations, + thresholds: [ + .instructions: BenchmarkThresholds( + relative: [ + .p90: 1.0 // we only record p90 + ] + ), + .objectAllocCount: BenchmarkThresholds( + absolute: [ + .p90: 0 // we only record p90 + ] + ), + ] + ) + ) { benchmark in + // This is what we actually benchmark + logger.log(level: logLevelUsed, "hello, benchmarking world") + } + } + } +} diff --git a/Benchmarks/Package.swift b/Benchmarks/Package.swift new file mode 100644 index 0000000..7f3082f --- /dev/null +++ b/Benchmarks/Package.swift @@ -0,0 +1,30 @@ +// swift-tools-version: 6.0 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "Benchmarks", + platforms: [ + .macOS(.v13) + ], + dependencies: [ + .package(path: "../"), + .package(url: "https://github.com/ordo-one/package-benchmark.git", from: "1.29.6"), + ] +) + +// Benchmark of SwiftLogBenchmarks +package.targets += [ + .executableTarget( + name: "SwiftLogBenchmarks", + dependencies: [ + .product(name: "Benchmark", package: "package-benchmark"), + .product(name: "Logging", package: "swift-log"), + ], + path: "Benchmarks/SwiftLogBenchmarks", + plugins: [ + .plugin(name: "BenchmarkPlugin", package: "package-benchmark") + ] + ) +] diff --git a/Benchmarks/Thresholds/Xcode 16.4/SwiftLogBenchmarks.debug_log_with_debug_log_level.p90.json b/Benchmarks/Thresholds/Xcode 16.4/SwiftLogBenchmarks.debug_log_with_debug_log_level.p90.json new file mode 100644 index 0000000..2371eb9 --- /dev/null +++ b/Benchmarks/Thresholds/Xcode 16.4/SwiftLogBenchmarks.debug_log_with_debug_log_level.p90.json @@ -0,0 +1,4 @@ +{ + "instructions" : 3279, + "objectAllocCount" : 1 +} \ No newline at end of file diff --git a/Benchmarks/Thresholds/Xcode 16.4/SwiftLogBenchmarks.debug_log_with_error_log_level.p90.json b/Benchmarks/Thresholds/Xcode 16.4/SwiftLogBenchmarks.debug_log_with_error_log_level.p90.json new file mode 100644 index 0000000..f625c78 --- /dev/null +++ b/Benchmarks/Thresholds/Xcode 16.4/SwiftLogBenchmarks.debug_log_with_error_log_level.p90.json @@ -0,0 +1,4 @@ +{ + "instructions" : 1126, + "objectAllocCount" : 0 +} \ No newline at end of file diff --git a/Benchmarks/Thresholds/Xcode 16.4/SwiftLogBenchmarks.error_log_with_debug_log_level.p90.json b/Benchmarks/Thresholds/Xcode 16.4/SwiftLogBenchmarks.error_log_with_debug_log_level.p90.json new file mode 100644 index 0000000..2371eb9 --- /dev/null +++ b/Benchmarks/Thresholds/Xcode 16.4/SwiftLogBenchmarks.error_log_with_debug_log_level.p90.json @@ -0,0 +1,4 @@ +{ + "instructions" : 3279, + "objectAllocCount" : 1 +} \ No newline at end of file diff --git a/Benchmarks/Thresholds/Xcode 16.4/SwiftLogBenchmarks.error_log_with_error_log_level.p90.json b/Benchmarks/Thresholds/Xcode 16.4/SwiftLogBenchmarks.error_log_with_error_log_level.p90.json new file mode 100644 index 0000000..2371eb9 --- /dev/null +++ b/Benchmarks/Thresholds/Xcode 16.4/SwiftLogBenchmarks.error_log_with_error_log_level.p90.json @@ -0,0 +1,4 @@ +{ + "instructions" : 3279, + "objectAllocCount" : 1 +} \ No newline at end of file diff --git a/Benchmarks/Thresholds/Xcode 26.1/SwiftLogBenchmarks.debug_log_with_debug_log_level.p90.json b/Benchmarks/Thresholds/Xcode 26.1/SwiftLogBenchmarks.debug_log_with_debug_log_level.p90.json new file mode 100644 index 0000000..3457187 --- /dev/null +++ b/Benchmarks/Thresholds/Xcode 26.1/SwiftLogBenchmarks.debug_log_with_debug_log_level.p90.json @@ -0,0 +1,4 @@ +{ + "instructions" : 3197, + "objectAllocCount" : 1 +} \ No newline at end of file diff --git a/Benchmarks/Thresholds/Xcode 26.1/SwiftLogBenchmarks.debug_log_with_error_log_level.p90.json b/Benchmarks/Thresholds/Xcode 26.1/SwiftLogBenchmarks.debug_log_with_error_log_level.p90.json new file mode 100644 index 0000000..cf2eff8 --- /dev/null +++ b/Benchmarks/Thresholds/Xcode 26.1/SwiftLogBenchmarks.debug_log_with_error_log_level.p90.json @@ -0,0 +1,4 @@ +{ + "instructions" : 1045, + "objectAllocCount" : 0 +} \ No newline at end of file diff --git a/Benchmarks/Thresholds/Xcode 26.1/SwiftLogBenchmarks.error_log_with_debug_log_level.p90.json b/Benchmarks/Thresholds/Xcode 26.1/SwiftLogBenchmarks.error_log_with_debug_log_level.p90.json new file mode 100644 index 0000000..3457187 --- /dev/null +++ b/Benchmarks/Thresholds/Xcode 26.1/SwiftLogBenchmarks.error_log_with_debug_log_level.p90.json @@ -0,0 +1,4 @@ +{ + "instructions" : 3197, + "objectAllocCount" : 1 +} \ No newline at end of file diff --git a/Benchmarks/Thresholds/Xcode 26.1/SwiftLogBenchmarks.error_log_with_error_log_level.p90.json b/Benchmarks/Thresholds/Xcode 26.1/SwiftLogBenchmarks.error_log_with_error_log_level.p90.json new file mode 100644 index 0000000..3457187 --- /dev/null +++ b/Benchmarks/Thresholds/Xcode 26.1/SwiftLogBenchmarks.error_log_with_error_log_level.p90.json @@ -0,0 +1,4 @@ +{ + "instructions" : 3197, + "objectAllocCount" : 1 +} \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f31d26b..b8f519b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -99,6 +99,20 @@ directory, for example: --action-offline-mode ``` +## Benchmarks + +Benchmarks for `swift-log` are in a separate Swift Package in the `Benchmarks` subfolder of this repository. +They use the [`package-benchmark`](https://github.com/ordo-one/package-benchmark) plugin. +Benchmarks depends on the [`jemalloc`](https://jemalloc.net) memory allocation library, which is used by `package-benchmark` to capture memory allocation statistics. +An installation guide can be found in the [Getting Started article](https://swiftpackageindex.com/ordo-one/package-benchmark/documentation/benchmark/gettingstarted#Installing-Prerequisites-and-Platform-Support) of `package-benchmark`. +Afterwards you can run the benchmarks from CLI by going to the `Benchmarks` subfolder (e.g. `cd Benchmarks`) and invoking: +``` +swift package benchmark +``` + +For more information please refer to `swift package benchmark --help` or the [documentation of `package-benchmark`](https://swiftpackageindex.com/ordo-one/package-benchmark/documentation/benchmark). + + ## How to contribute your work Please open a pull request at https://github.com/apple/swift-log. Make sure the CI passes, and then wait for code review.