mirror of
https://github.com/apple/swift-log
synced 2026-11-22 03:40:39 +00:00
Logging instructions and allocations benchmarks (#386)
Add simple benchmarks measuring instructions count and allocations when logging with various log levels. ### Motivation: Establish the baseline and have automated checks before approaching performance improvements. ### Modifications: - Dependency on https://github.com/ordo-one/package-benchmark added - A new `Benchmarks` sub-package is added - CI workflows now run benchmarks ### Result: - CI workflows are running benchmarks on Linux and macOS - Thresholds are recorded and verified only on macOS An example of a benchmark failing with a **better** results than expected: https://github.com/apple/swift-log/actions/runs/19540944353/job/55950355442 An example of a benchmark failing with a **worse** results than expected: https://github.com/apple/swift-log/actions/runs/19569105729/job/56038101552
This commit is contained in:
1 parent
dea0abb603
commit
95a70b954c
15 files changed
+239
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
.DS_Store
|
||||
/.build
|
||||
/Packages
|
||||
xcuserdata/
|
||||
DerivedData/
|
||||
.swiftpm/configuration/registries.json
|
||||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
|
||||
.netrc
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
]
|
||||
)
|
||||
]
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"instructions" : 3279,
|
||||
"objectAllocCount" : 1
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"instructions" : 1126,
|
||||
"objectAllocCount" : 0
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"instructions" : 3279,
|
||||
"objectAllocCount" : 1
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"instructions" : 3279,
|
||||
"objectAllocCount" : 1
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"instructions" : 3197,
|
||||
"objectAllocCount" : 1
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"instructions" : 1045,
|
||||
"objectAllocCount" : 0
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"instructions" : 3197,
|
||||
"objectAllocCount" : 1
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"instructions" : 3197,
|
||||
"objectAllocCount" : 1
|
||||
}
|
||||
@@ -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.
|
||||
Reference in new issue
Block a user