-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from mchhoy/message_serialization_helpers
Message serialization helpers
- Loading branch information
Showing
4 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters