Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: benchmark
on:
workflow_dispatch:
inputs:
sha:
type: string
required: true
description: "The commit SHA to run the benchmarks against."
push:
branches: [main]

jobs:
benchmark:
uses: vapor/ci/.github/workflows/run-benchmark.yml@main
with:
sha: ${{ inputs.sha }}
secrets: inherit
1 change: 1 addition & 0 deletions Benchmarks/.gitignore
1 change: 1 addition & 0 deletions Benchmarks/.swift-format
52 changes: 52 additions & 0 deletions Benchmarks/ConsoleLoggerBenchmarks/ConsoleLoggerBenchmarks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Benchmark
import ConsoleKit
import ConsoleLogger
import Logging

let benchmarks: @Sendable () -> Void = {
Benchmark.defaultConfiguration = .init(
metrics: [.peakMemoryResident, .mallocCountTotal],
thresholds: [
.peakMemoryResident: .init(
// Tolerate up to 4% of difference compared to the threshold.
relative: [.p90: 4],
// Tolerate up to one million bytes of difference compared to the threshold.
absolute: [.p90: 1_100_000]
),
.mallocCountTotal: .init(
// Tolerate up to 1% of difference compared to the threshold.
relative: [.p90: 1],
// Tolerate up to 2 malloc calls of difference compared to the threshold.
absolute: [.p90: 2]
),
]
)

Benchmark(
"Logging",
configuration: .init(
setup: {
let console = TestConsole()
LoggingSystem.bootstrap(
{ label, provider in
ConsoleLogger(label: label, console: console)
},
metadataProvider: .init {
["provided1": "from metadata provider", "provided2": "another metadata provider"]
}
)
}
)
) { benchmark in
var logger = Logger(label: "codes.vapor.console")
logger.logLevel = .trace
logger[metadataKey: "value"] = "one"

for _ in benchmark.scaledIterations {
logger.info(
"Info",
metadata: ["from-log": "value", "also-from-log": "other"]
)
}
}
}
47 changes: 47 additions & 0 deletions Benchmarks/ConsoleLoggerBenchmarks/TestConsole.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import ConsoleKit
import Synchronization

/// A test console that captures input and output for testing purposes.
///
/// > Warning: This class is a duplicate of the one in the main package,
/// remember to update all when making changes.
final class TestConsole: Console {
let _testInputQueue: Mutex<[String]> = Mutex([])

var testInputQueue: [String] {
get { self._testInputQueue.withLock { $0 } }
set { self._testInputQueue.withLock { $0 = newValue } }
}

let _testOutputQueue: Mutex<[String]> = Mutex([])
var testOutputQueue: [String] {
get { self._testOutputQueue.withLock { $0 } }
set { self._testOutputQueue.withLock { $0 = newValue } }
}

let _userInfo: Mutex<[AnySendableHashable: any Sendable]> = Mutex([:])
var userInfo: [AnySendableHashable: any Sendable] {
get { self._userInfo.withLock { $0 } }
set { self._userInfo.withLock { $0 = newValue } }
}

init() {
self.testInputQueue = []
self.testOutputQueue = []
self.userInfo = [:]
}

func input(isSecure: Bool) -> String {
return testInputQueue.popLast() ?? ""
}

func output(_ text: ConsoleText, newLine: Bool) {
testOutputQueue.insert(text.description + (newLine ? "\n" : ""), at: 0)
}

func report(error: String, newLine: Bool) {}

func clear(_ type: ConsoleClear) {}

var size: (width: Int, height: Int) { (width: 32, height: 0) }
}
42 changes: 42 additions & 0 deletions Benchmarks/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// swift-tools-version:6.1
import PackageDescription

let package = Package(
name: "benchmarks",
platforms: [
.macOS(.v15)
],
dependencies: [
.package(path: "../"),
.package(url: "https://github.com/ordo-one/package-benchmark.git", from: "1.29.2"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.6.3"),
],
targets: [
.executableTarget(
name: "ConsoleLoggerBenchmarks",
dependencies: [
.product(name: "Benchmark", package: "package-benchmark"),
.product(name: "ConsoleLogger", package: "console-kit"),
.product(name: "ConsoleKit", package: "console-kit"),
.product(name: "Logging", package: "swift-log"),
],
path: "ConsoleLoggerBenchmarks",
swiftSettings: swiftSettings,
plugins: [
.plugin(name: "BenchmarkPlugin", package: "package-benchmark")
]
)
]
)

var swiftSettings: [SwiftSetting] {
[
.enableUpcomingFeature("ExistentialAny"),
.enableUpcomingFeature("MemberImportVisibility"),
.enableUpcomingFeature("ExistentialAny"),
.enableUpcomingFeature("InternalImportsByDefault"),
.enableUpcomingFeature("MemberImportVisibility"),
.enableUpcomingFeature("InferIsolatedConformances"),
.enableUpcomingFeature("ImmutableWeakCaptures"),
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"mallocCountTotal" : 48,
"peakMemoryResident" : 13279231
}
Loading