From 954cfa322ef7bb08fcfdf7646ab0e772565dcc1d Mon Sep 17 00:00:00 2001 From: Vincent Herlemont Date: Sun, 29 Oct 2023 09:45:00 +0100 Subject: [PATCH] perf: refactor bench --- libraries/native_model/Cargo.toml | 8 -- libraries/native_model/benches/overhead.rs | 16 +--- .../benches/overhead_on_bincode.rs | 88 ------------------- .../native_model/benches/prepend_bytes.rs | 28 ------ libraries/native_model/justfile | 7 +- .../example/custom_codec/bincode_serde.rs | 16 ---- 6 files changed, 10 insertions(+), 153 deletions(-) delete mode 100644 libraries/native_model/benches/overhead_on_bincode.rs delete mode 100644 libraries/native_model/benches/prepend_bytes.rs diff --git a/libraries/native_model/Cargo.toml b/libraries/native_model/Cargo.toml index 63894fe5..be9950b8 100644 --- a/libraries/native_model/Cargo.toml +++ b/libraries/native_model/Cargo.toml @@ -37,13 +37,5 @@ default = ["serde", "bincode_1_3"] name = "overhead" harness = false -[[bench]] -name = "overhead_on_bincode" -harness = false - -[[bench]] -name = "prepend_bytes" -harness = false - [build-dependencies] skeptic = "0.13" \ No newline at end of file diff --git a/libraries/native_model/benches/overhead.rs b/libraries/native_model/benches/overhead.rs index a21750fa..80d88628 100644 --- a/libraries/native_model/benches/overhead.rs +++ b/libraries/native_model/benches/overhead.rs @@ -1,16 +1,8 @@ -use bincode::{Decode, Encode}; use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use native_model_macro::native_model; - -fn native_model_encode_body(obj: &T) -> Result, bincode::error::EncodeError> { - bincode::encode_to_vec(obj, bincode::config::standard()) -} - -fn native_model_decode_body(data: Vec) -> Result { - bincode::decode_from_slice(&data, bincode::config::standard()).map(|(result, _)| result) -} - -#[derive(Encode, Decode)] +use serde::{Deserialize, Serialize}; +use native_model::Model; +#[derive(Serialize, Deserialize)] #[native_model(id = 1, version = 1)] struct Data(Vec); @@ -31,7 +23,7 @@ fn criterion_benchmark(c: &mut Criterion) { // encode let data = Data(vec![1; nb_bytes]); - let mut encode_body = native_model_encode_body(&data).unwrap(); + let mut encode_body = data.native_model_encode_body().unwrap(); group.bench_function(BenchmarkId::new("encode", nb_bytes), |b| { b.iter(|| wrap(&mut encode_body)) }); diff --git a/libraries/native_model/benches/overhead_on_bincode.rs b/libraries/native_model/benches/overhead_on_bincode.rs deleted file mode 100644 index 48b1212a..00000000 --- a/libraries/native_model/benches/overhead_on_bincode.rs +++ /dev/null @@ -1,88 +0,0 @@ -use bincode::{Decode, Encode}; -use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use native_model_macro::native_model; - -#[derive(Encode, Decode)] -struct DataForBincode { - x: i32, - string: String, -} - -// Encode 1 data with bincode -fn native_model_encode_body(obj: &T) -> Result, bincode::error::EncodeError> { - bincode::encode_to_vec(obj, bincode::config::standard()) -} - -fn native_model_decode_body(data: Vec) -> Result { - bincode::decode_from_slice(&data, bincode::config::standard()).map(|(result, _)| result) -} - -fn encode_with_bincode(data: &DataForBincode) -> Vec { - native_model_encode_body(data).unwrap() -} - -fn decode_with_bincode(data: Vec) -> DataForBincode { - native_model_decode_body(data).unwrap() -} - -fn encode_decode_with_bincode(data: &DataForBincode) -> DataForBincode { - decode_with_bincode(encode_with_bincode(data)) -} - -#[derive(Encode, Decode)] -#[native_model(id = 1, version = 1)] -struct DataForNativeModel { - x: i32, - string: String, -} - -fn encode_with_native_model(data: &DataForNativeModel) -> Vec { - native_model::encode(data).unwrap() -} - -fn decode_with_native_model(data: Vec) -> DataForNativeModel { - let (data, _) = native_model::decode::(data).unwrap(); - data -} - -fn encode_decode_with_native_model(data: &DataForNativeModel) -> DataForNativeModel { - decode_with_native_model(encode_with_native_model(data)) -} - -fn criterion_benchmark(c: &mut Criterion) { - // Bincode - let data = DataForBincode { - x: 1, - // Set a very long string - string: "Hello".repeat(10000), - }; - c.bench_function("encode_with_bincode", |b| { - b.iter(|| encode_with_bincode(black_box(&data))) - }); - let encoded_data = encode_with_bincode(&data); - c.bench_function("decode_with_bincode", |b| { - b.iter(|| decode_with_bincode(black_box(encoded_data.clone()))) - }); - c.bench_function("encode_decode_with_bincode", |b| { - b.iter(|| encode_decode_with_bincode(black_box(&data))) - }); - - // Native model - let data = DataForNativeModel { - x: 1, - string: "Hello".repeat(10000), - }; - c.bench_function("encode_with_native_model", |b| { - b.iter(|| encode_with_native_model(black_box(&data))) - }); - let encoded_data = native_model::encode(&data).unwrap(); - c.bench_function("decode_with_native_model", |b| { - b.iter(|| decode_with_native_model(black_box(encoded_data.clone()))) - }); - c.bench_function("encode_decode_with_native_model", |b| { - b.iter(|| encode_decode_with_native_model(black_box(&data))) - }); -} - -criterion_group!(benches, criterion_benchmark); -criterion_main!(benches); diff --git a/libraries/native_model/benches/prepend_bytes.rs b/libraries/native_model/benches/prepend_bytes.rs deleted file mode 100644 index 7f77e371..00000000 --- a/libraries/native_model/benches/prepend_bytes.rs +++ /dev/null @@ -1,28 +0,0 @@ -/// Found a way to prepend bytes at the beginning of a Vec with a constant overhead. -use bincode::{Decode, Encode}; -use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; - -fn criterion_benchmark(c: &mut Criterion) { - let mut group = c.benchmark_group("encode"); - - // 1 byte, 1KB, 1MB, 10MB, 100MB - for nb_bytes in [1, 1024, 1024 * 1024, 10 * 1024 * 1024, 100 * 1024 * 1024].into_iter() { - group.throughput(criterion::Throughput::Bytes(nb_bytes as u64)); - - let header: Vec = vec![0; 4]; - let mut data: Vec = vec![1; nb_bytes]; - group.bench_function(BenchmarkId::new("prepend_bytes", nb_bytes), |b| { - b.iter(|| { - // Fastest way to prepend bytes to data - let mut header = header.clone(); - header.append(&mut data); - // prepend bytes to data - // let mut header = header.clone(); - // header.extend_from_slice(&data); - }); - }); - } -} - -criterion_group!(benches, criterion_benchmark); -criterion_main!(benches); diff --git a/libraries/native_model/justfile b/libraries/native_model/justfile index d2f0920e..e0c6eb6a 100644 --- a/libraries/native_model/justfile +++ b/libraries/native_model/justfile @@ -51,4 +51,9 @@ test_bincode_2_rc: test_postcard_1_0: @just _tests_crate '--features postcard_1_0' -test_all: test_no_default test_default test_bincode_1_3 test_bincode_2_rc test_postcard_1_0 \ No newline at end of file +test_all: test_no_default test_default test_bincode_1_3 test_bincode_2_rc test_postcard_1_0 + +bench_overhead: + cargo bench --bench overhead + +bench_all: bench_overhead \ No newline at end of file diff --git a/libraries/native_model/tests_crate/tests/example/custom_codec/bincode_serde.rs b/libraries/native_model/tests_crate/tests/example/custom_codec/bincode_serde.rs index 8d239753..b2ad9308 100644 --- a/libraries/native_model/tests_crate/tests/example/custom_codec/bincode_serde.rs +++ b/libraries/native_model/tests_crate/tests/example/custom_codec/bincode_serde.rs @@ -1,22 +1,6 @@ use bincode; use serde::{Deserialize, Serialize}; -// fn native_model_encode_body( -// model: &T, -// ) -> Result, bincode::error::EncodeError> { -// { -// bincode::serde::encode_to_vec(model, bincode::config::standard()) -// } -// } -// -// fn native_model_decode_body Deserialize<'a>>( -// data: Vec, -// ) -> Result { -// { -// Ok(bincode::serde::decode_from_slice(&data, bincode::config::standard())?.0) -// } -// } - pub struct Bincode; impl native_model::Encode for Bincode {