Files
swift-nio/Sources/NIOPerformanceTester/NIOAsyncWriterSingleWritesBenchmark.swift
T
Franz BuschandCory Benfield 1abe64c5e6 Add benchmarks for NIOAsyncWriter and NIOAsyncSequenceProducer (#2301)
# Motivation
We landed the async bridge types a while back but never added allocation and performance tests. Since we expect these types to be used performance critical paths we really should cover those with tests.

# Modification
Extends the allocation counter scaffolding to support async tests. Furthermore, add allocations tests for both the writer and producer. Lastly, I a also added a performance test for the producer.

# Result
We now have baseline tests for the `NIOAsyncWriter` and `NIOAsyncSequenceProducer`

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-10-26 03:51:20 -07:00

56 lines
1.8 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2022 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 DequeModule
import Atomics
fileprivate struct NoOpDelegate: NIOAsyncWriterSinkDelegate, @unchecked Sendable {
typealias Element = Int
let counter = ManagedAtomic(0)
func didYield(contentsOf sequence: Deque<Int>) {
counter.wrappingIncrement(by: sequence.count, ordering: .relaxed)
}
func didTerminate(error: Error?) {}
}
// This is unchecked Sendable because the Sink is not Sendable but the Sink is thread safe
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
final class NIOAsyncWriterSingleWritesBenchmark: AsyncBenchmark, @unchecked Sendable {
private let iterations: Int
private let delegate: NoOpDelegate
private let writer: NIOAsyncWriter<Int, NoOpDelegate>
private let sink: NIOAsyncWriter<Int, NoOpDelegate>.Sink
init(iterations: Int) {
self.iterations = iterations
self.delegate = .init()
let newWriter = NIOAsyncWriter<Int, NoOpDelegate>.makeWriter(isWritable: true, delegate: self.delegate)
self.writer = newWriter.writer
self.sink = newWriter.sink
}
func setUp() async throws {}
func tearDown() {}
func run() async throws -> Int {
for i in 0..<self.iterations {
try await self.writer.yield(i)
}
return self.delegate.counter.load(ordering: .sequentiallyConsistent)
}
}