UDP based allocation tests (#1496)

UDP based allocation tests (#1496)

Motivation:

With future work being considered in the UDP space it will be useful to have a baseline measure of allocations.

Modifications:

New allocation tests for UDP -
Ideas inspired by the TCP allocation tests.
Code a modified version of UDPEchoServer and UDPEchoClient examples.

Result:

There are UDP based allocation tests.

Co-authored-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
This commit is contained in:
authored and GitHub committed 2020-05-04 11:36:23 +01:00
1 parent c30cf9c50f
commit 9afac5ecda
6 files changed
+161

No files matched your search

@@ -136,3 +136,108 @@ func withAutoReleasePool<T>(_ execute: () throws -> T) rethrows -> T {
}
#endif
}
// MARK: UDP Shared
/// Repeatedly send, echo and receive a number of UDP requests.
/// Based on the UDPEchoClient/Server example.
enum UDPShared {
private final class EchoHandler: ChannelInboundHandler {
public typealias InboundIn = AddressedEnvelope<ByteBuffer>
public typealias OutboundOut = AddressedEnvelope<ByteBuffer>
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
// As we are not really interested getting notified on success or failure we just pass nil as promise to
// reduce allocations.
context.write(data, promise: nil)
}
public func channelReadComplete(context: ChannelHandlerContext) {
// As we are not really interested getting notified on success or failure we just pass nil as promise to
// reduce allocations.
context.flush()
}
public func errorCaught(context: ChannelHandlerContext, error: Error) {
// Errors should never happen.
fatalError("EchoHandler received errorCaught")
}
}
private final class EchoHandlerClient: ChannelInboundHandler {
public typealias InboundIn = AddressedEnvelope<ByteBuffer>
public typealias OutboundOut = AddressedEnvelope<ByteBuffer>
private var repetitionsRemaining: Int
private let remoteAddress: SocketAddress
init(remoteAddress: SocketAddress, numberOfRepetitions: Int) {
self.remoteAddress = remoteAddress
self.repetitionsRemaining = numberOfRepetitions
}
public func channelActive(context: ChannelHandlerContext) {
// Channel is available. It's time to send the message to the server to initialize the ping-pong sequence.
self.sendSomeDataIfDesiredOrClose(context: context)
}
private func sendSomeDataIfDesiredOrClose(context: ChannelHandlerContext) {
if repetitionsRemaining > 0 {
repetitionsRemaining -= 1
// Set the transmission data.
let line = "Something to send there and back again."
var buffer = context.channel.allocator.buffer(capacity: line.utf8.count)
buffer.writeString(line)
// Forward the data.
let envolope = AddressedEnvelope<ByteBuffer>(remoteAddress: remoteAddress, data: buffer)
context.writeAndFlush(self.wrapOutboundOut(envolope), promise: nil)
} else {
// We're all done - hurrah!
context.close(promise: nil)
}
}
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
// Got back a response - maybe send some more.
self.sendSomeDataIfDesiredOrClose(context: context)
}
public func errorCaught(context: ChannelHandlerContext, error: Error) {
// Errors should never happen.
fatalError("EchoHandlerClient received errorCaught")
}
}
static func doUDPRequests(group: EventLoopGroup, number numberOfRequests: Int) throws -> Int {
let serverChannel = try DatagramBootstrap(group: group)
.channelOption(ChannelOptions.socketOption(.reuseaddr), value: 1)
// Set the handlers that are applied to the bound channel
.channelInitializer { channel in
return channel.pipeline.addHandler(EchoHandler())
}
.bind(to: localhostPickPort).wait()
defer {
try! serverChannel.close().wait()
}
let remoteAddress = serverChannel.localAddress!
let clientChannel = try DatagramBootstrap(group: group)
// Enable SO_REUSEADDR.
.channelOption(ChannelOptions.socketOption(.reuseaddr), value: 1)
.channelInitializer { channel in
channel.pipeline.addHandler(EchoHandlerClient(remoteAddress: remoteAddress,
numberOfRepetitions: numberOfRequests))
}
.bind(to: localhostPickPort).wait()
// Will be closed after all the work is done and replies received.
try clientChannel.closeFuture.wait()
return numberOfRequests
}
}
@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2020 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
//
//===----------------------------------------------------------------------===//
import NIO
func run(identifier: String) {
measure(identifier: identifier) {
let numberDone = try! UDPShared.doUDPRequests(group: group, number: 1000)
precondition(numberDone == 1000)
return numberDone
}
}
@@ -0,0 +1,27 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2020 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
//
//===----------------------------------------------------------------------===//
import NIO
func run(identifier: String) {
measure(identifier: identifier) {
var numberDone = 1
for _ in 0..<1000 {
let newDones = try! UDPShared.doUDPRequests(group: group, number: 1)
precondition(newDones == 1)
numberDone += newDones
}
return numberDone
}
}
+2
View File
@@ -34,6 +34,8 @@ services:
- MAX_ALLOCS_ALLOWED_modifying_byte_buffer_view=2010 # 5.2 improvement 4000
- MAX_ALLOCS_ALLOWED_schedule_10000_tasks=90050
- MAX_ALLOCS_ALLOWED_read_10000_chunks_from_file=210050
- MAX_ALLOCS_ALLOWED_udp_1000_reqs_1_conn=16250
- MAX_ALLOCS_ALLOWED_udp_1_reqs_1000_conn=226050
- SANITIZER_ARG=--sanitize=thread
- INTEGRATION_TESTS_ARG=-f tests_0[013-9]
+2
View File
@@ -34,6 +34,8 @@ services:
- MAX_ALLOCS_ALLOWED_modifying_byte_buffer_view=6010
- MAX_ALLOCS_ALLOWED_schedule_10000_tasks=90050
- MAX_ALLOCS_ALLOWED_read_10000_chunks_from_file=230050
- MAX_ALLOCS_ALLOWED_udp_1000_reqs_1_conn=18250
- MAX_ALLOCS_ALLOWED_udp_1_reqs_1000_conn=235050
performance-test:
image: swift-nio:18.04-5.0
+2
View File
@@ -34,6 +34,8 @@ services:
- MAX_ALLOCS_ALLOWED_modifying_byte_buffer_view=6010
- MAX_ALLOCS_ALLOWED_schedule_10000_tasks=90050
- MAX_ALLOCS_ALLOWED_read_10000_chunks_from_file=210050
- MAX_ALLOCS_udp_1000_reqs_1_conn=16250
- MAX_ALLOCS_ALLOWED_udp_1_reqs_1000_conn=224050
performance-test:
image: swift-nio:18.04-5.1