Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions Benchmarks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
.benchmarkBaselines/
119 changes: 119 additions & 0 deletions Benchmarks/Benchmarks/NIOAsyncRuntimeBenchmarks/Benchmarks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//===----------------------------------------------------------------------===//
//
// Copyright (c) 2025 PassiveLogic, Inc.
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

// NOTE: By and large the benchmarks here were ported from swift-nio
// to allow side-by-side performance comparison
//
// See https://github.com/apple/swift-nio/blob/main/Benchmarks/Benchmarks/NIOPosixBenchmarks/Benchmarks.swift

import Benchmark
import NIOAsyncRuntime
import NIOCore

private let eventLoop = MultiThreadedEventLoopGroup.singleton.next()

let benchmarks = {
let defaultMetrics: [BenchmarkMetric] = [
.mallocCountTotal,
.contextSwitches,
.wallClock,
]

Benchmark(
"MTELG.immediateTasksThroughput",
configuration: Benchmark.Configuration(
metrics: defaultMetrics,
scalingFactor: .mega,
maxDuration: .seconds(10_000_000),
maxIterations: 5
)
) { benchmark in
func noOp() {}
for _ in benchmark.scaledIterations {
eventLoop.execute { noOp() }
}
}

Benchmark(
"MTELG.scheduleTask(in:_:)",
configuration: Benchmark.Configuration(
metrics: defaultMetrics,
scalingFactor: .mega,
maxDuration: .seconds(10_000_000),
maxIterations: 5
)
) { benchmark in
for _ in benchmark.scaledIterations {
eventLoop.scheduleTask(in: .hours(1), {})
}
}

Benchmark(
"MTELG.scheduleCallback(in:_:)",
configuration: Benchmark.Configuration(
metrics: defaultMetrics,
scalingFactor: .mega,
maxDuration: .seconds(10_000_000),
maxIterations: 5
)
) { benchmark in
final class Timer: NIOScheduledCallbackHandler {
func handleScheduledCallback(eventLoop: some EventLoop) {}
}
let timer = Timer()

benchmark.startMeasurement()
for _ in benchmark.scaledIterations {
let handle = try! eventLoop.scheduleCallback(in: .hours(1), handler: timer)
}
}

Benchmark(
"Jump to EL and back using execute and unsafecontinuation",
configuration: .init(
metrics: defaultMetrics,
scalingFactor: .kilo
)
) { benchmark in
for _ in benchmark.scaledIterations {
await withUnsafeContinuation { (continuation: UnsafeContinuation<Void, Never>) in
eventLoop.execute {
continuation.resume()
}
}
}
}

final actor Foo {
nonisolated public let unownedExecutor: UnownedSerialExecutor

init(eventLoop: any EventLoop) {
self.unownedExecutor = eventLoop.executor.asUnownedSerialExecutor()
}

func foo() {
blackHole(Void())
}
}

Benchmark(
"Jump to EL and back using actor with EL executor",
configuration: .init(
metrics: defaultMetrics,
scalingFactor: .kilo
)
) { benchmark in
let actor = Foo(eventLoop: eventLoop)
for _ in benchmark.scaledIterations {
await actor.foo()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===----------------------------------------------------------------------===//
//
// Copyright (c) 2025 PassiveLogic, Inc.
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

// NOTE: By and large the benchmarks here were ported from swift-nio
// to allow side-by-side performance comparison
//
// See https://github.com/apple/swift-nio/blob/main/Benchmarks/Benchmarks/NIOPosixBenchmarks/Benchmarks.swift

#if canImport(Darwin)
import Darwin.C
#elseif canImport(Glibc)
import Glibc
#else
#error("Unsupported platform.")
#endif

// This file allows us to hook the global executor which
// we can use to mimic task executors for now.
typealias EnqueueGlobalHook =
@convention(thin) (UnownedJob, @convention(thin) (UnownedJob) -> Void) -> Void

var swiftTaskEnqueueGlobalHook: EnqueueGlobalHook? {
get { _swiftTaskEnqueueGlobalHook.pointee }
set { _swiftTaskEnqueueGlobalHook.pointee = newValue }
}

private let _swiftTaskEnqueueGlobalHook: UnsafeMutablePointer<EnqueueGlobalHook?> =
dlsym(dlopen(nil, RTLD_LAZY), "swift_task_enqueueGlobal_hook").assumingMemoryBound(
to: EnqueueGlobalHook?.self)
81 changes: 81 additions & 0 deletions Benchmarks/Benchmarks/NIOCoreBenchmarks/Benchmarks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//===----------------------------------------------------------------------===//
//
// Copyright (c) 2025 PassiveLogic, Inc.
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

// NOTE: By and large the benchmarks here were ported from swift-nio
// to allow side-by-side performance comparison
//
// See https://github.com/apple/swift-nio/blob/main/Benchmarks/Benchmarks/NIOPosixBenchmarks/Benchmarks.swift

import Benchmark
import NIOCore
import NIOEmbedded

let benchmarks = {
let defaultMetrics: [BenchmarkMetric] = [
.mallocCountTotal
]

let leakMetrics: [BenchmarkMetric] = [
.mallocCountTotal,
.memoryLeaked,
]

Benchmark(
"NIOAsyncChannel.init",
configuration: .init(
metrics: defaultMetrics,
scalingFactor: .kilo,
maxDuration: .seconds(10_000_000),
maxIterations: 10
)
) { benchmark in
// Elide the cost of the 'EmbeddedChannel'. It's only used for its pipeline.
var channels: [EmbeddedChannel] = []
channels.reserveCapacity(benchmark.scaledIterations.count)
for _ in 0..<benchmark.scaledIterations.count {
channels.append(EmbeddedChannel())
}

benchmark.startMeasurement()
defer {
benchmark.stopMeasurement()
}
for channel in channels {
let asyncChanel = try NIOAsyncChannel<ByteBuffer, ByteBuffer>(
wrappingChannelSynchronously: channel)
blackHole(asyncChanel)
}
}

Benchmark(
"WaitOnPromise",
configuration: .init(
metrics: leakMetrics,
scalingFactor: .kilo,
maxDuration: .seconds(10_000_000),
maxIterations: 10_000 // need 10k to get a signal
)
) { benchmark in
// Elide the cost of the 'EmbeddedEventLoop'.
let el = EmbeddedEventLoop()

benchmark.startMeasurement()
defer {
benchmark.stopMeasurement()
}

for _ in 0..<benchmark.scaledIterations.count {
let p = el.makePromise(of: Int.self)
p.succeed(0)
do { _ = try! p.futureResult.wait() }
}
}
}
96 changes: 96 additions & 0 deletions Benchmarks/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions Benchmarks/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// swift-tools-version:5.10

//===----------------------------------------------------------------------===//
//
// Copyright (c) 2025 PassiveLogic, Inc.
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import PackageDescription

let package = Package(
name: "benchmarks",
platforms: [
.macOS("14")
],
dependencies: [
.package(path: "../"),
.package(url: "https://github.com/ordo-one/package-benchmark.git", from: "1.22.0"),
],
targets: [
.executableTarget(
name: "NIOAsyncRuntimeBenchmarks",
dependencies: [
.product(name: "Benchmark", package: "package-benchmark"),
.product(name: "NIOAsyncRuntime", package: "nio-async-runtime"),
],
path: "Benchmarks/NIOAsyncRuntimeBenchmarks",
plugins: [
.plugin(name: "BenchmarkPlugin", package: "package-benchmark")
]
)
]
)
3 changes: 3 additions & 0 deletions Sources/NIOAsyncRuntime/AsyncEventLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,6 @@ public final class AsyncEventLoop: EventLoop, @unchecked Sendable {
init(_ value: T) { self.value = value }
}
}

@available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
extension AsyncEventLoop: NIOSerialEventLoopExecutor {}