ipc-ring48 1.0.1

A bounded 48-byte POSIX shared-memory SPSC queue for Rust.
Documentation
use criterion::{BatchSize, Criterion, criterion_group, criterion_main};
use ipc_ring48::{Consumer, Producer, unlink};
use std::hint::black_box;
use std::time::Duration;

const BENCH_CAPACITY: usize = 1024;
const BATCH_MESSAGES: usize = 1024;

fn fresh_queue() -> (Producer, Consumer) {
    let _ = unlink();
    let producer = Producer::create_or_open(BENCH_CAPACITY).unwrap();
    let consumer = Consumer::open().unwrap();
    (producer, consumer)
}

fn bench_library_push(c: &mut Criterion) {
    c.bench_function("library_push_1024_messages", |b| {
        b.iter_batched(
            fresh_queue,
            |(producer, _consumer)| {
                for _ in 0..BATCH_MESSAGES {
                    producer.push(black_box([1_u8; 48])).unwrap();
                }
                let _ = unlink();
            },
            BatchSize::PerIteration,
        );
    });
}

fn bench_library_pop(c: &mut Criterion) {
    c.bench_function("library_pop_1024_messages", |b| {
        b.iter_batched(
            || {
                let (producer, consumer) = fresh_queue();
                for _ in 0..BATCH_MESSAGES {
                    producer.push([2_u8; 48]).unwrap();
                }
                (producer, consumer)
            },
            |(_producer, consumer)| {
                for _ in 0..BATCH_MESSAGES {
                    black_box(consumer.pop().unwrap());
                }
                let _ = unlink();
            },
            BatchSize::PerIteration,
        );
    });
}

fn bench_library_push_then_pop(c: &mut Criterion) {
    c.bench_function("library_push_then_pop", |b| {
        b.iter_batched(
            fresh_queue,
            |(producer, consumer)| {
                producer.push(black_box([3_u8; 48])).unwrap();
                black_box(consumer.pop().unwrap());
                let _ = unlink();
            },
            BatchSize::PerIteration,
        );
    });
}

fn bench_open_existing(c: &mut Criterion) {
    c.bench_function("open_existing_producer_mapping", |b| {
        b.iter_batched(
            || {
                let _ = unlink();
                Producer::create_or_open(BENCH_CAPACITY).unwrap()
            },
            |owner| {
                black_box(owner.capacity());
                black_box(Producer::open().unwrap());
                drop(owner);
                let _ = unlink();
            },
            BatchSize::PerIteration,
        );
    });

    c.bench_function("open_existing_consumer_mapping", |b| {
        b.iter_batched(
            || {
                let _ = unlink();
                Producer::create_or_open(BENCH_CAPACITY).unwrap()
            },
            |owner| {
                black_box(owner.capacity());
                black_box(Consumer::open().unwrap());
                drop(owner);
                let _ = unlink();
            },
            BatchSize::PerIteration,
        );
    });
}

fn criterion_config() -> Criterion {
    Criterion::default()
        .warm_up_time(Duration::from_millis(500))
        .measurement_time(Duration::from_secs(2))
}

criterion_group! {
    name = benches;
    config = criterion_config();
    targets = bench_library_push, bench_library_pop, bench_library_push_then_pop, bench_open_existing
}
criterion_main!(benches);