Skip to content

Commit

Permalink
ci(bench): refine bcs serialized size bench by passing function as in…
Browse files Browse the repository at this point in the history
…put values (rooch-network#1568)

This commit refines the benchmarks for 'bcs serialized size' by making the benchmark function take function values as inputs. This is done by redefining the benchmark function to take a vector of function containers as parameter, which allows different evaluation functions to be passed in as arguments. It provides a more flexible way to perform benchmarks and measure different aspects of the encoding and serializing processes.
  • Loading branch information
popcnt1 authored Apr 13, 2024
1 parent e2c3bbd commit ef42210
Showing 1 changed file with 52 additions and 23 deletions.
75 changes: 52 additions & 23 deletions crates/rooch-benchmarks/benches/bench_utils.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,67 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use criterion::{criterion_group, criterion_main, Criterion};
use std::time::Duration;

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use serde::Serialize;

use rooch_benchmarks::helper::profiled;
use rooch_types::transaction::L1Block;

pub fn l1_block_encode_size_benchmark(c: &mut Criterion) {
let l1_block = L1Block::default();
c.bench_function("l1_block_encode_siz", |b| {
b.iter(|| {
let _ = l1_block.encode().len() as u64;
})
});
pub struct BcsSerializeSizeFunContainer<T: ?Sized + Serialize> {
pub func: fn(&T) -> u64,
pub name: &'static str,
}

pub fn l1_block_serialized_size_benchmark(c: &mut Criterion) {
let l1_block = L1Block::default();
c.bench_function("l1_block_serialized_size", |b| {
b.iter(|| {
let _ = bcs::serialized_size(&l1_block).unwrap() as u64;
})
});
pub fn serialized_size<T>(value: &T) -> u64
where
T: ?Sized + Serialize,
{
bcs::serialized_size(value).unwrap() as u64
}

criterion_group! {
name = l1_block_encode_size_bench;
config = profiled(None);
targets = l1_block_encode_size_benchmark
pub fn encode_size<T>(value: &T) -> u64
where
T: ?Sized + Serialize,
{
bcs::to_bytes(value)
.expect("encode transaction should success")
.len() as u64
}

pub fn bcs_serialized_size_benchmark(c: &mut Criterion) {
let l1_block = L1Block::default();
let mut group = c.benchmark_group("bcs_serialized_size");

let funcs = vec![
BcsSerializeSizeFunContainer {
func: serialized_size,
name: "serialized_size",
},
BcsSerializeSizeFunContainer {
func: encode_size,
name: "encode_size",
},
];
for func in funcs.iter() {
group.bench_with_input(
BenchmarkId::from_parameter(func.name),
&func.func,
|b, &func| {
b.iter(|| {
let _ = func(&l1_block);
})
},
);
}
group.finish();
}

criterion_group! {
name = l1_block_serialized_size_bench;
config = profiled(None);
targets = l1_block_serialized_size_benchmark
name = bcs_serialized_size_bench;
config = profiled(None).warm_up_time(Duration::from_millis(10));
targets = bcs_serialized_size_benchmark
}

criterion_main!(l1_block_encode_size_bench, l1_block_serialized_size_bench);
criterion_main!(bcs_serialized_size_bench);

0 comments on commit ef42210

Please sign in to comment.