mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-24 04:17:52 +00:00
AsyncSequence throughput performance tests (#19)
* AsyncSequence throughput performance tests * Reduce source sequence overhead * Normalize whitespace * Added joined() throughput test * File renaming
This commit is contained in:
1 parent
18fcfe8e29
commit
91968fa3e9
2 files changed
+204
No files matched your search
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Value: Sendable>: 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<S: AsyncSequence, Output>( output: @autoclosure () -> Output, _ sequenceBuilder: (InfiniteAsyncSequence<Output>) -> 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<Int, Error> {
|
||||
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<S: AsyncSequence, Source: AsyncSequence>( 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<Int, Error> {
|
||||
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<S: AsyncSequence> : 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user