Skip to content

Commit

Permalink
Sort tests at compile time, not at startup
Browse files Browse the repository at this point in the history
Recently, another Miri user was trying to run `cargo miri test` on the
crate `iced-x86` with `--features=code_asm,mvex`. This configuration has
a startup time of ~18 minutes. That's ~18 minutes before any tests even
start to run. The fact that this crate has over 26,000 tests and Miri is
slow makes a lot of code which is otherwise a bit sloppy but fine into a
huge runtime issue.

Sorting the tests when the test harness is created instead of at startup
time knocks just under 4 minutes out of those ~18 minutes. I have ways
to remove most of the rest of the startup time, but this change requires
coordinating changes of both the compiler and libtest, so I'm sending it
separately.
  • Loading branch information
saethlin committed Sep 1, 2022
1 parent c233ee6 commit 01550d2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 38 deletions.
2 changes: 1 addition & 1 deletion test/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub fn list_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Res
let mut ntest = 0;
let mut nbench = 0;

for test in filter_tests(&opts, tests) {
for test in filter_tests(&opts, tests).into_iter() {
use crate::TestFn::*;

let TestDescAndFn { desc: TestDesc { name, .. }, testfn } = test;
Expand Down
17 changes: 7 additions & 10 deletions test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,20 +242,20 @@ where
let event = TestEvent::TeFiltered(filtered_descs, shuffle_seed);
notify_about_test_event(event)?;

let (filtered_tests, filtered_benchs): (Vec<_>, _) = filtered_tests
let (mut filtered_tests, filtered_benchs): (Vec<_>, _) = filtered_tests
.into_iter()
.enumerate()
.map(|(i, e)| (TestId(i), e))
.partition(|(_, e)| matches!(e.testfn, StaticTestFn(_) | DynTestFn(_)));

let concurrency = opts.test_threads.unwrap_or_else(get_concurrency);

let mut remaining = filtered_tests;
if let Some(shuffle_seed) = shuffle_seed {
shuffle_tests(shuffle_seed, &mut remaining);
} else {
remaining.reverse();
shuffle_tests(shuffle_seed, &mut filtered_tests);
}
// Store the tests in a VecDeque so we can efficiently remove the first element to run the
// tests in the order they were passed (unless shuffled).
let mut remaining = VecDeque::from(filtered_tests);
let mut pending = 0;

let (tx, rx) = channel::<CompletedTest>();
Expand Down Expand Up @@ -295,7 +295,7 @@ where

if concurrency == 1 {
while !remaining.is_empty() {
let (id, test) = remaining.pop().unwrap();
let (id, test) = remaining.pop_front().unwrap();
let event = TestEvent::TeWait(test.desc.clone());
notify_about_test_event(event)?;
let join_handle =
Expand All @@ -309,7 +309,7 @@ where
} else {
while pending > 0 || !remaining.is_empty() {
while pending < concurrency && !remaining.is_empty() {
let (id, test) = remaining.pop().unwrap();
let (id, test) = remaining.pop_front().unwrap();
let timeout = time::get_default_test_timeout();
let desc = test.desc.clone();

Expand Down Expand Up @@ -421,9 +421,6 @@ pub fn filter_tests(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> Vec<TestDescA
RunIgnored::No => {}
}

// Sort the tests alphabetically
filtered.sort_by(|t1, t2| t1.desc.name.as_slice().cmp(t2.desc.name.as_slice()));

filtered
}

Expand Down
27 changes: 0 additions & 27 deletions test/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,33 +600,6 @@ fn sample_tests() -> Vec<TestDescAndFn> {
tests
}

#[test]
pub fn sort_tests() {
let mut opts = TestOpts::new();
opts.run_tests = true;

let tests = sample_tests();
let filtered = filter_tests(&opts, tests);

let expected = vec![
"isize::test_pow".to_string(),
"isize::test_to_str".to_string(),
"sha1::test".to_string(),
"test::do_not_run_ignored_tests".to_string(),
"test::filter_for_ignored_option".to_string(),
"test::first_free_arg_should_be_a_filter".to_string(),
"test::ignored_tests_result_in_ignored".to_string(),
"test::parse_ignored_flag".to_string(),
"test::parse_include_ignored_flag".to_string(),
"test::run_include_ignored_option".to_string(),
"test::sort_tests".to_string(),
];

for (a, b) in expected.iter().zip(filtered) {
assert_eq!(*a, b.desc.name.to_string());
}
}

#[test]
pub fn shuffle_tests() {
let mut opts = TestOpts::new();
Expand Down

0 comments on commit 01550d2

Please sign in to comment.