|
1 | 1 | use super::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
| 2 | +use test::{black_box, Bencher}; |
2 | 3 |
|
3 | 4 | macro_rules! assert_almost_eq {
|
4 | 5 | ($a:expr, $b:expr) => {{
|
@@ -188,3 +189,45 @@ fn since_epoch() {
|
188 | 189 | let hundred_twenty_years = thirty_years * 4;
|
189 | 190 | assert!(a < hundred_twenty_years);
|
190 | 191 | }
|
| 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