From 91968fa3e9fb5ecb7fa22a68b2a77c4434c664f4 Mon Sep 17 00:00:00 2001 From: kperryua Date: Tue, 25 Jan 2022 12:07:23 -0800 Subject: [PATCH] AsyncSequence throughput performance tests (#19) * AsyncSequence throughput performance tests * Reduce source sequence overhead * Normalize whitespace * Added joined() throughput test * File renaming --- .../Performance/TestThroughput.swift | 66 +++++++++ .../Performance/ThroughputMeasurement.swift | 138 ++++++++++++++++++ 2 files changed, 204 insertions(+) create mode 100644 Tests/AsyncAlgorithmsTests/Performance/TestThroughput.swift create mode 100644 Tests/AsyncAlgorithmsTests/Performance/ThroughputMeasurement.swift diff --git a/Tests/AsyncAlgorithmsTests/Performance/TestThroughput.swift b/Tests/AsyncAlgorithmsTests/Performance/TestThroughput.swift new file mode 100644 index 0000000..9a28674 --- /dev/null +++ b/Tests/AsyncAlgorithmsTests/Performance/TestThroughput.swift @@ -0,0 +1,66 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Async Algorithms open source project +// +// Copyright (c) 2022 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 +// +//===----------------------------------------------------------------------===// + +import XCTest +import AsyncAlgorithms + +final class TestThroughput: XCTestCase { + func test_chain2() async { + await measureSequenceThroughput(output: 1) { + chain($0, [].async) + } + } + func test_chain3() async { + await measureSequenceThroughput(output: 1) { + chain($0, [].async, [].async) + } + } + func test_compacted() async { + await measureSequenceThroughput(output: .some(1)) { + $0.compacted() + } + } + func test_interspersed() async { + await measureSequenceThroughput(output: 1) { + $0.interspersed(with: 0) + } + } + func test_joined() async { + await measureSequenceThroughput(output: [1, 2, 3, 4, 5].async) { + $0.joined(separator: [0, 0, 0, 0, 0].async) + } + } + func test_merge2() async { + await measureSequenceThroughput(output: 1) { + merge($0, (0..<10).async) + } + } + func test_merge3() async { + await measureSequenceThroughput(output: 1) { + merge($0, (0..<10).async, (0..<10).async) + } + } + func test_removeDuplicates() async { + await measureSequenceThroughput(source: (1...).async) { + $0.removeDuplicates() + } + } + func test_zip2() async { + await measureSequenceThroughput(output: 1) { + zip($0, Indefinite(value: 2).async) + } + } + func test_zip3() async { + await measureSequenceThroughput(output: 1) { + zip($0, Indefinite(value: 2).async, Indefinite(value: 3).async) + } + } +} diff --git a/Tests/AsyncAlgorithmsTests/Performance/ThroughputMeasurement.swift b/Tests/AsyncAlgorithmsTests/Performance/ThroughputMeasurement.swift new file mode 100644 index 0000000..fb90c3c --- /dev/null +++ b/Tests/AsyncAlgorithmsTests/Performance/ThroughputMeasurement.swift @@ -0,0 +1,138 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Async Algorithms open source project +// +// Copyright (c) 2022 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 +// +//===----------------------------------------------------------------------===// + +import AsyncAlgorithms +import Foundation +import XCTest + +public struct InfiniteAsyncSequence: AsyncSequence, Sendable { + public typealias Element = Value + let value: Value + let duration: Double + + public struct AsyncIterator : AsyncIteratorProtocol, Sendable { + + @usableFromInline + let value: Value + + @usableFromInline + let duration: Double + + @usableFromInline + var start: Double? = nil + + @inlinable + public mutating func next() async throws -> Element? { + guard !Task.isCancelled else { + return nil + } + return value + } + } + public func makeAsyncIterator() -> AsyncIterator { + return AsyncIterator(value: value, duration: duration) + } +} + +final class _ThroughputMetric: NSObject, XCTMetric, @unchecked Sendable { + var eventCount = 0 + + override init() { } + + func reportMeasurements(from startTime: XCTPerformanceMeasurementTimestamp, to endTime: XCTPerformanceMeasurementTimestamp) throws -> [XCTPerformanceMeasurement] { + return [XCTPerformanceMeasurement(identifier: "com.swift.AsyncAlgorithms.Throughput", displayName: "Throughput", doubleValue: Double(eventCount) / (endTime.date.timeIntervalSinceReferenceDate - startTime.date.timeIntervalSinceReferenceDate), unitSymbol: " Events/sec")] + } + + func copy(with zone: NSZone? = nil) -> Any { + return self + } + + func willBeginMeasuring() { + eventCount = 0 + } + func didStopMeasuring() { } +} + +extension XCTestCase { + public func measureSequenceThroughput( output: @autoclosure () -> Output, _ sequenceBuilder: (InfiniteAsyncSequence) -> S) async where S: Sendable { + let metric = _ThroughputMetric() + + measure(metrics: [metric]) { + let infSeq = InfiniteAsyncSequence(value: output(), duration: 1.0) + let seq = sequenceBuilder(infSeq) + + let exp = self.expectation(description: "Finished") + let iterTask = Task { + var eventCount = 0 + for try await _ in seq { + eventCount += 1 + } + metric.eventCount = eventCount + exp.fulfill() + return eventCount + } + usleep(UInt32(1 * USEC_PER_SEC)) + iterTask.cancel() + self.wait(for: [exp], timeout: 2.0) + } + } + + public func measureSequenceThroughput( source: Source, _ sequenceBuilder: (Source) -> S) async where S: Sendable, Source: Sendable { + let metric = _ThroughputMetric() + + measure(metrics: [metric]) { + let infSeq = source + let seq = sequenceBuilder(infSeq) + + let exp = self.expectation(description: "Finished") + let iterTask = Task { + var eventCount = 0 + for try await _ in seq { + eventCount += 1 + } + metric.eventCount = eventCount + exp.fulfill() + return eventCount + } + usleep(UInt32(1 * USEC_PER_SEC)) + iterTask.cancel() + self.wait(for: [exp], timeout: 2.0) + } + } +} + +final class TestMeasurements: XCTestCase { + struct PassthroughSequence : AsyncSequence, Sendable where S : Sendable, S.AsyncIterator : Sendable { + typealias Element = S.Element + + struct AsyncIterator : AsyncIteratorProtocol, Sendable { + + @usableFromInline + var base : S.AsyncIterator + + @inlinable + mutating func next() async throws -> Element? { + return try await base.next() + } + } + + let base : S + func makeAsyncIterator() -> AsyncIterator { + .init(base: base.makeAsyncIterator()) + } + } + + public func testThroughputTesting() async { + await self.measureSequenceThroughput(output: 1) { + PassthroughSequence(base: $0) + } + } +}