Add BeaconChain benchmarking

This commit is contained in:
Paul Hauner 2019-01-27 15:45:29 +11:00
parent 8e9a139560
commit 2bda7e3d14
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
3 changed files with 80 additions and 7 deletions

View File

@ -4,6 +4,13 @@ version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018"
[[bench]]
name = "state_transition"
harness = false
[dev-dependencies]
criterion = "0.2"
[dependencies]
beacon_chain = { path = "../../beacon_chain" }
block_producer = { path = "../../../eth2/block_producer" }

View File

@ -0,0 +1,64 @@
use criterion::Criterion;
use criterion::{criterion_group, criterion_main};
use test_harness::BeaconChainHarness;
use types::ChainSpec;
fn mid_epoch_state_transition(c: &mut Criterion) {
let validator_count = 2;
let mut rig = BeaconChainHarness::new(ChainSpec::foundation(), validator_count);
let two_and_half_epochs = (rig.spec.epoch_length * 2) + (rig.spec.epoch_length / 2);
for _ in 0..two_and_half_epochs {
rig.advance_chain_with_block();
}
let block = rig.advance_chain_without_block();
let state = rig.beacon_chain.canonical_head().beacon_state.clone();
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())
})
});
}
fn epoch_boundary_state_transition(c: &mut Criterion) {
let validator_count = 10_000;
let mut rig = BeaconChainHarness::new(ChainSpec::foundation(), validator_count);
let three_epochs = rig.spec.epoch_length * 3;
for _ in 0..(three_epochs - 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();
c.bench_function("epoch boundary state transition 10k validators", move |b| {
let block = block.clone();
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);
})
});
}
criterion_group!(
benches,
mid_epoch_state_transition,
epoch_boundary_state_transition
);
criterion_main!(benches);

View File

@ -1,12 +1,10 @@
use super::TestValidator;
pub use beacon_chain::dump::{Error as DumpError, SlotDump};
use beacon_chain::BeaconChain;
use block_producer::BeaconNode;
use db::{
stores::{BeaconBlockStore, BeaconStateStore},
MemoryDB,
};
use serde_json::Result as SerdeResult;
use slot_clock::TestingSlotClock;
use std::fs::File;
use std::io::prelude::*;
@ -14,11 +12,11 @@ use std::sync::Arc;
use types::{BeaconBlock, ChainSpec, Keypair, Validator};
pub struct BeaconChainHarness {
db: Arc<MemoryDB>,
beacon_chain: Arc<BeaconChain<MemoryDB, TestingSlotClock>>,
block_store: Arc<BeaconBlockStore<MemoryDB>>,
state_store: Arc<BeaconStateStore<MemoryDB>>,
validators: Vec<TestValidator>,
pub db: Arc<MemoryDB>,
pub beacon_chain: Arc<BeaconChain<MemoryDB, TestingSlotClock>>,
pub block_store: Arc<BeaconBlockStore<MemoryDB>>,
pub state_store: Arc<BeaconStateStore<MemoryDB>>,
pub validators: Vec<TestValidator>,
pub spec: ChainSpec,
}
@ -76,6 +74,10 @@ impl BeaconChainHarness {
}
}
pub fn advance_chain_without_block(&mut self) -> BeaconBlock {
self.produce_next_slot()
}
pub fn advance_chain_with_block(&mut self) {
let block = self.produce_next_slot();
self.beacon_chain.process_block(block).unwrap();