mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-07 00:42:42 +00:00
Refactor block_processing
- Add the rayon library for parallelization - Move from std::sync::Rwlock to rayon::RwLock - Add `state` field to BeaconChain - Fix major bug in attestation validator where justified slot was incorrectly looked up.
This commit is contained in:
@@ -1,64 +1,80 @@
|
||||
use criterion::Criterion;
|
||||
use criterion::{criterion_group, criterion_main};
|
||||
use criterion::{black_box, criterion_group, criterion_main, Benchmark};
|
||||
use env_logger::{Builder, Env};
|
||||
use test_harness::BeaconChainHarness;
|
||||
use types::ChainSpec;
|
||||
use types::{ChainSpec, Hash256};
|
||||
|
||||
fn mid_epoch_state_transition(c: &mut Criterion) {
|
||||
let validator_count = 2;
|
||||
Builder::from_env(Env::default().default_filter_or("debug")).init();
|
||||
|
||||
let validator_count = 1000;
|
||||
let mut rig = BeaconChainHarness::new(ChainSpec::foundation(), validator_count);
|
||||
|
||||
let two_and_half_epochs = (rig.spec.epoch_length * 2) + (rig.spec.epoch_length / 2);
|
||||
let epoch_depth = (rig.spec.epoch_length * 2) + (rig.spec.epoch_length / 2);
|
||||
|
||||
for _ in 0..two_and_half_epochs {
|
||||
for _ in 0..epoch_depth {
|
||||
rig.advance_chain_with_block();
|
||||
}
|
||||
|
||||
let block = rig.advance_chain_without_block();
|
||||
let state = rig.beacon_chain.canonical_head().beacon_state.clone();
|
||||
let state = rig.beacon_chain.state.read().clone();
|
||||
|
||||
assert!((state.slot + 1) % rig.spec.epoch_length != 0);
|
||||
|
||||
c.bench_function("mid-epoch state transition 10k validators", move |b| {
|
||||
let block = block.clone();
|
||||
let state = state.clone();
|
||||
b.iter(|| {
|
||||
rig.beacon_chain
|
||||
.state_transition(state.clone(), &block.clone())
|
||||
let mut state = state.clone();
|
||||
black_box(state.per_slot_processing(Hash256::zero(), &rig.spec))
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
fn epoch_boundary_state_transition(c: &mut Criterion) {
|
||||
let validator_count = 10_000;
|
||||
// Builder::from_env(Env::default().default_filter_or("debug")).init();
|
||||
|
||||
let validator_count = 10000;
|
||||
let mut rig = BeaconChainHarness::new(ChainSpec::foundation(), validator_count);
|
||||
|
||||
let three_epochs = rig.spec.epoch_length * 3;
|
||||
let epoch_depth = rig.spec.epoch_length * 2;
|
||||
|
||||
for _ in 0..(three_epochs - 1) {
|
||||
for _ in 0..(epoch_depth - 1) {
|
||||
rig.advance_chain_with_block();
|
||||
}
|
||||
|
||||
let state = rig.beacon_chain.canonical_head().beacon_state.clone();
|
||||
assert_eq!(
|
||||
state.slot % rig.spec.epoch_length,
|
||||
rig.spec.epoch_length - 1,
|
||||
);
|
||||
let block = rig.advance_chain_without_block();
|
||||
let state = rig.beacon_chain.state.read().clone();
|
||||
|
||||
c.bench_function("epoch boundary state transition 10k validators", move |b| {
|
||||
let block = block.clone();
|
||||
assert_eq!((state.slot + 1) % rig.spec.epoch_length, 0);
|
||||
|
||||
c.bench(
|
||||
"routines",
|
||||
Benchmark::new("routine_1", move |b| {
|
||||
let state = state.clone();
|
||||
b.iter(|| {
|
||||
let mut state = state.clone();
|
||||
black_box(black_box(
|
||||
state.per_slot_processing(Hash256::zero(), &rig.spec),
|
||||
))
|
||||
})
|
||||
})
|
||||
.sample_size(5),
|
||||
);
|
||||
|
||||
/*
|
||||
c.bench_function("mid-epoch state transition 10k validators", move |b| {
|
||||
let state = state.clone();
|
||||
b.iter(|| {
|
||||
let state = rig
|
||||
.beacon_chain
|
||||
.state_transition(state.clone(), &block.clone())
|
||||
.unwrap();
|
||||
assert_eq!(state.slot % rig.spec.epoch_length, 0);
|
||||
let mut state = state.clone();
|
||||
black_box(black_box(
|
||||
state.per_slot_processing(Hash256::zero(), &rig.spec),
|
||||
))
|
||||
})
|
||||
});
|
||||
*/
|
||||
}
|
||||
|
||||
criterion_group!(
|
||||
benches,
|
||||
mid_epoch_state_transition,
|
||||
// mid_epoch_state_transition,
|
||||
epoch_boundary_state_transition
|
||||
);
|
||||
criterion_main!(benches);
|
||||
|
||||
Reference in New Issue
Block a user