Skip to content

Commit

Permalink
Improve performance of tests (#2336)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnadoba authored Dec 19, 2022
1 parent 86e8b5f commit 7e3b50b
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions Tests/NIOConcurrencyHelpersTests/NIOConcurrencyHelpersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,29 @@ class NIOConcurrencyHelpersTests: XCTestCase {
private func sumOfIntegers(until n: UInt64) -> UInt64 {
return n*(n+1)/2
}

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
let noAsyncs: UInt64 = 50
#else
/// `swift-corelibs-libdispatch` implementation of concurrent queues only initially spawn up to `System.coreCount` threads.
/// Afterwards they will create one thread per second.
/// Therefore this test takes `noAsyncs - System.coreCount` seconds to execute.
/// For example if `noAsyncs == 50` and `System.coreCount == 8` this test takes ~42 seconds to execute.
/// On non Darwin system we therefore limit the number of async operations to `System.coreCount`.
let noAsyncs: UInt64 = UInt64(System.coreCount)
#endif

@available(*, deprecated, message: "deprecated because it tests deprecated functionality")
func testLargeContendedAtomicSum() {
let noAsyncs: UInt64 = 50

let noCounts: UInt64 = 2_000

let q = DispatchQueue(label: "q", attributes: .concurrent)
let g = DispatchGroup()
let ai = NIOConcurrencyHelpers.Atomic<UInt64>(value: 0)
let everybodyHere = DispatchSemaphore(value: 0)
let go = DispatchSemaphore(value: 0)
for thread in 1...noAsyncs {
for thread in 1...self.noAsyncs {
q.async(group: g) {
everybodyHere.signal()
go.wait()
Expand All @@ -45,14 +56,14 @@ class NIOConcurrencyHelpersTests: XCTestCase {
}
}
}
for _ in 0..<noAsyncs {
for _ in 0..<self.noAsyncs {
everybodyHere.wait()
}
for _ in 0..<noAsyncs {
for _ in 0..<self.noAsyncs {
go.signal()
}
g.wait()
XCTAssertEqual(sumOfIntegers(until: noAsyncs) * noCounts, ai.load())
XCTAssertEqual(sumOfIntegers(until: self.noAsyncs) * noCounts, ai.load())
}

@available(*, deprecated, message: "deprecated because it tests deprecated functionality")
Expand Down Expand Up @@ -241,15 +252,14 @@ class NIOConcurrencyHelpersTests: XCTestCase {

@available(*, deprecated, message: "deprecated because it tests deprecated functionality")
func testLargeContendedNIOAtomicSum() {
let noAsyncs: UInt64 = 50
let noCounts: UInt64 = 2_000

let q = DispatchQueue(label: "q", attributes: .concurrent)
let g = DispatchGroup()
let ai = NIOConcurrencyHelpers.NIOAtomic<UInt64>.makeAtomic(value: 0)
let everybodyHere = DispatchSemaphore(value: 0)
let go = DispatchSemaphore(value: 0)
for thread in 1...noAsyncs {
for thread in 1...self.noAsyncs {
q.async(group: g) {
everybodyHere.signal()
go.wait()
Expand All @@ -258,14 +268,14 @@ class NIOConcurrencyHelpersTests: XCTestCase {
}
}
}
for _ in 0..<noAsyncs {
for _ in 0..<self.noAsyncs {
everybodyHere.wait()
}
for _ in 0..<noAsyncs {
for _ in 0..<self.noAsyncs {
go.signal()
}
g.wait()
XCTAssertEqual(sumOfIntegers(until: noAsyncs) * noCounts, ai.load())
XCTAssertEqual(sumOfIntegers(until: self.noAsyncs) * noCounts, ai.load())
}

@available(*, deprecated, message: "deprecated because it tests deprecated functionality")
Expand Down

0 comments on commit 7e3b50b

Please sign in to comment.