Skip to content

Commit a98a309

Browse files
committed
add benchmarks for 1, 2, 4, 8, 16 threads
1 parent 3914a7b commit a98a309

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

library/std/src/time/tests.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{Duration, Instant, SystemTime, UNIX_EPOCH};
2+
use test::{black_box, Bencher};
23

34
macro_rules! assert_almost_eq {
45
($a:expr, $b:expr) => {{
@@ -188,3 +189,45 @@ fn since_epoch() {
188189
let hundred_twenty_years = thirty_years * 4;
189190
assert!(a < hundred_twenty_years);
190191
}
192+
193+
macro_rules! bench_instant_threaded {
194+
($bench_name:ident, $thread_count:expr) => {
195+
#[bench]
196+
fn $bench_name(b: &mut Bencher) -> crate::thread::Result<()> {
197+
use crate::sync::atomic::{AtomicBool, Ordering};
198+
use crate::sync::Arc;
199+
200+
let running = Arc::new(AtomicBool::new(true));
201+
202+
let threads: Vec<_> = (0..$thread_count)
203+
.map(|_| {
204+
let flag = Arc::clone(&running);
205+
crate::thread::spawn(move || {
206+
while flag.load(Ordering::Relaxed) {
207+
black_box(Instant::now());
208+
}
209+
})
210+
})
211+
.collect();
212+
213+
b.iter(|| {
214+
let a = Instant::now();
215+
let b = Instant::now();
216+
assert!(b >= a);
217+
});
218+
219+
running.store(false, Ordering::Relaxed);
220+
221+
for t in threads {
222+
t.join()?;
223+
}
224+
Ok(())
225+
}
226+
};
227+
}
228+
229+
bench_instant_threaded!(instant_contention_01_threads, 0);
230+
bench_instant_threaded!(instant_contention_02_threads, 1);
231+
bench_instant_threaded!(instant_contention_04_threads, 3);
232+
bench_instant_threaded!(instant_contention_08_threads, 7);
233+
bench_instant_threaded!(instant_contention_16_threads, 15);

0 commit comments

Comments
 (0)