From acf854f558e97481d840460539e5d573999fc25a Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 5 May 2019 15:32:09 +1000 Subject: [PATCH] Add profiling tools, examples --- Cargo.toml | 3 +++ eth2/utils/ssz/Cargo.toml | 5 ++++ eth2/utils/ssz/benches/benches.rs | 38 +++++++++++++++++++++++++++ eth2/utils/ssz/examples/large_list.rs | 16 +++++++++++ eth2/utils/ssz/src/decode/impls.rs | 38 +++++++++++++-------------- 5 files changed, 81 insertions(+), 19 deletions(-) create mode 100644 eth2/utils/ssz/benches/benches.rs create mode 100644 eth2/utils/ssz/examples/large_list.rs diff --git a/Cargo.toml b/Cargo.toml index c05e22286..20c4cd0a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,3 +36,6 @@ members = [ "validator_client", "account_manager", ] + +[profile.release] +debug = true diff --git a/eth2/utils/ssz/Cargo.toml b/eth2/utils/ssz/Cargo.toml index dda169006..0423b1a8b 100644 --- a/eth2/utils/ssz/Cargo.toml +++ b/eth2/utils/ssz/Cargo.toml @@ -4,7 +4,12 @@ version = "0.1.0" authors = ["Paul Hauner "] edition = "2018" +[[bench]] +name = "benches" +harness = false + [dev-dependencies] +criterion = "0.2" ssz_derive = { path = "../ssz_derive" } [dependencies] diff --git a/eth2/utils/ssz/benches/benches.rs b/eth2/utils/ssz/benches/benches.rs new file mode 100644 index 000000000..45a57adef --- /dev/null +++ b/eth2/utils/ssz/benches/benches.rs @@ -0,0 +1,38 @@ +#[macro_use] +extern crate criterion; + +use criterion::black_box; +use criterion::{Benchmark, Criterion}; +use ssz::{Decodable, Encodable}; + +fn criterion_benchmark(c: &mut Criterion) { + let n = 8196; + + let vec: Vec = vec![4242; 8196]; + c.bench( + &format!("vec_of_{}_u64", n), + Benchmark::new("as_ssz_bytes", move |b| { + b.iter_with_setup(|| vec.clone(), |vec| black_box(vec.as_ssz_bytes())) + }) + .sample_size(100), + ); + + let vec: Vec = vec![4242; 8196]; + let bytes = vec.as_ssz_bytes(); + c.bench( + &format!("vec_of_{}_u64", n), + Benchmark::new("from_ssz_bytes", move |b| { + b.iter_with_setup( + || bytes.clone(), + |bytes| { + let vec: Vec = Vec::from_ssz_bytes(&bytes).unwrap(); + black_box(vec) + }, + ) + }) + .sample_size(100), + ); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches); diff --git a/eth2/utils/ssz/examples/large_list.rs b/eth2/utils/ssz/examples/large_list.rs new file mode 100644 index 000000000..b235771fc --- /dev/null +++ b/eth2/utils/ssz/examples/large_list.rs @@ -0,0 +1,16 @@ +//! Encode and decode a list 10,000 times. +//! +//! Useful for `cargo flamegraph`. + +use ssz::{Decodable, Encodable}; + +fn main() { + let vec: Vec = vec![4242; 8196]; + + let output: Vec> = (0..10_000) + .into_iter() + .map(|_| Vec::from_ssz_bytes(&vec.as_ssz_bytes()).unwrap()) + .collect(); + + println!("{}", output.len()); +} diff --git a/eth2/utils/ssz/src/decode/impls.rs b/eth2/utils/ssz/src/decode/impls.rs index 366d9d6b6..2741a0800 100644 --- a/eth2/utils/ssz/src/decode/impls.rs +++ b/eth2/utils/ssz/src/decode/impls.rs @@ -211,28 +211,28 @@ mod tests { /* * Input is exact length */ - let input = vec![42_u8; 32]; - let (decoded, i) = H256::from_ssz_bytes(&input).unwrap(); - assert_eq!(decoded.as_bytes(), &input[..]); - assert_eq!(i, 32); + let input = vec![42_u8; 32]; + let (decoded, i) = H256::from_ssz_bytes(&input).unwrap(); + assert_eq!(decoded.as_bytes(), &input[..]); + assert_eq!(i, 32); - /* - * Input is too long - */ - let mut input = vec![42_u8; 32]; - input.push(12); - let (decoded, i) = H256::from_ssz_bytes(&input, 0).unwrap(); - assert_eq!(decoded.as_bytes(), &input[0..32]); - assert_eq!(i, 32); + /* + * Input is too long + */ + let mut input = vec![42_u8; 32]; + input.push(12); + let (decoded, i) = H256::from_ssz_bytes(&input, 0).unwrap(); + assert_eq!(decoded.as_bytes(), &input[0..32]); + assert_eq!(i, 32); - /* - * Input is too short - */ - let input = vec![42_u8; 31]; - let res = H256::from_ssz_bytes(&input, 0); - assert_eq!(res, Err(DecodeError::TooShort)); + /* + * Input is too short + */ + let input = vec![42_u8; 31]; + let res = H256::from_ssz_bytes(&input, 0); + assert_eq!(res, Err(DecodeError::TooShort)); } - */ + */ #[test] fn first_length_points_backwards() {