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 <gbarnett@apple.com>
This commit is contained in:
Cory BenfieldandGeorge Barnett authored and GitHub committed 2025-12-01 13:40:11 +00:00
1 parent b60d141920
commit 2bc6627d74
3 files changed
+346

No files matched your search

@@ -16,10 +16,39 @@ import Benchmark
import NIOCore
import NIOEmbedded
// MARK: - Handlers for AddressedEnvelope benchmarks
private final class ByteBufferEnvelopeForwardingHandler: ChannelInboundHandler, Sendable {
typealias InboundIn = AddressedEnvelope<ByteBuffer>
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
context.fireChannelRead(data)
}
}
private final class StringEnvelopeForwardingHandler: ChannelInboundHandler, Sendable {
typealias InboundIn = AddressedEnvelope<String>
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..<benchmark.scaledIterations.count {
try! channel.writeInbound(envelope)
let result: AddressedEnvelope<ByteBuffer>? = 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<ByteBuffer>.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..<benchmark.scaledIterations.count {
try! channel.writeInbound(envelope)
let result: AddressedEnvelope<ByteBuffer>? = 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..<benchmark.scaledIterations.count {
try! channel.writeInbound(envelope)
let result: AddressedEnvelope<String>? = 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<String>.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..<benchmark.scaledIterations.count {
try! channel.writeInbound(envelope)
let result: AddressedEnvelope<String>? = try! channel.readInbound()
blackHole(result)
}
}
}
@@ -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(
@@ -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<ByteBuffer>
typealias OutboundOut = AddressedEnvelope<ByteBuffer>
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<ByteBuffer>
typealias OutboundOut = AddressedEnvelope<ByteBuffer>
private let buffer = ByteBuffer(repeating: 0, count: 512)
private let numberOfWrites: Int
private let remoteAddress: SocketAddress
private var receivedCount = 0
private let readsCompletePromise: EventLoopPromise<Void>
init(
numberOfWrites: Int,
remoteAddress: SocketAddress,
readsCompletePromise: EventLoopPromise<Void>
) {
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()
}