Skip to content

Commit

Permalink
Add deserialization benchmark.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-dahl committed Dec 13, 2023
1 parent eb1f461 commit fb08e1f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions r2r/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ futures = "0.3.25"
tokio = { version = "1.22.0", features = ["rt-multi-thread", "time", "macros"] }
rand = "0.8.5"
cdr = "0.2.4"
criterion = "0.5.1"

[build-dependencies]
r2r_common = { path = "../r2r_common", version = "0.8.2" }
Expand All @@ -51,3 +52,7 @@ doc-only = ["r2r_common/doc-only", "r2r_rcl/doc-only", "r2r_msg_gen/doc-only", "

[package.metadata.docs.rs]
features = ["doc-only"]

[[bench]]
name = "deserialization"
harness = false
57 changes: 57 additions & 0 deletions r2r/benches/deserialization.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use criterion::{criterion_group, criterion_main, Criterion};
use r2r::*;
use rand::Rng;
use rand::thread_rng;

const NUM_ELEMENTS: usize = 10_000;
const NUM_TIMES: usize = 1_000;

fn bench_ros_deserialization() {
let mut rng = thread_rng();
let mut numbers = Vec::<i32>::with_capacity(NUM_ELEMENTS);

for _ in 0..NUM_ELEMENTS {
numbers.push(rng.gen_range(0..i32::MAX));
}
let message = std_msgs::msg::Int32MultiArray {
layout: std_msgs::msg::MultiArrayLayout::default(),
data: numbers
};

let bytes = message.to_serialized_bytes().unwrap();

for _ in 0..NUM_TIMES {
let _ = std_msgs::msg::Int32MultiArray::from_serialized_bytes(&bytes).unwrap();
}
}

fn bench_cdr_deserialization() {
let mut rng = thread_rng();
let mut numbers = Vec::<i32>::with_capacity(NUM_ELEMENTS);

for _ in 0..NUM_ELEMENTS {
numbers.push(rng.gen_range(0..i32::MAX));
}
let message = std_msgs::msg::Int32MultiArray {
layout: std_msgs::msg::MultiArrayLayout::default(),
data: numbers
};

let bytes = message.to_serialized_bytes().unwrap();

for _ in 0..NUM_TIMES {
let _msg1 = cdr::deserialize::<std_msgs::msg::Int32MultiArray>(&bytes).unwrap();
// just for testing that we get the same result.
// let msg2 = std_msgs::msg::Int32MultiArray::from_serialized_bytes(&bytes).unwrap();
// assert_eq!(msg1, msg2);
// assert_eq!(msg2, message);
}
}

pub fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("ros_deserialization", |b| b.iter(|| bench_ros_deserialization()));
c.bench_function("cdr_deserialization", |b| b.iter(|| bench_cdr_deserialization()));
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

0 comments on commit fb08e1f

Please sign in to comment.