Skip to content

Commit

Permalink
Move std::time unit tests to integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 authored and gitbot committed Feb 20, 2025
1 parent 2ef34e0 commit ec2a17a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 53 deletions.
1 change: 1 addition & 0 deletions std/benches/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ extern crate test;

mod hash;
mod path;
mod time;
47 changes: 47 additions & 0 deletions std/benches/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::time::Instant;

#[cfg(not(target_arch = "wasm32"))]
use test::{Bencher, black_box};

macro_rules! bench_instant_threaded {
($bench_name:ident, $thread_count:expr) => {
#[bench]
#[cfg(not(target_arch = "wasm32"))]
fn $bench_name(b: &mut Bencher) -> std::thread::Result<()> {
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

let running = Arc::new(AtomicBool::new(true));

let threads: Vec<_> = (0..$thread_count)
.map(|_| {
let flag = Arc::clone(&running);
std::thread::spawn(move || {
while flag.load(Ordering::Relaxed) {
black_box(Instant::now());
}
})
})
.collect();

b.iter(|| {
let a = Instant::now();
let b = Instant::now();
assert!(b >= a);
});

running.store(false, Ordering::Relaxed);

for t in threads {
t.join()?;
}
Ok(())
}
};
}

bench_instant_threaded!(instant_contention_01_threads, 0);
bench_instant_threaded!(instant_contention_02_threads, 1);
bench_instant_threaded!(instant_contention_04_threads, 3);
bench_instant_threaded!(instant_contention_08_threads, 7);
bench_instant_threaded!(instant_contention_16_threads, 15);
3 changes: 0 additions & 3 deletions std/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
#![stable(feature = "time", since = "1.3.0")]

#[cfg(test)]
mod tests;

#[stable(feature = "time", since = "1.3.0")]
pub use core::time::Duration;
#[stable(feature = "duration_checked_float", since = "1.66.0")]
Expand Down
55 changes: 5 additions & 50 deletions std/src/time/tests.rs → std/tests/time.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use core::fmt::Debug;
#![feature(duration_constants)]

#[cfg(not(target_arch = "wasm32"))]
use test::{Bencher, black_box};

use super::{Duration, Instant, SystemTime, UNIX_EPOCH};
use std::fmt::Debug;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

macro_rules! assert_almost_eq {
($a:expr, $b:expr) => {{
Expand All @@ -29,10 +27,10 @@ fn instant_monotonic() {

#[test]
#[cfg(not(target_arch = "wasm32"))]
fn instant_monotonic_concurrent() -> crate::thread::Result<()> {
fn instant_monotonic_concurrent() -> std::thread::Result<()> {
let threads: Vec<_> = (0..8)
.map(|_| {
crate::thread::spawn(|| {
std::thread::spawn(|| {
let mut old = Instant::now();
let count = if cfg!(miri) { 1_000 } else { 5_000_000 };
for _ in 0..count {
Expand Down Expand Up @@ -229,46 +227,3 @@ fn big_math() {
check(instant.checked_add(Duration::from_secs(100)), Instant::checked_sub);
check(instant.checked_add(Duration::from_secs(i64::MAX as _)), Instant::checked_sub);
}

macro_rules! bench_instant_threaded {
($bench_name:ident, $thread_count:expr) => {
#[bench]
#[cfg(not(target_arch = "wasm32"))]
fn $bench_name(b: &mut Bencher) -> crate::thread::Result<()> {
use crate::sync::Arc;
use crate::sync::atomic::{AtomicBool, Ordering};

let running = Arc::new(AtomicBool::new(true));

let threads: Vec<_> = (0..$thread_count)
.map(|_| {
let flag = Arc::clone(&running);
crate::thread::spawn(move || {
while flag.load(Ordering::Relaxed) {
black_box(Instant::now());
}
})
})
.collect();

b.iter(|| {
let a = Instant::now();
let b = Instant::now();
assert!(b >= a);
});

running.store(false, Ordering::Relaxed);

for t in threads {
t.join()?;
}
Ok(())
}
};
}

bench_instant_threaded!(instant_contention_01_threads, 0);
bench_instant_threaded!(instant_contention_02_threads, 1);
bench_instant_threaded!(instant_contention_04_threads, 3);
bench_instant_threaded!(instant_contention_08_threads, 7);
bench_instant_threaded!(instant_contention_16_threads, 15);

0 comments on commit ec2a17a

Please sign in to comment.