Add tests and benches for epoch processing

This commit is contained in:
Paul Hauner 2019-02-26 16:27:42 +13:00
parent 53663e54b5
commit 906131f882
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
4 changed files with 60 additions and 8 deletions

View File

@ -4,6 +4,15 @@ version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"] authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018" edition = "2018"
[[bench]]
name = "benches"
harness = false
[dev-dependencies]
criterion = "0.2"
test_harness = { path = "../../beacon_node/beacon_chain/test_harness" }
env_logger = "0.6.0"
[dependencies] [dependencies]
hashing = { path = "../utils/hashing" } hashing = { path = "../utils/hashing" }
int_to_bytes = { path = "../utils/int_to_bytes" } int_to_bytes = { path = "../utils/int_to_bytes" }

View File

@ -0,0 +1,28 @@
use criterion::Criterion;
use criterion::{black_box, criterion_group, criterion_main};
// use env_logger::{Builder, Env};
use state_processing::SlotProcessable;
use types::{beacon_state::BeaconStateBuilder, ChainSpec, Hash256};
fn epoch_processing(c: &mut Criterion) {
// Builder::from_env(Env::default().default_filter_or("debug")).init();
let mut builder = BeaconStateBuilder::with_random_validators(8);
builder.spec = ChainSpec::few_validators();
builder.genesis().unwrap();
builder.teleport_to_end_of_epoch(builder.spec.genesis_epoch + 4);
let state = builder.build().unwrap();
c.bench_function("epoch processing", move |b| {
let spec = &builder.spec;
b.iter_with_setup(
|| state.clone(),
|mut state| black_box(state.per_slot_processing(Hash256::zero(), spec).unwrap()),
)
});
}
criterion_group!(benches, epoch_processing,);
criterion_main!(benches);

View File

@ -9,6 +9,8 @@ use types::{
Crosslink, Epoch, Hash256, InclusionError, PendingAttestation, RelativeEpoch, Crosslink, Epoch, Hash256, InclusionError, PendingAttestation, RelativeEpoch,
}; };
mod tests;
macro_rules! safe_add_assign { macro_rules! safe_add_assign {
($a: expr, $b: expr) => { ($a: expr, $b: expr) => {
$a = $a.saturating_add($b); $a = $a.saturating_add($b);
@ -725,11 +727,3 @@ impl From<BeaconStateError> for WinningRootError {
WinningRootError::BeaconStateError(e) WinningRootError::BeaconStateError(e)
} }
} }
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

View File

@ -0,0 +1,21 @@
#![cfg(test)]
use crate::EpochProcessable;
use env_logger::{Builder, Env};
use types::beacon_state::BeaconStateBuilder;
use types::*;
#[test]
fn runs_without_error() {
Builder::from_env(Env::default().default_filter_or("error")).init();
let mut builder = BeaconStateBuilder::with_random_validators(8);
builder.spec = ChainSpec::few_validators();
builder.genesis().unwrap();
builder.teleport_to_end_of_epoch(builder.spec.genesis_epoch + 4);
let mut state = builder.build().unwrap();
let spec = &builder.spec;
state.per_epoch_processing(spec).unwrap();
}