Skip to content
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

[Move/Example] Run tests in parallel #18802

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/sui-framework-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ license = "Apache-2.0"
publish = false

[dev-dependencies]
futures.workspace = true
prometheus.workspace = true
tokio.workspace = true

sui-framework.workspace = true
sui-move = { workspace = true, features = ["unit_test"] }
Expand Down
41 changes: 29 additions & 12 deletions crates/sui-framework-tests/src/unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fn run_deepbook_tests() {
buf.extend(["..", "sui-framework", "packages", "deepbook"]);
check_move_unit_tests(&buf);
}

#[test]
#[cfg_attr(msim, ignore)]
fn run_bridge_tests() {
Expand All @@ -52,30 +53,46 @@ fn run_bridge_tests() {
check_move_unit_tests(&buf);
}

fn check_packages_recursively(path: &Path) -> io::Result<()> {
for entry in fs::read_dir(path).unwrap() {
let entry = entry?;
if entry.path().join("Move.toml").exists() {
check_package_builds(&entry.path());
check_move_unit_tests(&entry.path());
} else if entry.file_type()?.is_dir() {
check_packages_recursively(&entry.path())?;
/// Look for Move packages (directories containing Move.toml) and checks that:
///
/// - It builds, in dev mode, with all warnings and lints enabled as errors.
/// - The tests all pass.
async fn check_packages_recursively(path: &Path) -> io::Result<()> {
let mut frontier = vec![path.to_owned()];
let mut move_packages = vec![];

while let Some(dir) = frontier.pop() {
for entry in fs::read_dir(dir).unwrap() {
let entry = entry?;
if entry.path().join("Move.toml").exists() {
move_packages.push(entry.path());
} else if entry.file_type()?.is_dir() {
frontier.push(entry.path());
}
}
}

futures::future::join_all(move_packages.into_iter().map(|p| {
tokio::task::spawn(async move {
check_package_builds(&p);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I played around with splitting this up, but it only shaved about a second of the run-time, so thought it would be clearer to do it as one step.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any async here, why use this over spawning threads?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact this is an anti-pattern, executing synchronous work in an async threadpool

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, there is nothing async here. I ended up implementing it this way because it seemed like the easiest way to take advantage of a multi-threaded pool (to limit the number of threads being spawned). If you can point me to a better way of doing that, I'll do that instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just dogscience-ing my way through life here, so I can be convinced that this is the right thing to do. My impression was that rayon was primarily intended to introduce parallelism into functional stream-based programs. It looks like it has a thread pool library, and we could use that, but I can't really make out what the moral difference is between abusing rayon for its thread pool implementation vs abusing tokio.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll admit that in this exact instance its not a huge issue to abuse tokio like this (because nothing else is sharing this particular runtime) but it is a very bad anti-pattern and if was done in production code could cause bad things to happen.

You can essentially break computation down into two camps: io bound (async) and cpu bound (sync) work. for io bound tasks, the idea is that lots of independent tasks can operate concurrently on a single (or multiple) thread, relinquishing control of execution back to the schedule when the task needs to wait for some io operation to happen. cpu bound tasks generally operate in a way such that they monopolize the thread they are scheduled on until the entire task is complete.

rayon is specifically designed to handle and schedule cpu bound work while tokio actually has two thread pools, a blocking pool for scheduling cpu bound work and an async pool (where things go when tokio::spawnd) for async/io bound tasks.

If you schedule blocking/cpu bound work on an async pool then other async tasks could get stuck behind them waiting for a very long time till they're able to make any forward progress.

All of this aside, did you take a look at using datatest-stable like we use for a number of other file-based tests such that each move example would easily be their own test with little to no care to those who are working or adding examples?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think datatest-stable would be a great fit here. I will put up a separate PR for that, but will keep this one around in case the timeouts do cause a problem, because I haven't had to set-up a fresh data test before, so I don't know how long it will take me, and I don't want people to be blocked if it takes a little while.

The reason I wasn't too hot on rayon in this case was that

  • although the test is not async, it's also not CPU bound -- it's I/O bound but written to use blocking I/O,
  • rayon really seems geared towards running a computation to produce a certain result, while these tests are purely running for their effects.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#18813 -- thanks for the suggestion @bmwill !

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

although the test is not async, it's also not CPU bound -- it's I/O bound but written to use blocking I/O,

Yes, i would generally put "blocking I/O" in the same camp as CPU bound tasks as they block the current thread until the operation is complete vs handing it off to something else to attempt to make progress on.

check_move_unit_tests(&p);
})
}))
.await;

Ok(())
}

#[test]
#[tokio::test(flavor = "multi_thread", worker_threads = 16)]
#[cfg_attr(msim, ignore)]
fn run_examples_move_unit_tests() -> io::Result<()> {
async fn run_examples_move_unit_tests() -> io::Result<()> {
let examples = {
let mut buf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
buf.extend(["..", "..", "examples"]);
buf
};

check_packages_recursively(&examples)?;

check_packages_recursively(&examples).await?;
Ok(())
}

Expand Down
Loading