Start building non-worst case benches

This commit is contained in:
Paul Hauner 2019-03-14 18:10:36 +13:00
parent b2fb2afb28
commit fc0cdb8226
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
2 changed files with 152 additions and 94 deletions

View File

@ -17,6 +17,8 @@ use types::*;
pub fn bench_block_processing_n_validators(c: &mut Criterion, validator_count: usize) { pub fn bench_block_processing_n_validators(c: &mut Criterion, validator_count: usize) {
let spec = ChainSpec::foundation(); let spec = ChainSpec::foundation();
let bench_builder = BlockBenchingBuilder::new(validator_count, &spec);
let (mut state, keypairs) = build_state(validator_count, &spec); let (mut state, keypairs) = build_state(validator_count, &spec);
let block = build_block(&mut state, &keypairs, &spec); let block = build_block(&mut state, &keypairs, &spec);
@ -94,8 +96,62 @@ fn build_state(validator_count: usize, spec: &ChainSpec) -> (BeaconState, Vec<Ke
builder.build() builder.build()
} }
fn build_block(state: &mut BeaconState, keypairs: &[Keypair], spec: &ChainSpec) -> BeaconBlock { pub struct BlockBenchingBuilder {
let mut builder = TestingBeaconBlockBuilder::new(spec); pub state_builder: TestingBeaconStateBuilder,
pub block_builder: TestingBeaconBlockBuilder,
pub num_validators: usize,
pub num_proposer_slashings: usize,
pub num_attester_slashings: usize,
pub num_indices_per_slashable_vote: usize,
pub num_attestations: usize,
pub num_deposits: usize,
pub num_exits: usize,
pub num_transfers: usize,
}
impl BlockBenchingBuilder {
pub fn new(num_validators: usize, spec: &ChainSpec) -> Self {
let mut state_builder =
TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(num_validators, &spec);
let mut block_builder = TestingBeaconBlockBuilder::new(spec);
Self {
state_builder,
block_builder,
num_validators: 0,
num_proposer_slashings: 0,
num_attester_slashings: 0,
num_indices_per_slashable_vote: spec.max_indices_per_slashable_vote as usize,
num_attestations: 0,
num_deposits: 0,
num_exits: 0,
num_transfers: 0
}
}
pub fn maximize_block_operations(&mut self, spec: &ChainSpec) {
self.num_proposer_slashings = spec.max_proposer_slashings as usize;
self.num_attester_slashings = spec.max_attester_slashings as usize;
self.num_indices_per_slashable_vote = spec.max_indices_per_slashable_vote as usize;
self.num_attestations = spec.max_attestations as usize;
self.num_deposits = spec.max_deposits as usize;
self.num_exits = spec.max_voluntary_exits as usize;
self.num_transfers = spec.max_transfers as usize;
}
pub fn set_slot(&mut self, slot: Slot, spec: &ChainSpec) {
self.state_builder.teleport_to_slot(slot, &spec);
}
pub fn build_caches(&mut self, spec: &ChainSpec) {
// Builds all caches; benches will not contain shuffling/committee building times.
self.state_builder.build_caches(&spec).unwrap();
}
pub fn build(self, spec: &ChainSpec) -> (BeaconBlock, BeaconState) {
let (mut state, keypairs) = self.state_builder.build();
let builder = &mut self.block_builder;
builder.set_slot(state.slot); builder.set_slot(state.slot);
@ -107,12 +163,12 @@ fn build_block(state: &mut BeaconState, keypairs: &[Keypair], spec: &ChainSpec)
// Used as a stream of validator indices for use in slashings, exits, etc. // Used as a stream of validator indices for use in slashings, exits, etc.
let mut validators_iter = (0..keypairs.len() as u64).into_iter(); let mut validators_iter = (0..keypairs.len() as u64).into_iter();
// Insert the maximum possible number of `ProposerSlashing` objects. // Insert `ProposerSlashing` objects.
debug!( debug!(
"Inserting {} proposer slashings...", "Inserting {} proposer slashings...",
spec.max_proposer_slashings self.num_proposer_slashings
); );
for _ in 0..spec.max_proposer_slashings { for _ in 0..self.num_proposer_slashings {
let validator_index = validators_iter.next().expect("Insufficient validators."); let validator_index = validators_iter.next().expect("Insufficient validators.");
builder.insert_proposer_slashing( builder.insert_proposer_slashing(
@ -123,16 +179,16 @@ fn build_block(state: &mut BeaconState, keypairs: &[Keypair], spec: &ChainSpec)
); );
} }
// Insert the maximum possible number of `AttesterSlashing` objects // Insert `AttesterSlashing` objects
debug!( debug!(
"Inserting {} attester slashings...", "Inserting {} attester slashings...",
spec.max_attester_slashings self.num_attester_slashings
); );
for _ in 0..spec.max_attester_slashings { for _ in 0..self.num_attester_slashings {
let mut attesters: Vec<u64> = vec![]; let mut attesters: Vec<u64> = vec![];
let mut secret_keys: Vec<&SecretKey> = vec![]; let mut secret_keys: Vec<&SecretKey> = vec![];
for _ in 0..spec.max_indices_per_slashable_vote { for _ in 0..self.num_indices_per_slashable_vote {
let validator_index = validators_iter.next().expect("Insufficient validators."); let validator_index = validators_iter.next().expect("Insufficient validators.");
attesters.push(validator_index); attesters.push(validator_index);
@ -142,26 +198,26 @@ fn build_block(state: &mut BeaconState, keypairs: &[Keypair], spec: &ChainSpec)
builder.insert_attester_slashing(&attesters, &secret_keys, &state.fork, spec); builder.insert_attester_slashing(&attesters, &secret_keys, &state.fork, spec);
} }
// Insert the maximum possible number of `Attestation` objects. // Insert `Attestation` objects.
debug!("Inserting {} attestations...", spec.max_attestations); debug!("Inserting {} attestations...", self.num_attestations);
let all_secret_keys: Vec<&SecretKey> = keypairs.iter().map(|keypair| &keypair.sk).collect(); let all_secret_keys: Vec<&SecretKey> = keypairs.iter().map(|keypair| &keypair.sk).collect();
builder builder
.fill_with_attestations(state, &all_secret_keys, spec) .insert_attestations(&state, &all_secret_keys, self.num_attestations as usize, spec)
.unwrap(); .unwrap();
// Insert the maximum possible number of `Deposit` objects. // Insert `Deposit` objects.
debug!("Inserting {} deposits...", spec.max_deposits); debug!("Inserting {} deposits...", self.num_deposits);
for i in 0..spec.max_deposits { for i in 0..self.num_deposits {
builder.insert_deposit(32_000_000_000, state.deposit_index + i, state, spec); builder.insert_deposit(32_000_000_000, state.deposit_index + (i as u64), &state, spec);
} }
// Insert the maximum possible number of `Exit` objects. // Insert the maximum possible number of `Exit` objects.
debug!("Inserting {} exits...", spec.max_voluntary_exits); debug!("Inserting {} exits...", self.num_exits);
for _ in 0..spec.max_voluntary_exits { for _ in 0..self.num_exits {
let validator_index = validators_iter.next().expect("Insufficient validators."); let validator_index = validators_iter.next().expect("Insufficient validators.");
builder.insert_exit( builder.insert_exit(
state, &state,
validator_index, validator_index,
&keypairs[validator_index as usize].sk, &keypairs[validator_index as usize].sk,
spec, spec,
@ -169,8 +225,8 @@ fn build_block(state: &mut BeaconState, keypairs: &[Keypair], spec: &ChainSpec)
} }
// Insert the maximum possible number of `Transfer` objects. // Insert the maximum possible number of `Transfer` objects.
debug!("Inserting {} transfers...", spec.max_transfers); debug!("Inserting {} transfers...", self.num_transfers);
for _ in 0..spec.max_transfers { for _ in 0..self.num_transfers {
let validator_index = validators_iter.next().expect("Insufficient validators."); let validator_index = validators_iter.next().expect("Insufficient validators.");
// Manually set the validator to be withdrawn. // Manually set the validator to be withdrawn.
@ -178,7 +234,7 @@ fn build_block(state: &mut BeaconState, keypairs: &[Keypair], spec: &ChainSpec)
state.previous_epoch(spec); state.previous_epoch(spec);
builder.insert_transfer( builder.insert_transfer(
state, &state,
validator_index, validator_index,
validator_index, validator_index,
1, 1,
@ -192,7 +248,8 @@ fn build_block(state: &mut BeaconState, keypairs: &[Keypair], spec: &ChainSpec)
// Set the eth1 data to be different from the state. // Set the eth1 data to be different from the state.
block.eth1_data.block_hash = Hash256::from_slice(&vec![42; 32]); block.eth1_data.block_hash = Hash256::from_slice(&vec![42; 32]);
block (block, state)
}
} }
/// Run the detailed benchmarking suite on the given `BeaconState`. /// Run the detailed benchmarking suite on the given `BeaconState`.

View File

@ -74,19 +74,20 @@ impl TestingBeaconBlockBuilder {
self.block.body.attester_slashings.push(attester_slashing); self.block.body.attester_slashings.push(attester_slashing);
} }
/// Fills the block with `MAX_ATTESTATIONS` attestations. /// Fills the block with `num_attestations` attestations.
/// ///
/// It will first go and get each committee that is able to include an attestation in this /// It will first go and get each committee that is able to include an attestation in this
/// block. If there are enough committees, it will produce an attestation for each. If there /// block. If there _are_ enough committees, it will produce an attestation for each. If there
/// are _not_ enough committees, it will start splitting the committees in half until it /// _are not_ enough committees, it will start splitting the committees in half until it
/// achieves the target. It will then produce separate attestations for each split committee. /// achieves the target. It will then produce separate attestations for each split committee.
/// ///
/// Note: the signed messages of the split committees will be identical -- it would be possible /// Note: the signed messages of the split committees will be identical -- it would be possible
/// to aggregate these split attestations. /// to aggregate these split attestations.
pub fn fill_with_attestations( pub fn insert_attestations(
&mut self, &mut self,
state: &BeaconState, state: &BeaconState,
secret_keys: &[&SecretKey], secret_keys: &[&SecretKey],
num_attestations: usize,
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<(), BeaconStateError> { ) -> Result<(), BeaconStateError> {
let mut slot = self.block.slot - spec.min_attestation_inclusion_delay; let mut slot = self.block.slot - spec.min_attestation_inclusion_delay;
@ -110,7 +111,7 @@ impl TestingBeaconBlockBuilder {
} }
for (committee, shard) in state.get_crosslink_committees_at_slot(slot, spec)? { for (committee, shard) in state.get_crosslink_committees_at_slot(slot, spec)? {
if attestations_added >= spec.max_attestations { if attestations_added >= num_attestations {
break; break;
} }
@ -125,12 +126,12 @@ impl TestingBeaconBlockBuilder {
// Loop through all the committees, splitting each one in half until we have // Loop through all the committees, splitting each one in half until we have
// `MAX_ATTESTATIONS` committees. // `MAX_ATTESTATIONS` committees.
loop { loop {
if committees.len() >= spec.max_attestations as usize { if committees.len() >= num_attestations as usize {
break; break;
} }
for index in 0..committees.len() { for index in 0..committees.len() {
if committees.len() >= spec.max_attestations as usize { if committees.len() >= num_attestations as usize {
break; break;
} }