-
Notifications
You must be signed in to change notification settings - Fork 180
Add framing benches #2051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add framing benches #2051
Changes from all commits
f919b76
2b19ee0
b7e6369
57a3652
5071a51
08a97e8
afc99d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| use binary_sv2::{self, Serialize}; | ||
| use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; | ||
| use framing_sv2::{framing::Sv2Frame, header::Header}; | ||
|
|
||
| #[cfg(not(feature = "with_buffer_pool"))] | ||
| type Slice = Vec<u8>; | ||
|
|
||
| #[cfg(feature = "with_buffer_pool")] | ||
| type Slice = buffer_sv2::Slice; | ||
|
|
||
| #[cfg(feature = "with_buffer_pool")] | ||
| const BACKEND: &str = "buffer_pool"; | ||
|
|
||
| #[cfg(not(feature = "with_buffer_pool"))] | ||
| const BACKEND: &str = "vec"; | ||
|
|
||
| const PAYLOAD_SIZES: &[usize] = &[64, 1024, 16 * 1024, 60 * 1024]; | ||
|
|
||
| fn payload(size: usize) -> Vec<u8> { | ||
| let len = size as u32; | ||
| let mut ve = Vec::with_capacity(6 + size); | ||
|
|
||
| ve.push(0); | ||
| ve.push(0); | ||
| ve.push(0); | ||
|
|
||
| ve.push((len & 0xFF) as u8); | ||
| ve.push(((len >> 8) & 0xFF) as u8); | ||
| ve.push(((len >> 16) & 0xFF) as u8); | ||
|
|
||
| ve.extend(std::iter::repeat(2u8).take(size)); | ||
|
|
||
| ve | ||
| } | ||
|
|
||
| #[derive(Serialize, Clone)] | ||
| struct Tester { | ||
| _a: Vec<u8>, | ||
| } | ||
|
|
||
| fn tester(size: usize) -> Tester { | ||
| Tester { | ||
| _a: vec![2u8; size], | ||
| } | ||
| } | ||
|
|
||
| fn bench_from_message(c: &mut Criterion) { | ||
| let mut group = c.benchmark_group(format!("sv2frame::from_message::{BACKEND}")); | ||
|
|
||
| for &size in PAYLOAD_SIZES { | ||
| group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, &size| { | ||
| b.iter(|| { | ||
| Sv2Frame::<Vec<u8>, Slice>::from_message(black_box(payload(size)), 1, 0, false) | ||
| .unwrap() | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| group.finish(); | ||
| } | ||
|
|
||
| fn bench_serialize_fresh(c: &mut Criterion) { | ||
| let mut group = c.benchmark_group(format!("sv2frame::serialize_fresh::{BACKEND}")); | ||
|
|
||
| for &size in PAYLOAD_SIZES { | ||
| let frame = Sv2Frame::<Tester, Slice>::from_message(tester(size), 1, 0, false).unwrap(); | ||
|
|
||
| let mut buf = vec![0u8; frame.encoded_length()]; | ||
|
|
||
| group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, _| { | ||
| b.iter(|| { | ||
| frame.clone().serialize(&mut buf).unwrap(); | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| group.finish(); | ||
| } | ||
|
|
||
| fn bench_serialize_fast(c: &mut Criterion) { | ||
| let mut group = c.benchmark_group(format!("sv2frame::serialize_fast::{BACKEND}")); | ||
|
|
||
| for &size in PAYLOAD_SIZES { | ||
| let frame = Sv2Frame::<Tester, Slice>::from_message(tester(size), 1, 0, false).unwrap(); | ||
|
|
||
| let mut buf = vec![0u8; frame.encoded_length()]; | ||
| frame.clone().serialize(&mut buf).unwrap(); | ||
|
|
||
| group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, _| { | ||
| b.iter(|| { | ||
| frame.clone().serialize(black_box(&mut buf)).unwrap(); | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| group.finish(); | ||
| } | ||
|
|
||
| fn bench_from_bytes(c: &mut Criterion) { | ||
| let mut group = c.benchmark_group(format!("sv2frame::from_bytes::{BACKEND}")); | ||
|
|
||
| for &size in PAYLOAD_SIZES { | ||
| group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, _| { | ||
| b.iter(|| Sv2Frame::<Vec<u8>, _>::from_bytes(black_box(payload(size))).unwrap()) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aren't we also computing the time for the |
||
| }); | ||
| } | ||
|
|
||
| group.finish(); | ||
| } | ||
|
|
||
| fn bench_size_hint(c: &mut Criterion) { | ||
| let mut group = c.benchmark_group(format!("sv2frame::size_hint::{BACKEND}")); | ||
|
|
||
| for &size in PAYLOAD_SIZES { | ||
| group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, _| { | ||
| b.iter(|| black_box(Sv2Frame::<Vec<u8>, Slice>::size_hint(&payload(size)))) | ||
| }); | ||
| } | ||
|
|
||
| group.finish(); | ||
| } | ||
|
|
||
| fn bench_encrypted_len(c: &mut Criterion) { | ||
| let mut group = c.benchmark_group(format!("sv2frame::encrypted_len::{BACKEND}")); | ||
|
|
||
| for &size in PAYLOAD_SIZES { | ||
| let header = Header::from_bytes(&payload(size)).unwrap(); | ||
| group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, _| { | ||
| b.iter(|| black_box(header.encrypted_len())) | ||
| }); | ||
| } | ||
|
|
||
| group.finish(); | ||
| } | ||
|
|
||
| criterion_group!( | ||
| framing, | ||
| bench_from_message, | ||
| bench_serialize_fresh, | ||
| bench_serialize_fast, | ||
| bench_from_bytes, | ||
| bench_size_hint, | ||
| bench_encrypted_len | ||
| ); | ||
|
|
||
| criterion_main!(framing); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,14 +120,13 @@ impl Header { | |
| /// | ||
| /// The calculated length includes the full payload length and any additional space required | ||
| /// for the MACs. | ||
| #[allow(clippy::manual_div_ceil)] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. on my machine div_ceil was quite slow compared to current version. |
||
| pub fn encrypted_len(&self) -> usize { | ||
| let len = self.len(); | ||
| let mut chunks = len / (SV2_FRAME_CHUNK_SIZE - AEAD_MAC_LEN); | ||
| if len % (SV2_FRAME_CHUNK_SIZE - AEAD_MAC_LEN) != 0 { | ||
| chunks += 1; | ||
| } | ||
| let mac_len = chunks * AEAD_MAC_LEN; | ||
| len + mac_len | ||
| let payload_per_chunk = SV2_FRAME_CHUNK_SIZE - AEAD_MAC_LEN; | ||
|
|
||
| let chunks = (len + payload_per_chunk - 1) / payload_per_chunk; | ||
| len + chunks * AEAD_MAC_LEN | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shoudn't this be using
payload()?