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, BenchmarkId, Criterion};
use swap_or_not_shuffle::{compute_shuffled_index, shuffle_list as fast_shuffle};
const SHUFFLE_ROUND_COUNT: u8 = 90;
@@ -25,70 +22,32 @@ fn shuffles(c: &mut Criterion) {
b.iter(|| black_box(shuffle_list(&seed, 8)))
});
c.bench(
"whole list shuffle",
Benchmark::new("8 elements", move |b| {
let seed = vec![42; 32];
b.iter(|| black_box(shuffle_list(&seed, 8)))
}),
);
for size in [8, 16, 512, 16_384] {
c.bench_with_input(
BenchmarkId::new("whole list shuffle", format!("{size} elements")),
&size,
move |b, &n| {
let seed = vec![42; 32];
b.iter(|| black_box(shuffle_list(&seed, n)))
},
);
}
c.bench(
"whole list shuffle",
Benchmark::new("16 elements", move |b| {
let seed = vec![42; 32];
b.iter(|| black_box(shuffle_list(&seed, 16)))
}),
);
c.bench(
"whole list shuffle",
Benchmark::new("512 elements", move |b| {
let seed = vec![42; 32];
b.iter(|| black_box(shuffle_list(&seed, 512)))
})
.sample_size(10),
);
c.bench(
"_fast_ whole list shuffle",
Benchmark::new("512 elements", move |b| {
let seed = vec![42; 32];
let list: Vec<usize> = (0..512).collect();
b.iter(|| black_box(fast_shuffle(list.clone(), SHUFFLE_ROUND_COUNT, &seed, true)))
})
.sample_size(10),
);
c.bench(
"whole list shuffle",
Benchmark::new("16384 elements", move |b| {
let seed = vec![42; 32];
b.iter(|| black_box(shuffle_list(&seed, 16_384)))
})
.sample_size(10),
);
c.bench(
"_fast_ whole list shuffle",
Benchmark::new("16384 elements", move |b| {
let seed = vec![42; 32];
let list: Vec<usize> = (0..16384).collect();
b.iter(|| black_box(fast_shuffle(list.clone(), SHUFFLE_ROUND_COUNT, &seed, true)))
})
.sample_size(10),
);
c.bench(
"_fast_ whole list shuffle",
Benchmark::new("4m elements", move |b| {
let seed = vec![42; 32];
let list: Vec<usize> = (0..4_000_000).collect();
b.iter(|| black_box(fast_shuffle(list.clone(), SHUFFLE_ROUND_COUNT, &seed, true)))
})
.sample_size(10),
);
let mut group = c.benchmark_group("fast");
group.sample_size(10);
for size in [512, 16_384, 4_000_000] {
group.bench_with_input(
BenchmarkId::new("whole list shuffle", format!("{size} elements")),
&size,
move |b, &n| {
let seed = vec![42; 32];
let list: Vec<usize> = (0..n).collect();
b.iter(|| black_box(fast_shuffle(list.clone(), SHUFFLE_ROUND_COUNT, &seed, true)))
},
);
}
group.finish();
}
criterion_group!(benches, shuffles,);
criterion_group!(benches, shuffles);
criterion_main!(benches);

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);