about summary refs log tree commit diff
path: root/library/std/benches
diff options
context:
space:
mode:
authorbjorn3 <17426603+bjorn3@users.noreply.github.com>2025-01-17 09:49:46 +0000
committerbjorn3 <17426603+bjorn3@users.noreply.github.com>2025-01-26 10:28:04 +0000
commit4ce917dfd5182d78c7351092473de1960efc9626 (patch)
tree1c979c96b26e0e9ad77e1bbe74548774694e18fe /library/std/benches
parentb8fa843a1a60146b93ca5b1d11bbe23c1b1076f3 (diff)
downloadrust-4ce917dfd5182d78c7351092473de1960efc9626.tar.gz
rust-4ce917dfd5182d78c7351092473de1960efc9626.zip
Move std::time unit tests to integration tests
Diffstat (limited to 'library/std/benches')
-rw-r--r--library/std/benches/lib.rs1
-rw-r--r--library/std/benches/time.rs47
2 files changed, 48 insertions, 0 deletions
diff --git a/library/std/benches/lib.rs b/library/std/benches/lib.rs
index ae000d24d28..e749d9c0f79 100644
--- a/library/std/benches/lib.rs
+++ b/library/std/benches/lib.rs
@@ -6,3 +6,4 @@ extern crate test;
 
 mod hash;
 mod path;
+mod time;
diff --git a/library/std/benches/time.rs b/library/std/benches/time.rs
new file mode 100644
index 00000000000..552481cad92
--- /dev/null
+++ b/library/std/benches/time.rs
@@ -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);