diff --git a/IntegrationTests/tests_04_performance/test_01_resources/shared.swift b/IntegrationTests/tests_04_performance/test_01_resources/shared.swift index 32e31d534..50267593b 100644 --- a/IntegrationTests/tests_04_performance/test_01_resources/shared.swift +++ b/IntegrationTests/tests_04_performance/test_01_resources/shared.swift @@ -136,3 +136,108 @@ func withAutoReleasePool(_ 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 + public typealias OutboundOut = AddressedEnvelope + + 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 + public typealias OutboundOut = AddressedEnvelope + 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(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 + } + +} diff --git a/IntegrationTests/tests_04_performance/test_01_resources/test_udp_1000_reqs_1_conn.swift b/IntegrationTests/tests_04_performance/test_01_resources/test_udp_1000_reqs_1_conn.swift new file mode 100644 index 000000000..a5254af45 --- /dev/null +++ b/IntegrationTests/tests_04_performance/test_01_resources/test_udp_1000_reqs_1_conn.swift @@ -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 + } +} diff --git a/IntegrationTests/tests_04_performance/test_01_resources/test_udp_1_reqs_1000_conn.swift b/IntegrationTests/tests_04_performance/test_01_resources/test_udp_1_reqs_1000_conn.swift new file mode 100644 index 000000000..295ddc62e --- /dev/null +++ b/IntegrationTests/tests_04_performance/test_01_resources/test_udp_1_reqs_1000_conn.swift @@ -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 + } +} diff --git a/docker/docker-compose.1604.52.yaml b/docker/docker-compose.1604.52.yaml index 2dabf9bc2..742761aae 100644 --- a/docker/docker-compose.1604.52.yaml +++ b/docker/docker-compose.1604.52.yaml @@ -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] diff --git a/docker/docker-compose.1804.50.yaml b/docker/docker-compose.1804.50.yaml index 9af563708..d29cecfdd 100644 --- a/docker/docker-compose.1804.50.yaml +++ b/docker/docker-compose.1804.50.yaml @@ -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 diff --git a/docker/docker-compose.1804.51.yaml b/docker/docker-compose.1804.51.yaml index 66ffa32d1..ed8461385 100644 --- a/docker/docker-compose.1804.51.yaml +++ b/docker/docker-compose.1804.51.yaml @@ -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