forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#89082 - smoelius:master, r=kennytm
Implement rust-lang#85440 (Random test ordering) This PR adds `--shuffle` and `--shuffle-seed` options to `libtest`. The options are similar to the [`-shuffle` option](https://github.com/golang/go/blob/c894b442d1e5e150ad33fa3ce13dbfab1c037b3a/src/testing/testing.go#L1482-L1499) that was recently added to Go. Here are the relevant parts of the help message: ``` --shuffle Run tests in random order --shuffle-seed SEED Run tests in random order; seed the random number generator with SEED ... By default, the tests are run in alphabetical order. Use --shuffle or set RUST_TEST_SHUFFLE to run the tests in random order. Pass the generated "shuffle seed" to --shuffle-seed (or set RUST_TEST_SHUFFLE_SEED) to run the tests in the same order again. Note that --shuffle and --shuffle-seed do not affect whether the tests are run in parallel. ``` Is an RFC needed for this?
- Loading branch information
Showing
14 changed files
with
313 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -5,3 +5,4 @@ pub mod concurrency; | |
pub mod exit_code; | ||
pub mod isatty; | ||
pub mod metrics; | ||
pub mod shuffle; |
This file contains 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,67 @@ | ||
use crate::cli::TestOpts; | ||
use crate::types::{TestDescAndFn, TestId, TestName}; | ||
use std::collections::hash_map::DefaultHasher; | ||
use std::hash::Hasher; | ||
use std::time::{SystemTime, UNIX_EPOCH}; | ||
|
||
pub fn get_shuffle_seed(opts: &TestOpts) -> Option<u64> { | ||
opts.shuffle_seed.or_else(|| { | ||
if opts.shuffle { | ||
Some( | ||
SystemTime::now() | ||
.duration_since(UNIX_EPOCH) | ||
.expect("Failed to get system time") | ||
.as_nanos() as u64, | ||
) | ||
} else { | ||
None | ||
} | ||
}) | ||
} | ||
|
||
pub fn shuffle_tests(shuffle_seed: u64, tests: &mut [(TestId, TestDescAndFn)]) { | ||
let test_names: Vec<&TestName> = tests.iter().map(|test| &test.1.desc.name).collect(); | ||
let test_names_hash = calculate_hash(&test_names); | ||
let mut rng = Rng::new(shuffle_seed, test_names_hash); | ||
shuffle(&mut rng, tests); | ||
} | ||
|
||
// `shuffle` is from `rust-analyzer/src/cli/analysis_stats.rs`. | ||
fn shuffle<T>(rng: &mut Rng, slice: &mut [T]) { | ||
for i in 0..slice.len() { | ||
randomize_first(rng, &mut slice[i..]); | ||
} | ||
|
||
fn randomize_first<T>(rng: &mut Rng, slice: &mut [T]) { | ||
assert!(!slice.is_empty()); | ||
let idx = rng.rand_range(0..slice.len() as u64) as usize; | ||
slice.swap(0, idx); | ||
} | ||
} | ||
|
||
struct Rng { | ||
state: u64, | ||
extra: u64, | ||
} | ||
|
||
impl Rng { | ||
fn new(seed: u64, extra: u64) -> Self { | ||
Self { state: seed, extra } | ||
} | ||
|
||
fn rand_range(&mut self, range: core::ops::Range<u64>) -> u64 { | ||
self.rand_u64() % (range.end - range.start) + range.start | ||
} | ||
|
||
fn rand_u64(&mut self) -> u64 { | ||
self.state = calculate_hash(&(self.state, self.extra)); | ||
self.state | ||
} | ||
} | ||
|
||
// `calculate_hash` is from `core/src/hash/mod.rs`. | ||
fn calculate_hash<T: core::hash::Hash>(t: &T) -> u64 { | ||
let mut s = DefaultHasher::new(); | ||
t.hash(&mut s); | ||
s.finish() | ||
} |
This file contains 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
Oops, something went wrong.