diff --git a/consensus/swap_or_not_shuffle/benches/benches.rs b/consensus/swap_or_not_shuffle/benches/benches.rs index 8caa4eb28a7..2909ff1ac69 100644 --- a/consensus/swap_or_not_shuffle/benches/benches.rs +++ b/consensus/swap_or_not_shuffle/benches/benches.rs @@ -23,28 +23,30 @@ fn shuffles(c: &mut Criterion) { }); for size in [8, 16, 512, 16_384] { - c.bench_function_with_input( + c.bench_with_input( BenchmarkId::new("whole list shuffle", format!("{size} elements")), &size, - move |b, n| { + move |b, &n| { let seed = vec![42; 32]; b.iter(|| black_box(shuffle_list(&seed, n))) }, ); } + let mut group = c.benchmark_group("fast"); + group.sample_size(10); for size in [512, 16_384, 4_000_000] { - c.bench_function_with_input( - BenchmarkId::new("fast whole list shuffle", format!("{size} elements")), + group.bench_with_input( + BenchmarkId::new("whole list shuffle", format!("{size} elements")), &size, - move |b, n| { + move |b, &n| { let seed = vec![42; 32]; - let list: Vec = (0..size).collect(); + let list: Vec = (0..n).collect(); b.iter(|| black_box(fast_shuffle(list.clone(), SHUFFLE_ROUND_COUNT, &seed, true))) }, - ) - .sample_size(10); + ); } + group.finish(); } criterion_group!(benches, shuffles); diff --git a/consensus/types/benches/benches.rs b/consensus/types/benches/benches.rs index 5c1036a4c5a..c6dda142b24 100644 --- a/consensus/types/benches/benches.rs +++ b/consensus/types/benches/benches.rs @@ -1,7 +1,4 @@ -#![allow(deprecated)] - -use criterion::Criterion; -use criterion::{black_box, criterion_group, criterion_main, Benchmark}; +use criterion::{black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion}; use milhouse::List; use rayon::prelude::*; use ssz::Encode; @@ -53,75 +50,82 @@ fn all_benches(c: &mut Criterion) { let validator_count = 16_384; let spec = Arc::new(MainnetEthSpec::default_spec()); + let mut g = c.benchmark_group("types"); + g.sample_size(10); + let mut state = get_state::(validator_count); state.build_caches(&spec).expect("should build caches"); let state_bytes = state.as_ssz_bytes(); let inner_state = state.clone(); - c.bench( - &format!("{}_validators", validator_count), - Benchmark::new("encode/beacon_state", move |b| { + g.bench_with_input( + BenchmarkId::new("encode/beacon_state", validator_count), + &inner_state, + |b, state| { b.iter_batched_ref( - || inner_state.clone(), + || state.clone(), |state| black_box(state.as_ssz_bytes()), - criterion::BatchSize::SmallInput, + BatchSize::SmallInput, ) - }) - .sample_size(10), + }, ); - c.bench( - &format!("{}_validators", validator_count), - Benchmark::new("decode/beacon_state", move |b| { + g.bench_with_input( + BenchmarkId::new("decode/beacon_state", validator_count), + &(state_bytes.clone(), spec.clone()), + |b, (bytes, spec)| { b.iter_batched_ref( - || (state_bytes.clone(), spec.clone()), + || (bytes.clone(), spec.clone()), |(bytes, spec)| { let state: BeaconState = BeaconState::from_ssz_bytes(&bytes, &spec).expect("should decode"); black_box(state) }, - criterion::BatchSize::SmallInput, + BatchSize::SmallInput, ) - }) - .sample_size(10), + }, ); let inner_state = state.clone(); - c.bench( - &format!("{}_validators", validator_count), - Benchmark::new("clone/beacon_state", move |b| { + g.bench_with_input( + BenchmarkId::new("clone/beacon_state", validator_count), + &inner_state, + |b, state| { b.iter_batched_ref( - || inner_state.clone(), + || state.clone(), |state| black_box(state.clone()), - criterion::BatchSize::SmallInput, + BatchSize::SmallInput, ) - }) - .sample_size(10), + }, ); let inner_state = state.clone(); - c.bench( - &format!("{}_validators", validator_count), - Benchmark::new( + g.bench_with_input( + BenchmarkId::new( "initialized_cached_tree_hash_without_changes/beacon_state", - move |b| { - b.iter_batched_ref( - || inner_state.clone(), - |state| black_box(state.update_tree_hash_cache()), - criterion::BatchSize::SmallInput, - ) - }, - ) - .sample_size(10), + validator_count, + ), + &inner_state, + |b, state| { + b.iter_batched_ref( + || state.clone(), + |state| black_box(state.update_tree_hash_cache()), + BatchSize::SmallInput, + ) + }, ); let mut inner_state = state.clone(); inner_state.drop_all_caches().unwrap(); - c.bench( - &format!("{}_validators", validator_count), - Benchmark::new("non_initialized_cached_tree_hash/beacon_state", move |b| { + g.bench_with_input( + BenchmarkId::new( + "non_initialized_cached_tree_hash/beacon_state", + validator_count, + ), + &inner_state, + |b, state| { b.iter_batched_ref( - || inner_state.clone(), + || state.clone(), |state| { black_box( state @@ -129,41 +133,40 @@ fn all_benches(c: &mut Criterion) { .expect("should update tree hash"), ) }, - criterion::BatchSize::SmallInput, + BatchSize::SmallInput, ) - }) - .sample_size(10), + }, ); let inner_state = state.clone(); - c.bench( - &format!("{}_validators", validator_count), - Benchmark::new( + g.bench_with_input( + BenchmarkId::new( "initialized_cached_tree_hash_with_new_validators/beacon_state", - move |b| { - b.iter_batched_ref( - || { - let mut state = inner_state.clone(); - for _ in 0..16 { - state - .validators_mut() - .push(Validator::default()) - .expect("should push validatorj"); - state - .balances_mut() - .push(32_000_000_000) - .expect("should push balance"); - } + validator_count, + ), + &inner_state, + |b, state| { + b.iter_batched_ref( + || { + let mut state = state.clone(); + for _ in 0..16 { + state + .validators_mut() + .push(Validator::default()) + .expect("should push validator"); state - }, - |state| black_box(state.update_tree_hash_cache()), - criterion::BatchSize::SmallInput, - ) - }, - ) - .sample_size(10), + .balances_mut() + .push(32_000_000_000) + .expect("should push balance"); + } + state + }, + |state| black_box(state.update_tree_hash_cache()), + BatchSize::SmallInput, + ) + }, ); } -criterion_group!(benches, all_benches,); +criterion_group!(benches, all_benches); criterion_main!(benches);