Skip to content

Commit

Permalink
bench/test
Browse files Browse the repository at this point in the history
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
  • Loading branch information
sagudev committed Oct 30, 2024
1 parent dc0b954 commit 0b3bb83
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ harness = false
name = "ipc_receiver_set"
harness = false

[[bench]]
name = "ipc_shared_mem"
harness = false

[features]
default = []
force-inprocess = []
Expand Down
65 changes: 65 additions & 0 deletions benches/ipc_shared_mem.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::time::Instant;

use criterion::{criterion_group, criterion_main, Criterion};
use ipc_channel::ipc::{self, IpcSharedMemory};

#[inline]
fn on_recv<const MUT: bool>(mut ism: IpcSharedMemory) -> IpcSharedMemory {
if MUT {
let data = unsafe { ism.deref_mut() };
for d in data {
*d += 1;
}
ism
} else {
let mut data = ism.to_vec();
for d in &mut data {
*d += 1;
}
IpcSharedMemory::from_bytes(&data)
}
}

fn ping_pong_mut_shared_mem<const MUT: bool, const SIZE: usize, const COUNT: u8>(
criterion: &mut Criterion,
) {
criterion.bench_function(
&format!(
"ping_pong_shared_mem{}_{SIZE}_{COUNT}",
if MUT { "_mut" } else { "" }
),
|bencher| {
bencher.iter_custom(|_| {
let (tx1, rx1) = ipc::channel().unwrap();
let (tx2, rx2) = ipc::channel().unwrap();
let tx = tx1.clone();
let _t1 = std::thread::spawn(move || {
for _i in 0..=COUNT / 2 {
tx2.send(on_recv::<MUT>(rx1.recv().unwrap())).unwrap();
}
});
let t2 = std::thread::spawn(move || {
for _i in 0..COUNT / 2 {
tx1.send(on_recv::<MUT>(rx2.recv().unwrap())).unwrap();
}
rx2.recv().unwrap().to_vec()
});
let start = Instant::now();
tx.send(IpcSharedMemory::from_byte(0, SIZE)).unwrap();
let data = t2.join().unwrap();
let duration = start.elapsed();
assert!(data.iter().all(|d| *d == (COUNT / 2) * 2 + 1));
duration
});
},
);
}

criterion_group!(
benches,
ping_pong_mut_shared_mem<true, {4*1024*1024}, 100>,
ping_pong_mut_shared_mem<false, {4*1024*1024}, 100>,
ping_pong_mut_shared_mem<true, {4*1024*1024}, 125>,
ping_pong_mut_shared_mem<false, {4*1024*1024}, 125>,
);
criterion_main!(benches);

0 comments on commit 0b3bb83

Please sign in to comment.