Skip to content

Commit 01c85c5

Browse files
authored
Add basic benchmarks (#102)
### Motivation Fixes #28 to ensure we have a baseline before tagging the 1.0. ### Modifications Added benchmarks and actually made a few small tweaks to reduce allocations. ### Result Baseline for benchmarks, any perf changes will be deliberate. ### Test Plan Verified all locally.
1 parent 0767984 commit 01c85c5

13 files changed

+189
-4
lines changed

.github/workflows/main.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,12 @@ jobs:
122122
with:
123123
name: "Example packages"
124124
matrix_string: '${{ needs.construct-example-packages-matrix.outputs.example-packages-matrix }}'
125+
126+
benchmarks:
127+
name: Benchmarks
128+
uses: apple/swift-nio/.github/workflows/benchmarks.yml@main
129+
with:
130+
benchmark_package_path: "Benchmarks"
131+
linux_5_10_enabled: false
132+
linux_6_0_enabled: false
133+
linux_6_1_enabled: false

.github/workflows/pull_request.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,12 @@ jobs:
129129
with:
130130
name: "Example packages"
131131
matrix_string: '${{ needs.construct-example-packages-matrix.outputs.example-packages-matrix }}'
132+
133+
benchmarks:
134+
name: Benchmarks
135+
uses: apple/swift-nio/.github/workflows/benchmarks.yml@main
136+
with:
137+
benchmark_package_path: "Benchmarks"
138+
linux_5_10_enabled: false
139+
linux_6_0_enabled: false
140+
linux_6_1_enabled: false

Benchmarks/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc

Benchmarks/Package.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// swift-tools-version: 6.2
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "Benchmarks",
7+
platforms: [
8+
.macOS(.v15)
9+
],
10+
dependencies: [
11+
.package(path: "../"),
12+
.package(url: "https://github.com/ordo-one/package-benchmark.git", from: "1.29.6"),
13+
],
14+
targets: [
15+
.executableTarget(
16+
name: "Benchmarks",
17+
dependencies: [
18+
.product(name: "Benchmark", package: "package-benchmark"),
19+
.product(name: "Configuration", package: "swift-configuration"),
20+
],
21+
path: "Sources",
22+
plugins: [
23+
.plugin(name: "BenchmarkPlugin", package: "package-benchmark")
24+
]
25+
)
26+
]
27+
)
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftConfiguration open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the SwiftConfiguration project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftConfiguration project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import Benchmark
16+
import Configuration
17+
import Foundation
18+
19+
let benchmarks: @Sendable () -> Void = {
20+
let defaultMetrics: [BenchmarkMetric] = [
21+
.mallocCountTotal
22+
// .contextSwitches,
23+
// .wallClock,
24+
]
25+
26+
Benchmark(
27+
"ConfigReader_InMemoryProvider_notFound_string",
28+
configuration: Benchmark.Configuration(
29+
metrics: defaultMetrics,
30+
scalingFactor: .kilo
31+
)
32+
) { benchmark in
33+
let emptyReader = ConfigReader(provider: InMemoryProvider(values: [:]))
34+
for _ in benchmark.scaledIterations {
35+
blackHole(emptyReader.string(forKey: "foo"))
36+
}
37+
}
38+
39+
Benchmark(
40+
"ConfigReader_InMemoryProvider_found_string",
41+
configuration: Benchmark.Configuration(
42+
metrics: defaultMetrics,
43+
scalingFactor: .kilo
44+
)
45+
) { benchmark in
46+
let emptyReader = ConfigReader(provider: InMemoryProvider(values: ["foo": "bar"]))
47+
for _ in benchmark.scaledIterations {
48+
blackHole(emptyReader.string(forKey: "foo"))
49+
}
50+
}
51+
52+
Benchmark(
53+
"ConfigReader_InMemoryProvider_found_int",
54+
configuration: Benchmark.Configuration(
55+
metrics: defaultMetrics,
56+
scalingFactor: .kilo
57+
)
58+
) { benchmark in
59+
let emptyReader = ConfigReader(provider: InMemoryProvider(values: ["foo": 24]))
60+
for _ in benchmark.scaledIterations {
61+
blackHole(emptyReader.int(forKey: "foo"))
62+
}
63+
}
64+
65+
Benchmark(
66+
"ConfigReader_InMemoryProvider_found_fetchInt",
67+
configuration: Benchmark.Configuration(
68+
metrics: defaultMetrics,
69+
scalingFactor: .kilo
70+
)
71+
) { (benchmark) async throws -> Void in
72+
let emptyReader = ConfigReader(provider: InMemoryProvider(values: ["foo": 24]))
73+
for _ in benchmark.scaledIterations {
74+
blackHole(try await emptyReader.fetchInt(forKey: "foo"))
75+
}
76+
}
77+
78+
Benchmark(
79+
"EnvironmentVariablesProvider_notFound_string",
80+
configuration: Benchmark.Configuration(
81+
metrics: defaultMetrics,
82+
scalingFactor: .kilo
83+
)
84+
) { (benchmark) throws -> Void in
85+
let provider = EnvironmentVariablesProvider(environmentVariables: [:])
86+
let key: AbsoluteConfigKey = "foo"
87+
for _ in benchmark.scaledIterations {
88+
blackHole(try provider.value(forKey: key, type: .string))
89+
}
90+
}
91+
92+
Benchmark(
93+
"EnvironmentVariablesProvider_found_string",
94+
configuration: Benchmark.Configuration(
95+
metrics: defaultMetrics,
96+
scalingFactor: .kilo
97+
)
98+
) { (benchmark) throws -> Void in
99+
let provider = EnvironmentVariablesProvider(environmentVariables: ["foo": "bar"])
100+
let key: AbsoluteConfigKey = "foo"
101+
for _ in benchmark.scaledIterations {
102+
blackHole(try provider.value(forKey: key, type: .string))
103+
}
104+
}
105+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"mallocCountTotal" : 3009
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"mallocCountTotal" : 3008
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"mallocCountTotal" : 3008
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"mallocCountTotal" : 3005
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"mallocCountTotal" : 3005
3+
}

0 commit comments

Comments
 (0)