Skip to content

Commit

Permalink
make test thread number configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
zerosnacks committed Nov 14, 2024
1 parent 6056a66 commit 3cc9af8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion crates/cast/bin/cmd/create2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Create2Args {

let regex = RegexSetBuilder::new(regexs).case_insensitive(!case_sensitive).build()?;

let n_threads = global.jobs().unwrap_or(1);
let n_threads = global.jobs(true).unwrap_or(1);

let mut salt = B256::ZERO;
let remaining = if let Some(caller_address) = caller {
Expand Down
24 changes: 14 additions & 10 deletions crates/cli/src/opts/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ impl GlobalOpts {

/// Spawn a new global thread pool.
pub fn try_spawn(self, jobs: Option<usize>) -> Result<(), rayon::ThreadPoolBuildError> {
if let Some(jobs) = jobs.or_else(|| self.jobs()) {
if let Some(jobs) = jobs.or_else(|| self.jobs(false)) {
trace!(target: "forge::cli", "starting global thread pool with up to {} threads", jobs);

// Attempt to spawn the global thread pool with the specified number of threads.
// If it is already initialized simply return.
if ThreadPoolBuilder::new().num_threads(jobs).build_global().is_err() {
return Ok(());
warn!(target: "forge::cli", "global thread pool already initialized");
}

trace!(target: "forge::cli", "starting global thread pool with up to {} threads", jobs);

Ok(())
} else {
// If `--jobs` is not provided, do not spawn the global thread pool.
Expand All @@ -90,14 +90,18 @@ impl GlobalOpts {
///
/// Try to use the number of threads specified by `--jobs` if provided, otherwise use the number
/// of logical CPUs.
pub fn jobs(&self) -> Option<usize> {
self.jobs.map(|jobs| {
pub fn jobs(&self, default: bool) -> Option<usize> {
if let Some(jobs) = self.jobs {
if jobs == 0 {
current_num_threads()
} else {
jobs.min(current_num_threads())
return Some(current_num_threads());
}
})

Some(jobs.min(current_num_threads()))
} else if default {
return Some(current_num_threads());
} else {
return None;
}
}

/// Create a new shell instance.
Expand Down
3 changes: 1 addition & 2 deletions crates/forge/bin/cmd/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use foundry_config::{
};
use foundry_debugger::Debugger;
use foundry_evm::traces::identifier::TraceIdentifiers;
use rayon::current_num_threads;
use regex::Regex;
use std::{
collections::{BTreeMap, BTreeSet},
Expand Down Expand Up @@ -895,7 +894,7 @@ impl Provider for TestArgs {
dict.insert("show_progress".to_string(), true.into());
}

if let Some(threads) = self.global.jobs().or(Some(current_num_threads())) {
if let Some(threads) = self.global.jobs(true) {
dict.insert("threads".to_string(), threads.into());
}

Expand Down
7 changes: 5 additions & 2 deletions crates/forge/src/multi_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use foundry_evm::{
traces::{InternalTraceMode, TraceMode},
};
use foundry_linking::{LinkOutput, Linker};
use rayon::prelude::*;
use rayon::{current_num_threads, prelude::*};
use revm::primitives::SpecId;
use std::{
borrow::Borrow,
Expand Down Expand Up @@ -185,7 +185,10 @@ impl MultiContractRunner {
);

if show_progress {
let tests_progress = TestsProgress::new(contracts.len(), rayon::current_num_threads());
let tests_progress = TestsProgress::new(
contracts.len(),
self.config.threads.unwrap_or(current_num_threads()),
);
// Collect test suite results to stream at the end of test run.
let results: Vec<(String, SuiteResult)> = contracts
.par_iter()
Expand Down

0 comments on commit 3cc9af8

Please sign in to comment.