-
Notifications
You must be signed in to change notification settings - Fork 699
Add NIOAsyncChannel
benchmark
#2536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
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() | ||
} | ||
} |
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) |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is pretty bad since we are allocating for every single |
||
} |
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 | ||
} |
There was a problem hiding this comment.
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?