use std::sync::Arc;
use std::thread;
use std::time::Instant;
use mpmc_queue::queue::{BoundedQueue, MpmcQueue};
fn benchmark(capacity: usize, producers: usize, consumers: usize) {
let q = Arc::new(MpmcQueue::new(capacity));
let total_items = 1_000_000; let items_per_producer = total_items / producers;
let items_per_consumer = total_items / consumers;
let start = Instant::now();
let mut handles = vec![];
for _ in 0..producers {
let q = Arc::clone(&q);
handles.push(thread::spawn(move || {
for i in 0..items_per_producer {
q.push(i);
}
}));
}
for _ in 0..consumers {
let q = Arc::clone(&q);
handles.push(thread::spawn(move || {
for _ in 0..items_per_consumer {
q.pop();
}
}));
}
for h in handles {
h.join().unwrap();
}
let duration = start.elapsed();
println!(
"Cap: {}, P: {}, C: {} → {:.2} ops/sec",
capacity,
producers,
consumers,
total_items as f64 / duration.as_secs_f64()
);
}
fn main() {
let capacities = [64, 256, 1024];
let thread_counts = [1, 2, 4, 8, 16];
for &cap in &capacities {
for &t in &thread_counts {
benchmark(cap, t, t);
}
benchmark(cap, 8, 2);
benchmark(cap, 2, 8);
println!("----------------------------------");
}
}