Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Benchmarks/Benchmarks/IOBenchmarks/Benchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
91 changes: 91 additions & 0 deletions Benchmarks/Benchmarks/IOBenchmarks/UDPBurst.swift
Original file line number Diff line number Diff line change
@@ -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..<numberOfBursts {
for _ in 0..<burstSize {
try await connection.write(from: datagram.span.bytes)
}
var output = OutputRawSpan(buffer: buffer, initializedCount: 0)
try await connection.read(into: &output)
_ = output.finalize(for: buffer)
}

try await connection.close()
}

@available(anyAppleOS 27.0, *)
private func runServer(socket: inout UDPSocket, numberOfBursts: Int) async throws {
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: datagramSize, alignment: 1)
defer {
buffer.deallocate()
}

for _ in 0..<numberOfBursts {
var sender: SocketAddress?
for _ in 0..<burstSize {
var output = OutputRawSpan(buffer: buffer, initializedCount: 0)
sender = try await socket.receive(into: &output).sender
_ = output.finalize(for: buffer)
}
try await socket.send(from: RawSpan(_unsafeBytes: UnsafeRawBufferPointer(rebasing: buffer[..<1])), to: sender!)
}
}
91 changes: 91 additions & 0 deletions Benchmarks/Benchmarks/IOBenchmarks/UDPPingPong.swift
Original file line number Diff line number Diff line change
@@ -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 that the client sends and the server sends back.
private let datagramSize = 512

/// Plays ping pong between a connected client and a bound server socket over the loopback interface.
///
/// - Parameters:
/// - numberOfDatagrams: The number of datagrams that go back and forth.
/// - ioBackend: The I/O backend that the executor's threads use.
/// - benchmark: The benchmark to measure.
@available(anyAppleOS 27.0, *)
func runUDPPingPong(numberOfDatagrams: Int, ioBackend: PThreadIOBackend, benchmark: Benchmark) async throws {
try await PThreadTaskExecutor.withExecutor(name: "UDPPingPong", 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, numberOfDatagrams: numberOfDatagrams)
}

try await runServer(socket: &server, numberOfDatagrams: numberOfDatagrams)
try await server.close()

try await group.waitForAll()
}
}
}
}

/// Sends the datagrams and waits for each one to come back before sending the next one.
@available(anyAppleOS 27.0, *)
private func runClient(connectingTo address: SocketAddress, numberOfDatagrams: Int) async throws {
var connection = try await UDPConnection.connect(to: address)

let datagram = [UInt8](repeating: 0, count: datagramSize)
// The buffer has to stay alive while a read is in flight so we heap alloc here.
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: datagramSize, alignment: 1)
defer {
buffer.deallocate()
}

for _ in 0..<numberOfDatagrams {
try await connection.write(from: datagram.span.bytes)

var output = OutputRawSpan(buffer: buffer, initializedCount: 0)
try await connection.read(into: &output)
_ = output.finalize(for: buffer)
}

try await connection.close()
}

/// Sends every datagram back to its sender.
@available(anyAppleOS 27.0, *)
private func runServer(socket: inout UDPSocket, numberOfDatagrams: Int) async throws {
// The buffer has to stay alive while a receive is in flight so we heap alloc here.
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: datagramSize, alignment: 1)
defer {
buffer.deallocate()
}

for _ in 0..<numberOfDatagrams {
var output = OutputRawSpan(buffer: buffer, initializedCount: 0)
let (count, sender) = try await socket.receive(into: &output)
_ = output.finalize(for: buffer)
try await socket.send(
from: RawSpan(_unsafeBytes: UnsafeRawBufferPointer(rebasing: buffer[..<count])),
to: sender
)
}
}
Loading