Add benches, examples for cached hashing.
Ignore flamegraph files
This commit is contained in:
parent
4dd1239b24
commit
a4559e798d
2
.gitignore
vendored
2
.gitignore
vendored
@ -4,3 +4,5 @@ Cargo.lock
|
||||
*.pk
|
||||
*.sk
|
||||
*.raw_keypairs
|
||||
flamegraph.svg
|
||||
perf.data*
|
||||
|
@ -4,7 +4,15 @@ version = "0.1.0"
|
||||
authors = ["Paul Hauner <paul@paulhauner.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[profile.release]
|
||||
debug = true
|
||||
|
||||
[[bench]]
|
||||
name = "benches"
|
||||
harness = false
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = "0.2"
|
||||
tree_hash_derive = { path = "../tree_hash_derive" }
|
||||
|
||||
[dependencies]
|
||||
|
73
eth2/utils/cached_tree_hash/benches/benches.rs
Normal file
73
eth2/utils/cached_tree_hash/benches/benches.rs
Normal file
@ -0,0 +1,73 @@
|
||||
#[macro_use]
|
||||
extern crate criterion;
|
||||
|
||||
use cached_tree_hash::TreeHashCache;
|
||||
use criterion::black_box;
|
||||
use criterion::{Benchmark, Criterion};
|
||||
use ethereum_types::H256 as Hash256;
|
||||
use hashing::hash;
|
||||
use tree_hash::TreeHash;
|
||||
|
||||
fn criterion_benchmark(c: &mut Criterion) {
|
||||
let n = 1024;
|
||||
|
||||
let source_vec: Vec<Hash256> = (0..n).map(|_| Hash256::random()).collect();
|
||||
|
||||
let mut source_modified_vec = source_vec.clone();
|
||||
source_modified_vec[n - 1] = Hash256::random();
|
||||
|
||||
let modified_vec = source_modified_vec.clone();
|
||||
c.bench(
|
||||
&format!("vec_of_{}_hashes", n),
|
||||
Benchmark::new("standard", move |b| {
|
||||
b.iter_with_setup(
|
||||
|| modified_vec.clone(),
|
||||
|modified_vec| black_box(modified_vec.tree_hash_root()),
|
||||
)
|
||||
})
|
||||
.sample_size(100),
|
||||
);
|
||||
|
||||
let modified_vec = source_modified_vec.clone();
|
||||
c.bench(
|
||||
&format!("vec_of_{}_hashes", n),
|
||||
Benchmark::new("build_cache", move |b| {
|
||||
b.iter_with_setup(
|
||||
|| modified_vec.clone(),
|
||||
|vec| black_box(TreeHashCache::new(&vec, 0)),
|
||||
)
|
||||
})
|
||||
.sample_size(100),
|
||||
);
|
||||
|
||||
let vec = source_vec.clone();
|
||||
let modified_vec = source_modified_vec.clone();
|
||||
c.bench(
|
||||
&format!("vec_of_{}_hashes", n),
|
||||
Benchmark::new("cache_update", move |b| {
|
||||
b.iter_with_setup(
|
||||
|| {
|
||||
let cache = TreeHashCache::new(&vec, 0).unwrap();
|
||||
(cache, modified_vec.clone())
|
||||
},
|
||||
|(mut cache, modified_vec)| black_box(cache.update(&modified_vec)),
|
||||
)
|
||||
})
|
||||
.sample_size(100),
|
||||
);
|
||||
|
||||
c.bench(
|
||||
&format!("{}_hashes", n),
|
||||
Benchmark::new("hash_64_bytes", move |b| {
|
||||
b.iter(|| {
|
||||
for _ in 0..n {
|
||||
let _digest = hash(&[42; 64]);
|
||||
}
|
||||
})
|
||||
})
|
||||
.sample_size(100),
|
||||
);
|
||||
}
|
||||
|
||||
criterion_group!(benches, criterion_benchmark);
|
||||
criterion_main!(benches);
|
21
eth2/utils/cached_tree_hash/examples/8k_hashes_cached.rs
Normal file
21
eth2/utils/cached_tree_hash/examples/8k_hashes_cached.rs
Normal file
@ -0,0 +1,21 @@
|
||||
use cached_tree_hash::TreeHashCache;
|
||||
use ethereum_types::H256 as Hash256;
|
||||
|
||||
fn run(vec: &Vec<Hash256>, modified_vec: &Vec<Hash256>) {
|
||||
let mut cache = TreeHashCache::new(vec, 0).unwrap();
|
||||
|
||||
cache.update(modified_vec).unwrap();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let n = 2048;
|
||||
|
||||
let vec: Vec<Hash256> = (0..n).map(|_| Hash256::random()).collect();
|
||||
|
||||
let mut modified_vec = vec.clone();
|
||||
modified_vec[n - 1] = Hash256::random();
|
||||
|
||||
for _ in 0..100 {
|
||||
run(&vec, &modified_vec);
|
||||
}
|
||||
}
|
11
eth2/utils/cached_tree_hash/examples/8k_hashes_standard.rs
Normal file
11
eth2/utils/cached_tree_hash/examples/8k_hashes_standard.rs
Normal file
@ -0,0 +1,11 @@
|
||||
use cached_tree_hash::TreeHashCache;
|
||||
use ethereum_types::H256 as Hash256;
|
||||
use tree_hash::TreeHash;
|
||||
|
||||
fn main() {
|
||||
let n = 2048;
|
||||
|
||||
let vec: Vec<Hash256> = (0..n).map(|_| Hash256::random()).collect();
|
||||
|
||||
vec.tree_hash_root();
|
||||
}
|
Loading…
Reference in New Issue
Block a user