From e2d890b6fdf4a8f2f60e6ffd530f001ac76a58ec Mon Sep 17 00:00:00 2001 From: Max Booth Date: Fri, 18 Oct 2024 14:59:55 -0400 Subject: [PATCH] add serialization benchmark --- Cargo.toml | 7 +++++- benches/image_compression.rs | 1 + benches/serialize.rs | 44 ++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 benches/serialize.rs diff --git a/Cargo.toml b/Cargo.toml index 162cabee..a38ce5f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -80,6 +80,11 @@ trybuild = "1.0.96" name = "prefix_sum" harness = false +# TODO: Fix bench +# [[bench]] +# name = "image_compression" +# harness = false + [[bench]] -name = "image_compression" +name = "serialize" harness = false diff --git a/benches/image_compression.rs b/benches/image_compression.rs index ff4780b4..d025caf0 100644 --- a/benches/image_compression.rs +++ b/benches/image_compression.rs @@ -227,6 +227,7 @@ fn mean(numbers: &[f64]) -> f64 { } fn compression_benchmark(c: &mut Criterion) { + // TODO: replace file path with in-memory image (or add an image that is safe to distribute with the codebase) // https://qoiformat.org/benchmark/ let files = fs::read_dir("/home/rasputin/qoi_benchmark_images/screenshot_web/") .unwrap() diff --git a/benches/serialize.rs b/benches/serialize.rs new file mode 100644 index 00000000..475cd07e --- /dev/null +++ b/benches/serialize.rs @@ -0,0 +1,44 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use criterion::black_box; +use criterion::criterion_group; +use criterion::criterion_main; +use criterion::Criterion; +use rkyv::rancor::Error as RancorError; +use wprs::serialization::wayland::DataToTransfer; + +// benchmark to make sure we stay performant at serializing/deserializing large buffers. +// this means there should be zero copy, zero validation. +fn serialization_benchmark(c: &mut Criterion) { + let i: u32 = 100 * 1024 * 1024; + let buf = (0..i).map(|i| (i % 256) as u8).collect::>(); + let message = DataToTransfer(buf); + + c.bench_function("serialize", |b| { + b.iter(|| black_box(rkyv::to_bytes::(&message))); + }); + + let archived_message = rkyv::to_bytes::(&message).unwrap(); + c.bench_function("deserialize", |b| { + b.iter(|| { + black_box(rkyv::from_bytes::( + &archived_message[..], + )) + }); + }); +} + +criterion_group!(benches, serialization_benchmark); +criterion_main!(benches);