Skip to content

Commit 1be1d76

Browse files
authored
Merge branch 'consolekit-5' into logger-fragment-builder
2 parents 73c6d4a + 8252efa commit 1be1d76

7 files changed

Lines changed: 173 additions & 0 deletions

File tree

.github/workflows/benchmark.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: benchmark
2+
on:
3+
workflow_dispatch:
4+
inputs:
5+
sha:
6+
type: string
7+
required: true
8+
description: "The commit SHA to run the benchmarks against."
9+
push:
10+
branches: [main]
11+
12+
jobs:
13+
benchmark:
14+
uses: vapor/ci/.github/workflows/run-benchmark.yml@main
15+
with:
16+
sha: ${{ inputs.sha }}
17+
secrets: inherit

Benchmarks/.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.build
2+
Packages
3+
*.xcodeproj
4+
Package.pins
5+
Package.resolved
6+
.DS_Store
7+
DerivedData
8+
.swiftpm
9+
Tests/LinuxMain.swift
10+
.vscode

Benchmarks/.swift-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../.swift-format
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import Benchmark
2+
import ConsoleKit
3+
import ConsoleLogger
4+
import Logging
5+
6+
let benchmarks: @Sendable () -> Void = {
7+
Benchmark.defaultConfiguration = .init(
8+
metrics: [.peakMemoryResident, .mallocCountTotal],
9+
thresholds: [
10+
.peakMemoryResident: .init(
11+
// Tolerate up to 4% of difference compared to the threshold.
12+
relative: [.p90: 4],
13+
// Tolerate up to one million bytes of difference compared to the threshold.
14+
absolute: [.p90: 1_100_000]
15+
),
16+
.mallocCountTotal: .init(
17+
// Tolerate up to 1% of difference compared to the threshold.
18+
relative: [.p90: 1],
19+
// Tolerate up to 2 malloc calls of difference compared to the threshold.
20+
absolute: [.p90: 2]
21+
),
22+
]
23+
)
24+
25+
Benchmark(
26+
"Logging",
27+
configuration: .init(
28+
setup: {
29+
let console = TestConsole()
30+
LoggingSystem.bootstrap(
31+
{ label, provider in
32+
ConsoleLogger(label: label, console: console)
33+
},
34+
metadataProvider: .init {
35+
["provided1": "from metadata provider", "provided2": "another metadata provider"]
36+
}
37+
)
38+
}
39+
)
40+
) { benchmark in
41+
var logger = Logger(label: "codes.vapor.console")
42+
logger.logLevel = .trace
43+
logger[metadataKey: "value"] = "one"
44+
45+
for _ in benchmark.scaledIterations {
46+
logger.info(
47+
"Info",
48+
metadata: ["from-log": "value", "also-from-log": "other"]
49+
)
50+
}
51+
}
52+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import ConsoleKit
2+
import Synchronization
3+
4+
/// A test console that captures input and output for testing purposes.
5+
///
6+
/// > Warning: This class is a duplicate of the one in the main package,
7+
/// remember to update all when making changes.
8+
final class TestConsole: Console {
9+
let _testInputQueue: Mutex<[String]> = Mutex([])
10+
11+
var testInputQueue: [String] {
12+
get { self._testInputQueue.withLock { $0 } }
13+
set { self._testInputQueue.withLock { $0 = newValue } }
14+
}
15+
16+
let _testOutputQueue: Mutex<[String]> = Mutex([])
17+
var testOutputQueue: [String] {
18+
get { self._testOutputQueue.withLock { $0 } }
19+
set { self._testOutputQueue.withLock { $0 = newValue } }
20+
}
21+
22+
let _userInfo: Mutex<[AnySendableHashable: any Sendable]> = Mutex([:])
23+
var userInfo: [AnySendableHashable: any Sendable] {
24+
get { self._userInfo.withLock { $0 } }
25+
set { self._userInfo.withLock { $0 = newValue } }
26+
}
27+
28+
init() {
29+
self.testInputQueue = []
30+
self.testOutputQueue = []
31+
self.userInfo = [:]
32+
}
33+
34+
func input(isSecure: Bool) -> String {
35+
return testInputQueue.popLast() ?? ""
36+
}
37+
38+
func output(_ text: ConsoleText, newLine: Bool) {
39+
testOutputQueue.insert(text.description + (newLine ? "\n" : ""), at: 0)
40+
}
41+
42+
func report(error: String, newLine: Bool) {}
43+
44+
func clear(_ type: ConsoleClear) {}
45+
46+
var size: (width: Int, height: Int) { (width: 32, height: 0) }
47+
}

Benchmarks/Package.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// swift-tools-version:6.1
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "benchmarks",
6+
platforms: [
7+
.macOS(.v15)
8+
],
9+
dependencies: [
10+
.package(path: "../"),
11+
.package(url: "https://github.com/ordo-one/package-benchmark.git", from: "1.29.2"),
12+
.package(url: "https://github.com/apple/swift-log.git", from: "1.6.3"),
13+
],
14+
targets: [
15+
.executableTarget(
16+
name: "ConsoleLoggerBenchmarks",
17+
dependencies: [
18+
.product(name: "Benchmark", package: "package-benchmark"),
19+
.product(name: "ConsoleLogger", package: "console-kit"),
20+
.product(name: "ConsoleKit", package: "console-kit"),
21+
.product(name: "Logging", package: "swift-log"),
22+
],
23+
path: "ConsoleLoggerBenchmarks",
24+
swiftSettings: swiftSettings,
25+
plugins: [
26+
.plugin(name: "BenchmarkPlugin", package: "package-benchmark")
27+
]
28+
)
29+
]
30+
)
31+
32+
var swiftSettings: [SwiftSetting] {
33+
[
34+
.enableUpcomingFeature("ExistentialAny"),
35+
.enableUpcomingFeature("MemberImportVisibility"),
36+
.enableUpcomingFeature("ExistentialAny"),
37+
.enableUpcomingFeature("InternalImportsByDefault"),
38+
.enableUpcomingFeature("MemberImportVisibility"),
39+
.enableUpcomingFeature("InferIsolatedConformances"),
40+
.enableUpcomingFeature("ImmutableWeakCaptures"),
41+
]
42+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"mallocCountTotal" : 48,
3+
"peakMemoryResident" : 13279231
4+
}

0 commit comments

Comments
 (0)