-
Notifications
You must be signed in to change notification settings - Fork 128
Add an upper bound on the number of test cases we run in parallel. #1390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
grynspan
wants to merge
14
commits into
main
Choose a base branch
from
jgrynspan/experimental-parallelization-cap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4da4061
Add an upper bound on the number of test cases we run in parallel.
grynspan 85822e3
Don't need a separate Darwin implementation
grynspan 23224a2
Guard against (hypothetical) bad CPU core counts from the OS
grynspan 5f9bbc5
Don't bother making the serializer an optional
grynspan 2b551e6
Hit the task local less frequently
grynspan 5e47794
Incorporate feedback
grynspan e11adc8
Merge branch 'main' into jgrynspan/experimental-parallelization-cap
grynspan 56a354e
Add width checks
grynspan dc66208
Don't use a serializer if the user doesn't explicitly opt in, add som…
grynspan e03b747
Update comment
grynspan 253aebb
--no-parallel and --experimental-maximum-parallelization-width conflict
grynspan 781cc58
Lower the environment variable check so that Xcode 26 (which always s…
grynspan d46825d
Missing else
grynspan a4b8c1c
Ignore envvar when running our own tests as it (global state) interfe…
grynspan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2024–2025 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 | ||
| // See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
| // | ||
|
|
||
| private import _TestingInternals | ||
|
|
||
| /// The number of CPU cores on the current system, or `nil` if that | ||
| /// information is not available. | ||
| var cpuCoreCount: Int? { | ||
| #if SWT_TARGET_OS_APPLE || os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android) | ||
| return Int(sysconf(Int32(_SC_NPROCESSORS_CONF))) | ||
| #elseif os(Windows) | ||
| var siInfo = SYSTEM_INFO() | ||
| GetSystemInfo(&siInfo) | ||
| return Int(siInfo.dwNumberOfProcessors) | ||
| #elseif os(WASI) | ||
| return 1 | ||
| #else | ||
| #warning("Platform-specific implementation missing: CPU core count unavailable") | ||
| return nil | ||
| #endif | ||
| } | ||
|
|
||
| /// The default parallelization width when parallelized testing is enabled. | ||
| var defaultParallelizationWidth: Int { | ||
| // cpuCoreCount.map { max(1, $0) * 2 } ?? .max | ||
| .max | ||
| } | ||
|
|
||
| /// A type whose instances can run a series of work items in strict order. | ||
| /// | ||
| /// When a work item is scheduled on an instance of this type, it runs after any | ||
| /// previously-scheduled work items. If it suspends, subsequently-scheduled work | ||
| /// items do not start running; they must wait until the suspended work item | ||
| /// either returns or throws an error. | ||
| /// | ||
| /// This type is not part of the public interface of the testing library. | ||
| final actor Serializer { | ||
grynspan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// The maximum number of work items that may run concurrently. | ||
| nonisolated let maximumWidth: Int | ||
|
|
||
| /// The number of scheduled work items, including any currently running. | ||
| private var _currentWidth = 0 | ||
|
|
||
| /// Continuations for any scheduled work items that haven't started yet. | ||
| private var _continuations = [CheckedContinuation<Void, Never>]() | ||
|
|
||
| init(maximumWidth: Int = 1) { | ||
| precondition(maximumWidth >= 1, "Invalid serializer width \(maximumWidth).") | ||
| self.maximumWidth = maximumWidth | ||
grynspan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// Run a work item serially after any previously-scheduled work items. | ||
| /// | ||
| /// - Parameters: | ||
| /// - workItem: A closure to run. | ||
| /// | ||
| /// - Returns: Whatever is returned from `workItem`. | ||
| /// | ||
| /// - Throws: Whatever is thrown by `workItem`. | ||
| func run<R>(_ workItem: @isolated(any) @Sendable () async throws -> R) async rethrows -> R where R: Sendable { | ||
| _currentWidth += 1 | ||
| defer { | ||
| // Resume the next scheduled closure. | ||
| if !_continuations.isEmpty { | ||
| let continuation = _continuations.removeFirst() | ||
grynspan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| continuation.resume() | ||
| } | ||
|
|
||
| _currentWidth -= 1 | ||
| } | ||
|
|
||
| await withCheckedContinuation { continuation in | ||
| if _currentWidth <= maximumWidth { | ||
| // Nothing else was scheduled, so we can resume immediately. | ||
| continuation.resume() | ||
| } else { | ||
| // Something was scheduled, so add the continuation to the | ||
| // list. When it resumes, we can run. | ||
| _continuations.append(continuation) | ||
| } | ||
| } | ||
|
|
||
| return try await workItem() | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.