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
34 changes: 33 additions & 1 deletion Benchmarks/Benchmarks/NIOPosixBenchmarks/Benchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
//===----------------------------------------------------------------------===//

import Benchmark
import NIOPosix

private let eventLoop = MultiThreadedEventLoopGroup(numberOfThreads: 1).next()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just grab the next() from the shared singleton?


let benchmarks = {
let defaultMetrics: [BenchmarkMetric] = [
Expand All @@ -27,6 +30,35 @@ let benchmarks = {
scalingFactor: .mega
)
) { benchmark in
try runTCPEcho(numberOfWrites: benchmark.scaledIterations.upperBound)
try runTCPEcho(
numberOfWrites: benchmark.scaledIterations.upperBound,
eventLoop: eventLoop
)
}

// This benchmark is only available above 5.9 since our EL conformance
// to serial executor is also gated behind 5.9.
#if compiler(>=5.9)
Benchmark(
"TCPEchoAsyncChannel",
configuration: .init(
metrics: defaultMetrics,
timeUnits: .milliseconds,
scalingFactor: .mega,
setup: {
swiftTaskEnqueueGlobalHook = { job, _ in
eventLoop.executor.enqueue(job)
}
},
teardown: {
swiftTaskEnqueueGlobalHook = nil
}
)
) { benchmark in
try await runTCPEchoAsyncChannel(
numberOfWrites: benchmark.scaledIterations.upperBound,
eventLoop: eventLoop
)
}
#endif
}
3 changes: 1 addition & 2 deletions Benchmarks/Benchmarks/NIOPosixBenchmarks/TCPEcho.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ private final class EchoRequestChannelHandler: ChannelInboundHandler {
}
}

func runTCPEcho(numberOfWrites: Int) throws {
let eventLoop = MultiThreadedEventLoopGroup.singleton.next()
func runTCPEcho(numberOfWrites: Int, eventLoop: any EventLoop) throws {
let serverChannel = try ServerBootstrap(group: eventLoop)
.childChannelInitializer { channel in
channel.eventLoop.makeCompletedFuture {
Expand Down
89 changes: 89 additions & 0 deletions Benchmarks/Benchmarks/NIOPosixBenchmarks/TCPEchoAsyncChannel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

@_spi(AsyncChannel) import NIOCore
@_spi(AsyncChannel) import NIOPosix

func runTCPEchoAsyncChannel(numberOfWrites: Int, eventLoop: EventLoop) async throws {
let serverChannel = try await ServerBootstrap(group: eventLoop)
.bind(
host: "127.0.0.1",
port: 0
) { channel in
channel.eventLoop.makeCompletedFuture {
return try NIOAsyncChannel(
synchronouslyWrapping: channel,
configuration: .init(
inboundType: ByteBuffer.self,
outboundType: ByteBuffer.self
)
)
}
}

let clientChannel = try await ClientBootstrap(group: eventLoop)
.connect(
host: "127.0.0.1",
port: serverChannel.channel.localAddress!.port!
) { channel in
channel.eventLoop.makeCompletedFuture {
return try NIOAsyncChannel(
synchronouslyWrapping: channel,
configuration: .init(
inboundType: ByteBuffer.self,
outboundType: ByteBuffer.self
)
)
}
}

let bufferSize = 10000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: naming threw me for a sec as I expected a buffer, not a ByteBuffer. something like messageSize is a more obvious


try await withThrowingTaskGroup(of: Void.self) { group in
// This child task is echoing back the data on the server.
group.addTask {
for try await connectionChannel in serverChannel.inboundStream {
for try await inboundData in connectionChannel.inboundStream {
try await connectionChannel.outboundWriter.write(inboundData)
}
}
}

// This child task is collecting the echoed back responses.
group.addTask {
var receivedData = 0
for try await inboundData in clientChannel.inboundStream {
receivedData += inboundData.readableBytes

if receivedData == numberOfWrites * bufferSize {
return
}
}
}

// Let's start sending data.
let data = ByteBuffer(repeating: 0, count: bufferSize)
for _ in 0..<numberOfWrites {
try await clientChannel.outboundWriter.write(data)
}

// Waiting for the child task that collects the responses to finish.
try await group.next()

// Cancelling the server child task.
group.cancelAll()
try await serverChannel.channel.closeFuture.get()
try await clientChannel.channel.closeFuture.get()
}
}
33 changes: 33 additions & 0 deletions Benchmarks/Benchmarks/NIOPosixBenchmarks/Util/GlobalExecutor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

#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)
2 changes: 1 addition & 1 deletion Benchmarks/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import PackageDescription
let package = Package(
name: "benchmarks",
platforms: [
.macOS(.v13),
.macOS("14"),
],
dependencies: [
.package(path: "../"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 93
"mallocCountTotal" : 90
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 5554895
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 95
"mallocCountTotal" : 92
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 95
"mallocCountTotal" : 92
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 95
"mallocCountTotal" : 92
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 5636901
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty bad since we are allocating for every single enqueue now. I will raise one or more follow up PRs to reduce this significantly. I was able to remove all allocations except one in the runtime.

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 93
"mallocCountTotal" : 90
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 5554895
}