#![allow(deprecated)]
#![cfg(feature = "pinning")]
#![allow(
clippy::cast_possible_truncation,
clippy::needless_pass_by_value,
clippy::semicolon_if_nothing_returned
)]
use std::hint::black_box;
use std::sync::Arc;
use std::thread::scope;
use std::time::Duration;
use criterion::{criterion_group, criterion_main, Criterion};
use sefer_alloc::{PinnedRunner, ShardedRegion};
const INSERTS_PER_THREAD: usize = 4_000;
const THREAD_COUNTS: &[usize] = &[1, 2, 4];
fn bench_pinned_writes(c: &mut Criterion) {
let mut group = c.benchmark_group("pinned_write");
group.sample_size(10);
group.warm_up_time(Duration::from_millis(150));
group.measurement_time(Duration::from_millis(600));
for &threads in THREAD_COUNTS {
let label = format!("pinned/threads={threads}");
group.bench_function(&label, |b| {
b.iter(|| {
let region = Arc::new(ShardedRegion::<u64>::with_shards(
threads,
INSERTS_PER_THREAD,
));
let runner = PinnedRunner::with_workers(®ion, threads)
.expect("core_affinity::get_core_ids should succeed in a bench process");
assert_eq!(runner.worker_count(), threads);
runner.run_arc(®ion, |_shard_id, region| {
for v in 0..u64::try_from(INSERTS_PER_THREAD).unwrap() {
let _ = black_box(region.insert(v));
}
});
black_box(region);
});
});
}
group.finish();
}
fn bench_unpinned_writes(c: &mut Criterion) {
let mut group = c.benchmark_group("pinned_write");
group.sample_size(10);
group.warm_up_time(Duration::from_millis(150));
group.measurement_time(Duration::from_millis(600));
for &threads in THREAD_COUNTS {
let label = format!("unpinned/threads={threads}");
group.bench_function(&label, |b| {
b.iter(|| {
let region = Arc::new(ShardedRegion::<u64>::with_shards(
threads,
INSERTS_PER_THREAD,
));
scope(|s| {
for _ in 0..threads {
let region = Arc::clone(®ion);
s.spawn(move || {
for v in 0..u64::try_from(INSERTS_PER_THREAD).unwrap() {
let _ = black_box(region.insert(v));
}
});
}
});
black_box(region);
});
});
}
group.finish();
}
criterion_group!(benches, bench_pinned_writes, bench_unpinned_writes);
criterion_main!(benches);