From 2bc6627d74bb2c37c8c8253b81bd1e95066b0e32 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Mon, 1 Dec 2025 13:40:11 +0000 Subject: [PATCH] Add some benchmarks for UDP and UDP metadata (#3457) Motivation: I'm unhappy with AddressedEnvelope.Metadata, and I plan to change it. Before I change it, I want some way to get a clear picture of whether my changes make things better or worse. Modifications: Added a bunch of benchmarks Result: Some data will be available. --------- Co-authored-by: George Barnett --- .../NIOCoreBenchmarks/Benchmarks.swift | 165 ++++++++++++++++++ .../NIOPosixBenchmarks/Benchmarks.swift | 30 ++++ .../NIOPosixBenchmarks/UDPEcho.swift | 151 ++++++++++++++++ 3 files changed, 346 insertions(+) create mode 100644 Benchmarks/Benchmarks/NIOPosixBenchmarks/UDPEcho.swift diff --git a/Benchmarks/Benchmarks/NIOCoreBenchmarks/Benchmarks.swift b/Benchmarks/Benchmarks/NIOCoreBenchmarks/Benchmarks.swift index c821b872d..ca70e9fb3 100644 --- a/Benchmarks/Benchmarks/NIOCoreBenchmarks/Benchmarks.swift +++ b/Benchmarks/Benchmarks/NIOCoreBenchmarks/Benchmarks.swift @@ -16,10 +16,39 @@ import Benchmark import NIOCore import NIOEmbedded +// MARK: - Handlers for AddressedEnvelope benchmarks + +private final class ByteBufferEnvelopeForwardingHandler: ChannelInboundHandler, Sendable { + typealias InboundIn = AddressedEnvelope + + func channelRead(context: ChannelHandlerContext, data: NIOAny) { + context.fireChannelRead(data) + } +} + +private final class StringEnvelopeForwardingHandler: ChannelInboundHandler, Sendable { + typealias InboundIn = AddressedEnvelope + + func channelRead(context: ChannelHandlerContext, data: NIOAny) { + context.fireChannelRead(data) + } +} + +// MARK: - Benchmarks + let benchmarks = { + #if LOCAL_TESTING + let defaultMetrics: [BenchmarkMetric] = [ + .mallocCountTotal, + .contextSwitches, + .wallClock, + .instructions, + ] + #else let defaultMetrics: [BenchmarkMetric] = [ .mallocCountTotal ] + #endif let leakMetrics: [BenchmarkMetric] = [ .mallocCountTotal, @@ -75,4 +104,140 @@ let benchmarks = { do { _ = try! p.futureResult.wait() } } } + + Benchmark( + "AddressedEnvelope.ByteBuffer.noMetadata", + configuration: .init( + metrics: defaultMetrics, + scalingFactor: .kilo, + maxDuration: .seconds(10_000_000), + maxIterations: 10 + ) + ) { benchmark in + // Setup: Create channel with 20 forwarding handlers + let channel = EmbeddedChannel() + for _ in 0..<20 { + try! channel.pipeline.syncOperations.addHandler(ByteBufferEnvelopeForwardingHandler()) + } + + // Create the envelope without metadata + let address = try! SocketAddress(ipAddress: "::1", port: 8080) + let buffer = ByteBuffer(string: "test data") + let envelope = AddressedEnvelope(remoteAddress: address, data: buffer) + + benchmark.startMeasurement() + defer { + benchmark.stopMeasurement() + } + + for _ in 0..? = try! channel.readInbound() + blackHole(result) + } + } + + Benchmark( + "AddressedEnvelope.ByteBuffer.withMetadata", + configuration: .init( + metrics: defaultMetrics, + scalingFactor: .kilo, + maxDuration: .seconds(10_000_000), + maxIterations: 10 + ) + ) { benchmark in + // Setup: Create channel with 20 forwarding handlers + let channel = EmbeddedChannel() + for _ in 0..<20 { + try! channel.pipeline.syncOperations.addHandler(ByteBufferEnvelopeForwardingHandler()) + } + + // Create the envelope with full metadata + let address = try! SocketAddress(ipAddress: "::1", port: 8080) + let buffer = ByteBuffer(string: "test data") + let metadata = AddressedEnvelope.Metadata( + ecnState: .transportNotCapable, + packetInfo: NIOPacketInfo(destinationAddress: address, interfaceIndex: 1), + segmentSize: 1200 + ) + let envelope = AddressedEnvelope(remoteAddress: address, data: buffer, metadata: metadata) + + benchmark.startMeasurement() + defer { + benchmark.stopMeasurement() + } + + for _ in 0..? = try! channel.readInbound() + blackHole(result) + } + } + + Benchmark( + "AddressedEnvelope.String.noMetadata", + configuration: .init( + metrics: defaultMetrics, + scalingFactor: .kilo, + maxDuration: .seconds(10_000_000), + maxIterations: 10 + ) + ) { benchmark in + // Setup: Create channel with 20 forwarding handlers + let channel = EmbeddedChannel() + for _ in 0..<20 { + try! channel.pipeline.syncOperations.addHandler(StringEnvelopeForwardingHandler()) + } + + // Create the envelope without metadata + let address = try! SocketAddress(ipAddress: "::1", port: 8080) + let envelope = AddressedEnvelope(remoteAddress: address, data: "test data") + + benchmark.startMeasurement() + defer { + benchmark.stopMeasurement() + } + + for _ in 0..? = try! channel.readInbound() + blackHole(result) + } + } + + Benchmark( + "AddressedEnvelope.String.withMetadata", + configuration: .init( + metrics: defaultMetrics, + scalingFactor: .kilo, + maxDuration: .seconds(10_000_000), + maxIterations: 10 + ) + ) { benchmark in + // Setup: Create channel with 20 forwarding handlers + let channel = EmbeddedChannel() + for _ in 0..<20 { + try! channel.pipeline.syncOperations.addHandler(StringEnvelopeForwardingHandler()) + } + + // Create the envelope with full metadata + let address = try! SocketAddress(ipAddress: "::1", port: 8080) + let metadata = AddressedEnvelope.Metadata( + ecnState: .transportNotCapable, + packetInfo: NIOPacketInfo(destinationAddress: address, interfaceIndex: 1), + segmentSize: 1200 + ) + let envelope = AddressedEnvelope(remoteAddress: address, data: "test data", metadata: metadata) + + benchmark.startMeasurement() + defer { + benchmark.stopMeasurement() + } + + for _ in 0..? = try! channel.readInbound() + blackHole(result) + } + } } diff --git a/Benchmarks/Benchmarks/NIOPosixBenchmarks/Benchmarks.swift b/Benchmarks/Benchmarks/NIOPosixBenchmarks/Benchmarks.swift index ed178968c..947934644 100644 --- a/Benchmarks/Benchmarks/NIOPosixBenchmarks/Benchmarks.swift +++ b/Benchmarks/Benchmarks/NIOPosixBenchmarks/Benchmarks.swift @@ -67,6 +67,36 @@ let benchmarks = { ) } + Benchmark( + "UDPEcho", + configuration: .init( + metrics: defaultMetrics, + scalingFactor: .kilo, + maxDuration: .seconds(10_000_000), + maxIterations: 5 + ) + ) { benchmark in + try runUDPEcho( + numberOfWrites: benchmark.scaledIterations.upperBound, + eventLoop: eventLoop + ) + } + + Benchmark( + "UDPEchoPacketInfo", + configuration: .init( + metrics: defaultMetrics, + scalingFactor: .kilo, + maxDuration: .seconds(10_000_000), + maxIterations: 5 + ) + ) { benchmark in + try runUDPEchoPacketInfo( + numberOfWrites: benchmark.scaledIterations.upperBound, + eventLoop: eventLoop + ) + } + Benchmark( "MTELG.scheduleTask(in:_:)", configuration: Benchmark.Configuration( diff --git a/Benchmarks/Benchmarks/NIOPosixBenchmarks/UDPEcho.swift b/Benchmarks/Benchmarks/NIOPosixBenchmarks/UDPEcho.swift new file mode 100644 index 000000000..18a33f2a5 --- /dev/null +++ b/Benchmarks/Benchmarks/NIOPosixBenchmarks/UDPEcho.swift @@ -0,0 +1,151 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftNIO open source project +// +// Copyright (c) 2025 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 NIOCore +import NIOPosix + +private final class UDPEchoHandler: ChannelInboundHandler { + typealias InboundIn = AddressedEnvelope + typealias OutboundOut = AddressedEnvelope + + func channelRead(context: ChannelHandlerContext, data: NIOAny) { + context.write(data, promise: nil) + } + + func channelReadComplete(context: ChannelHandlerContext) { + context.flush() + } +} + +private final class UDPEchoRequestHandler: ChannelInboundHandler { + typealias InboundIn = AddressedEnvelope + typealias OutboundOut = AddressedEnvelope + + private let buffer = ByteBuffer(repeating: 0, count: 512) + private let numberOfWrites: Int + private let remoteAddress: SocketAddress + private var receivedCount = 0 + private let readsCompletePromise: EventLoopPromise + + init( + numberOfWrites: Int, + remoteAddress: SocketAddress, + readsCompletePromise: EventLoopPromise + ) { + self.numberOfWrites = numberOfWrites + self.remoteAddress = remoteAddress + self.readsCompletePromise = readsCompletePromise + } + + func channelActive(context: ChannelHandlerContext) { + self.writeAgain(context: context) + } + + func channelRead(context: ChannelHandlerContext, data: NIOAny) { + self.receivedCount += 1 + + if self.receivedCount == self.numberOfWrites { + self.readsCompletePromise.succeed() + } else { + self.writeAgain(context: context) + } + } + + private func writeAgain(context: ChannelHandlerContext) { + let envelope = AddressedEnvelope( + remoteAddress: self.remoteAddress, + data: buffer + ) + context.write(Self.wrapOutboundOut(envelope), promise: nil) + context.flush() + } +} + +func runUDPEcho(numberOfWrites: Int, eventLoop: any EventLoop) throws { + let address = try SocketAddress.makeAddressResolvingHost("127.0.0.1", port: 0) + + // Create server channel + let serverChannel = try DatagramBootstrap(group: eventLoop) + .channelInitializer { channel in + channel.eventLoop.makeCompletedFuture { + try channel.pipeline.syncOperations.addHandler(UDPEchoHandler()) + } + } + .bind(to: address) + .wait() + + let readsCompletePromise = eventLoop.makePromise(of: Void.self) + + // Create client channel + let clientChannel = try DatagramBootstrap(group: eventLoop) + .channelInitializer { channel in + channel.eventLoop.makeCompletedFuture { + let handler = UDPEchoRequestHandler( + numberOfWrites: numberOfWrites, + remoteAddress: serverChannel.localAddress!, + readsCompletePromise: readsCompletePromise + ) + try channel.pipeline.syncOperations.addHandler(handler) + } + } + .bind(to: address) + .wait() + + // Wait for all echoes to complete + try readsCompletePromise.futureResult.wait() + + // Cleanup + try serverChannel.close().wait() + try clientChannel.close().wait() +} + +func runUDPEchoPacketInfo(numberOfWrites: Int, eventLoop: any EventLoop) throws { + let address = try SocketAddress.makeAddressResolvingHost("127.0.0.1", port: 0) + + // Create server channel with receivePacketInfo enabled + let serverChannel = try DatagramBootstrap(group: eventLoop) + .channelOption(.receivePacketInfo, value: true) + .channelInitializer { channel in + channel.eventLoop.makeCompletedFuture { + try channel.pipeline.syncOperations.addHandler(UDPEchoHandler()) + } + } + .bind(to: address) + .wait() + + let readsCompletePromise = eventLoop.makePromise(of: Void.self) + + // Create client channel with receivePacketInfo enabled + let clientChannel = try DatagramBootstrap(group: eventLoop) + .channelOption(.receivePacketInfo, value: true) + .channelInitializer { channel in + channel.eventLoop.makeCompletedFuture { + let handler = UDPEchoRequestHandler( + numberOfWrites: numberOfWrites, + remoteAddress: serverChannel.localAddress!, + readsCompletePromise: readsCompletePromise + ) + try channel.pipeline.syncOperations.addHandler(handler) + } + } + .bind(to: address) + .wait() + + // Wait for all echoes to complete + try readsCompletePromise.futureResult.wait() + + // Cleanup + try serverChannel.close().wait() + try clientChannel.close().wait() +}