From 7e3b50b38e4e66f31db6cf4a784c6af148bac846 Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Mon, 19 Dec 2022 19:14:27 +0100 Subject: [PATCH] Improve performance of tests (#2336) --- .../NIOConcurrencyHelpersTests.swift | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/Tests/NIOConcurrencyHelpersTests/NIOConcurrencyHelpersTests.swift b/Tests/NIOConcurrencyHelpersTests/NIOConcurrencyHelpersTests.swift index b61591d70e..6869b34204 100644 --- a/Tests/NIOConcurrencyHelpersTests/NIOConcurrencyHelpersTests.swift +++ b/Tests/NIOConcurrencyHelpersTests/NIOConcurrencyHelpersTests.swift @@ -25,10 +25,21 @@ 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) @@ -36,7 +47,7 @@ class NIOConcurrencyHelpersTests: XCTestCase { let ai = NIOConcurrencyHelpers.Atomic(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() @@ -45,14 +56,14 @@ class NIOConcurrencyHelpersTests: XCTestCase { } } } - for _ in 0...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() @@ -258,14 +268,14 @@ class NIOConcurrencyHelpersTests: XCTestCase { } } } - for _ in 0..