forked from rooch-network/rooch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci(bench): refine bcs serialized size bench by passing function as in…
…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
Showing
1 changed file
with
52 additions
and
23 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
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); |