Skip to content

Commit

Permalink
Test without joining threads, by using a separate counter for each test
Browse files Browse the repository at this point in the history
  • Loading branch information
Pr0methean committed Jan 21, 2024
1 parent d25559c commit d42f3ae
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions benches/iai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@ macro_rules! single_thread_bench_iai {
}
}

static ITERATIONS_LEFT: AtomicU64 = AtomicU64::new(0);

macro_rules! contended_bench_iai {
($n:expr) => {
contended_bench_iai!($n, num_cpus::get_physical());
};
($n:expr, $threads:expr) => {
paste! {
fn [< contended_bench_ $n _shared_buffer >]() {
ITERATIONS_LEFT.store(2 * RESEEDING_THRESHOLD * $n.max(1), SeqCst);
let iterations_left = Arc::new(AtomicU64::new(2 * RESEEDING_THRESHOLD * $n.max(1)));
let root = BenchmarkSharedBufferRng::<$n>::new(OsRng::default());
let rngs: Vec<_> = (0..$threads)
.map(|_| root.new_standard_rng(RESEEDING_THRESHOLD))
Expand All @@ -55,23 +53,21 @@ macro_rules! contended_bench_iai {
drop(root);
let background_threads: Vec<_> = rngs.into_iter()
.map(|mut rng| {
let iterations_left = iterations_left.clone();
spawn(move || {
while ITERATIONS_LEFT.fetch_sub(1, SeqCst) > 0 {
while iterations_left.fetch_sub(1, SeqCst) > 0 {
black_box(rng.next_u64());
}
})
})
.collect();
while ITERATIONS_LEFT.fetch_sub(1, SeqCst) > 0 {
while iterations_left.fetch_sub(1, SeqCst) > 0 {
black_box(main_thread_rng.next_u64());
}
background_threads
.into_iter()
.for_each(|handle| handle.join().unwrap());
}

fn [< contended_bench_ $n _local_buffer >]() {
ITERATIONS_LEFT.store(2 * RESEEDING_THRESHOLD * $n.max(1), SeqCst);
let iterations_left = Arc::new(AtomicU64::new(2 * RESEEDING_THRESHOLD * $n.max(1)));
let rngs: Vec<_> = (0..$threads)
.map(|_| {
let mut buffer = BlockRng64::new(RngBufferCore::<$n, OsRng>(OsRng::default()));
Expand All @@ -83,19 +79,17 @@ macro_rules! contended_bench_iai {
let main_thread_rng = rngs.pop().unwrap();
let background_threads: Vec<_> = rngs.into_iter()
.map(|mut rng| {
let iterations_left = iterations_left.clone();
spawn(move || {
while ITERATIONS_LEFT.fetch_sub(1, SeqCst) > 0 {
black_box(rng.next_u64());
}
})
})
.collect();
while ITERATIONS_LEFT.fetch_sub(1, SeqCst) > 0 {
while iterations_left.fetch_sub(1, SeqCst) > 0 {
black_box(main_thread_rng.next_u64());
}
background_threads
.into_iter()
.for_each(|handle| handle.join().unwrap());
}
}
};
Expand Down

0 comments on commit d42f3ae

Please sign in to comment.