Add profiling tools, examples

This commit is contained in:
Paul Hauner 2019-05-05 15:32:09 +10:00
parent fd5f914c3c
commit acf854f558
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
5 changed files with 81 additions and 19 deletions

View File

@ -36,3 +36,6 @@ members = [
"validator_client",
"account_manager",
]
[profile.release]
debug = true

View File

@ -4,7 +4,12 @@ version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018"
[[bench]]
name = "benches"
harness = false
[dev-dependencies]
criterion = "0.2"
ssz_derive = { path = "../ssz_derive" }
[dependencies]

View File

@ -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<u64> = 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<u64> = 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<u64> = Vec::from_ssz_bytes(&bytes).unwrap();
black_box(vec)
},
)
})
.sample_size(100),
);
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

View File

@ -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<u64> = vec![4242; 8196];
let output: Vec<Vec<u64>> = (0..10_000)
.into_iter()
.map(|_| Vec::from_ssz_bytes(&vec.as_ssz_bytes()).unwrap())
.collect();
println!("{}", output.len());
}

View File

@ -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() {