Update criterion (#5886)

* Start updating criterion

* Update consensus benches
This commit is contained in:
Michael Sproul
2024-06-05 01:12:56 +10:00
committed by GitHub
parent 2c72bb8fc6
commit 7a7fc82cbd
4 changed files with 159 additions and 217 deletions

View File

@@ -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::<MainnetEthSpec>(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<MainnetEthSpec> =
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
},
|state| black_box(state.update_tree_hash_cache()),
criterion::BatchSize::SmallInput,
)
},
)
.sample_size(10),
.validators_mut()
.push(Validator::default())
.expect("should push validator");
state
.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);