From 0175486175bbc6b76bfa414622da73c567e500b7 Mon Sep 17 00:00:00 2001 From: Franz Busch Date: Wed, 21 Oct 2026 13:41:23 +0200 Subject: [PATCH] [IO] Add UDP benchmarks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two new benchmarks for UDP: - `UDPPingPong` measures one round trip of a datagram. - `UDPBurst` sends 32 datagrams, then waits for one reply. ``` ┌──────────────────────────────────────────────────────┬────────────────┬───────────────────┬────────────┬───────────┬───────────────┐ │ │ Ping-pong wall │ Ping-pong mallocs │ Burst wall │ Burst CPU │ Burst mallocs │ ├──────────────────────────────────────────────────────┼────────────────┼───────────────────┼────────────┼───────────┼───────────────┤ │ swift-platform-executors, epoll │ 4.4 µs │ 0 │ 39.9 µs │ 40 µs │ 0 │ ├──────────────────────────────────────────────────────┼────────────────┼───────────────────┼────────────┼───────────┼───────────────┤ │ swift-platform-executors, io_uring │ 4.2 µs │ 0 │ 61.1 µs │ 60 µs │ 0 │ ├──────────────────────────────────────────────────────┼────────────────┼───────────────────┼────────────┼───────────┼───────────────┤ │ NIO, handlers on the event loop │ 4.2 µs │ 2.0 │ 34.4 µs │ 30 µs │ 35 │ ├──────────────────────────────────────────────────────┼────────────────┼───────────────────┼────────────┼───────────┼───────────────┤ │ NIO, NIOAsyncChannel, tasks on the global executor │ 81.2 µs │ 8.1 │ 209 µs │ 210 µs │ 118 │ ├──────────────────────────────────────────────────────┼────────────────┼───────────────────┼────────────┼───────────┼───────────────┤ │ NIO, NIOAsyncChannel, tasks pinned to the event loop │ 12.7 µs │ 2.1 │ 131 µs │ 130 µs │ 35 │ └──────────────────────────────────────────────────────┴────────────────┴───────────────────┴────────────┴───────────┴───────────────┘ ``` --- .../Benchmarks/IOBenchmarks/Benchmarks.swift | 39 ++++++++ .../Benchmarks/IOBenchmarks/UDPBurst.swift | 91 +++++++++++++++++++ .../Benchmarks/IOBenchmarks/UDPPingPong.swift | 91 +++++++++++++++++++ 3 files changed, 221 insertions(+) create mode 100644 Benchmarks/Benchmarks/IOBenchmarks/UDPBurst.swift create mode 100644 Benchmarks/Benchmarks/IOBenchmarks/UDPPingPong.swift diff --git a/Benchmarks/Benchmarks/IOBenchmarks/Benchmarks.swift b/Benchmarks/Benchmarks/IOBenchmarks/Benchmarks.swift index aee832c..4a8edf4 100644 --- a/Benchmarks/Benchmarks/IOBenchmarks/Benchmarks.swift +++ b/Benchmarks/Benchmarks/IOBenchmarks/Benchmarks.swift @@ -107,6 +107,45 @@ let benchmarks: @Sendable () -> Void = { ) } + ioBenchmark( + "UDPPingPong", + configuration: .init( + metrics: defaultMetrics, + scalingFactor: .kilo, + maxDuration: .seconds(10_000_000), + maxIterations: 5 + ) + ) { benchmark, ioBackend in + guard #available(anyAppleOS 27.0, *) else { + fatalError("The UDP ping pong benchmark requires the I/O APIs.") + } + try await runUDPPingPong( + numberOfDatagrams: benchmark.scaledIterations.upperBound, + ioBackend: ioBackend, + benchmark: benchmark + ) + } + + // Every iteration is a burst of 32 datagrams. + ioBenchmark( + "UDPBurst", + configuration: .init( + metrics: defaultMetrics, + scalingFactor: .kilo, + maxDuration: .seconds(10_000_000), + maxIterations: 5 + ) + ) { benchmark, ioBackend in + guard #available(anyAppleOS 27.0, *) else { + fatalError("The UDP burst benchmark requires the I/O APIs.") + } + try await runUDPBurst( + numberOfBursts: benchmark.scaledIterations.upperBound, + ioBackend: ioBackend, + benchmark: benchmark + ) + } + ioBenchmark( "TCPEcho", configuration: .init( diff --git a/Benchmarks/Benchmarks/IOBenchmarks/UDPBurst.swift b/Benchmarks/Benchmarks/IOBenchmarks/UDPBurst.swift new file mode 100644 index 0000000..0c6fe12 --- /dev/null +++ b/Benchmarks/Benchmarks/IOBenchmarks/UDPBurst.swift @@ -0,0 +1,91 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2026 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +import Benchmark +import PlatformExecutors + +/// The size of every datagram. +private let datagramSize = 512 + +/// The number of datagrams that the client sends before it waits for the server to acknowledge them. +private let burstSize = 32 + +/// Sends bursts of datagrams from a client to a server over the loopback interface. +/// +/// - Parameters: +/// - numberOfBursts: The number of bursts that the client sends. +/// - ioBackend: The I/O backend that the executor's threads use. +/// - benchmark: The benchmark to measure. +@available(anyAppleOS 27.0, *) +func runUDPBurst(numberOfBursts: Int, ioBackend: PThreadIOBackend, benchmark: Benchmark) async throws { + try await PThreadTaskExecutor.withExecutor(name: "UDPBurst", poolSize: 1, ioBackend: ioBackend) { executor in + try await withTaskExecutorPreference(executor) { + benchmark.startMeasurement() + defer { + benchmark.stopMeasurement() + } + + try await withThrowingTaskGroup { group in + var server = try await UDPSocket.bind(to: .v4(SocketAddress.V4(address: .loopback, port: 0))) + let address = try server.localAddress + group.addTask { + try await runClient(connectingTo: address, numberOfBursts: numberOfBursts) + } + + try await runServer(socket: &server, numberOfBursts: numberOfBursts) + try await server.close() + + try await group.waitForAll() + } + } + } +} + +@available(anyAppleOS 27.0, *) +private func runClient(connectingTo address: SocketAddress, numberOfBursts: Int) async throws { + var connection = try await UDPConnection.connect(to: address) + + let datagram = [UInt8](repeating: 0, count: datagramSize) + let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: datagramSize, alignment: 1) + defer { + buffer.deallocate() + } + + for _ in 0..