From fac138c517367fc82dfb6e79443de849666771c3 Mon Sep 17 00:00:00 2001 From: Reza Shirazian Date: Mon, 28 Mar 2022 17:09:22 -0700 Subject: [PATCH] #45 - Two sided performance test for merge (#114) This PR extends `measureSequenceThroughput` to take two outputs so functions like merge/ zip etc can be tested by both sides. --- .../Performance/TestThroughput.swift | 24 ++++----- .../Performance/ThroughputMeasurement.swift | 51 +++++++++++++++++++ 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/Tests/AsyncAlgorithmsTests/Performance/TestThroughput.swift b/Tests/AsyncAlgorithmsTests/Performance/TestThroughput.swift index 95fd3be..2d490cc 100644 --- a/Tests/AsyncAlgorithmsTests/Performance/TestThroughput.swift +++ b/Tests/AsyncAlgorithmsTests/Performance/TestThroughput.swift @@ -15,13 +15,13 @@ import AsyncAlgorithms #if canImport(Darwin) final class TestThroughput: XCTestCase { func test_chain2() async { - await measureSequenceThroughput(output: 1) { - chain($0, [].async) + await measureSequenceThroughput(firstOutput: 1, secondOutput: 2) { + chain($0, $1) } } func test_chain3() async { - await measureSequenceThroughput(output: 1) { - chain($0, [].async, [].async) + await measureSequenceThroughput(firstOutput: 1, secondOutput: 2, thirdOutput: 3) { + chain($0, $1, $2) } } func test_compacted() async { @@ -40,13 +40,13 @@ final class TestThroughput: XCTestCase { } } func test_merge2() async { - await measureSequenceThroughput(output: 1) { - merge($0, (0..<10).async) + await measureSequenceThroughput(firstOutput: 1, secondOutput: 2) { + merge($0, $1) } } func test_merge3() async { - await measureSequenceThroughput(output: 1) { - merge($0, (0..<10).async, (0..<10).async) + await measureSequenceThroughput(firstOutput: 1, secondOutput: 2, thirdOutput: 3) { + merge($0, $1, $2) } } func test_removeDuplicates() async { @@ -55,13 +55,13 @@ final class TestThroughput: XCTestCase { } } func test_zip2() async { - await measureSequenceThroughput(output: 1) { - zip($0, Indefinite(value: 2).async) + await measureSequenceThroughput(firstOutput: 1, secondOutput: 2) { + zip($0, $1) } } func test_zip3() async { - await measureSequenceThroughput(output: 1) { - zip($0, Indefinite(value: 2).async, Indefinite(value: 3).async) + await measureSequenceThroughput(firstOutput: 1, secondOutput: 2, thirdOutput: 3) { + zip($0, $1, $2) } } } diff --git a/Tests/AsyncAlgorithmsTests/Performance/ThroughputMeasurement.swift b/Tests/AsyncAlgorithmsTests/Performance/ThroughputMeasurement.swift index c1fc811..1ebf1e4 100644 --- a/Tests/AsyncAlgorithmsTests/Performance/ThroughputMeasurement.swift +++ b/Tests/AsyncAlgorithmsTests/Performance/ThroughputMeasurement.swift @@ -87,6 +87,57 @@ extension XCTestCase { } } + public func measureSequenceThroughput(firstOutput: @autoclosure () -> Output, secondOutput: @autoclosure () -> Output, _ sequenceBuilder: (InfiniteAsyncSequence, InfiniteAsyncSequence) -> S) async where S: Sendable { + let metric = _ThroughputMetric() + let sampleTime: Double = 0.1 + + measure(metrics: [metric]) { + let firstInfSeq = InfiniteAsyncSequence(value: firstOutput(), duration: sampleTime) + let secondInfSeq = InfiniteAsyncSequence(value: secondOutput(), duration: sampleTime) + let seq = sequenceBuilder(firstInfSeq, secondInfSeq) + + 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(sampleTime * Double(USEC_PER_SEC))) + iterTask.cancel() + self.wait(for: [exp], timeout: sampleTime * 2) + } + } + + public func measureSequenceThroughput(firstOutput: @autoclosure () -> Output, secondOutput: @autoclosure () -> Output, thirdOutput: @autoclosure () -> Output, _ sequenceBuilder: (InfiniteAsyncSequence, InfiniteAsyncSequence, InfiniteAsyncSequence) -> S) async where S: Sendable { + let metric = _ThroughputMetric() + let sampleTime: Double = 0.1 + + measure(metrics: [metric]) { + let firstInfSeq = InfiniteAsyncSequence(value: firstOutput(), duration: sampleTime) + let secondInfSeq = InfiniteAsyncSequence(value: secondOutput(), duration: sampleTime) + let thirdInfSeq = InfiniteAsyncSequence(value: thirdOutput(), duration: sampleTime) + let seq = sequenceBuilder(firstInfSeq, secondInfSeq, thirdInfSeq) + + 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(sampleTime * Double(USEC_PER_SEC))) + iterTask.cancel() + self.wait(for: [exp], timeout: sampleTime * 2) + } +} + public func measureSequenceThroughput( source: Source, _ sequenceBuilder: (Source) -> S) async where S: Sendable, Source: Sendable { let metric = _ThroughputMetric() let sampleTime: Double = 0.1