Skip to content

Commit

Permalink
Update consensus benches
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul committed Jun 4, 2024
1 parent c37eeda commit 870c0b6
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 77 deletions.
18 changes: 10 additions & 8 deletions consensus/swap_or_not_shuffle/benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> = (0..size).collect();
let list: Vec<usize> = (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);
Expand Down
141 changes: 72 additions & 69 deletions consensus/types/benches/benches.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -53,117 +50,123 @@ 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
.update_tree_hash_cache()
.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);

0 comments on commit 870c0b6

Please sign in to comment.