Skip to content

Commit 79c9920

Browse files
committed
Added Benchmark that tests serialization and deserialization of an enum.
Previous benchmarks were concerned about empty sending and empty receiving or sending bytes directly. This will now test the serialization. Signed-off-by: Narfinger <[email protected]>
1 parent d918d57 commit 79c9920

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ harness = false
2323
name = "ipc_shared_mem"
2424
harness = false
2525

26+
[[bench]]
27+
name = "struct_ipc"
28+
harness = false
29+
2630
[features]
2731
default = []
2832
force-inprocess = []
@@ -52,6 +56,7 @@ crossbeam-utils = "0.8"
5256
futures-test = "0.3"
5357
static_assertions = "1.1.0"
5458
criterion = { version = "0.5", features = ["html_reports"] }
59+
rand = "0.9.2"
5560

5661
[target.'cfg(target_os = "windows")'.dependencies.windows]
5762
version = "0.61"

benches/ipc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,5 @@ criterion_group!(
7070
transfer_receivers<8>,
7171
transfer_receivers<64>,
7272
);
73+
7374
criterion_main!(benches);

benches/struct_ipc.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use criterion::{criterion_group, criterion_main, Criterion};
2+
use ipc_channel::ipc;
3+
use rand::prelude::*;
4+
use serde::{Deserialize, Serialize};
5+
6+
#[derive(Clone, Serialize, Deserialize)]
7+
enum Test {
8+
Msg0,
9+
Msg1,
10+
Msg2(u32, u32),
11+
Msg3(u32, u32, u32),
12+
Msg4(String),
13+
Msg5,
14+
Msg6,
15+
}
16+
17+
impl Test {
18+
fn new(rng: &mut ThreadRng) -> Self {
19+
match rng.random_range(0..=6) {
20+
0 => Self::Msg0,
21+
1 => Self::Msg1,
22+
2 => Self::Msg2(23, 42),
23+
3 => Self::Msg3(23, 42, 243),
24+
4 => Self::Msg4(String::from("This is a test string of medium size")),
25+
5 => Self::Msg5,
26+
6 => Self::Msg6,
27+
_ => Self::Msg6,
28+
}
29+
}
30+
}
31+
32+
fn transfer_simple_struct(criterion: &mut Criterion) {
33+
criterion.bench_function("transfer_simple_struct", |bencher| {
34+
let (tx, rx) = ipc::channel().unwrap();
35+
let mut rng = rand::rng();
36+
37+
bencher.iter_batched(
38+
|| Test::new(&mut rng),
39+
|s| {
40+
tx.send(s).unwrap();
41+
rx.recv().unwrap();
42+
},
43+
criterion::BatchSize::SmallInput,
44+
);
45+
});
46+
}
47+
48+
criterion_group!(benches, transfer_simple_struct,);
49+
50+
criterion_main!(benches);

0 commit comments

Comments
 (0)