-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
107e044
commit d84ee4b
Showing
4 changed files
with
127 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,124 @@ | ||
use criterion::{criterion_group, criterion_main, profiler::Profiler, BatchSize, Criterion}; | ||
// hash benchmarks; run with 'cargo bench' | ||
use std::ops::Deref; | ||
|
||
use bencher::{benchmark_group, benchmark_main, Bencher}; | ||
use firewood::merkle::{Merkle, TrieHash, TRIE_HASH_LEN}; | ||
use firewood_shale::{ | ||
cached::PlainMem, disk_address::DiskAddress, CachedStore, Storable, StoredView, | ||
cached::PlainMem, compact::CompactHeader, disk_address::DiskAddress, CachedStore, ObjCache, | ||
Storable, StoredView, | ||
}; | ||
use rand::{distributions::Alphanumeric, Rng, SeedableRng}; | ||
use pprof::ProfilerGuard; | ||
use rand::{distributions::Alphanumeric, rngs::StdRng, Rng, SeedableRng}; | ||
use std::{fs::File, iter::repeat_with, ops::Deref, os::raw::c_int, path::Path}; | ||
|
||
const ZERO_HASH: TrieHash = TrieHash([0u8; TRIE_HASH_LEN]); | ||
|
||
fn bench_dehydrate(b: &mut Bencher) { | ||
// To enable flamegraph output | ||
// cargo bench --bench shale-bench -- --profile-time=N | ||
enum FlamegraphProfiler { | ||
Init(c_int), | ||
Active(ProfilerGuard<'static>), | ||
} | ||
|
||
impl Profiler for FlamegraphProfiler { | ||
fn start_profiling(&mut self, _benchmark_id: &str, _benchmark_dir: &Path) { | ||
if let Self::Init(frequency) = self { | ||
let guard = ProfilerGuard::new(*frequency).unwrap(); | ||
*self = Self::Active(guard); | ||
} | ||
} | ||
|
||
fn stop_profiling(&mut self, _benchmark_id: &str, benchmark_dir: &Path) { | ||
std::fs::create_dir_all(benchmark_dir).unwrap(); | ||
let filename = "firewood-flamegraph.svg"; | ||
let flamegraph_path = benchmark_dir.join(filename); | ||
let flamegraph_file = File::create(flamegraph_path.as_path()) | ||
.unwrap_or_else(|_| panic!("Error creating file `{}`", flamegraph_path.display())); | ||
|
||
if let Self::Active(profiler) = self { | ||
profiler | ||
.report() | ||
.build() | ||
.unwrap() | ||
.flamegraph(flamegraph_file) | ||
.expect("Error writing flamegraph"); | ||
} | ||
} | ||
} | ||
|
||
fn bench_dehydrate(criterion: &mut Criterion) { | ||
let mut to = [1u8; TRIE_HASH_LEN]; | ||
b.iter(|| { | ||
ZERO_HASH.dehydrate(&mut to).unwrap(); | ||
|
||
criterion.bench_function("dehydrate", |b| { | ||
b.iter(|| ZERO_HASH.dehydrate(&mut to).unwrap()); | ||
}); | ||
} | ||
|
||
fn bench_hydrate(b: &mut Bencher) { | ||
fn bench_hydrate(criterion: &mut Criterion) { | ||
let mut store = PlainMem::new(TRIE_HASH_LEN as u64, 0u8); | ||
store.write(0, ZERO_HASH.deref()); | ||
|
||
b.iter(|| { | ||
TrieHash::hydrate(0, &store).unwrap(); | ||
criterion.bench_function("hydrate", |b| { | ||
b.iter(|| TrieHash::hydrate(0, &store).unwrap()); | ||
}); | ||
} | ||
|
||
fn bench_insert(b: &mut Bencher) { | ||
fn bench_insert<const N: usize>(criterion: &mut Criterion) { | ||
const TEST_MEM_SIZE: u64 = 20_000_000; | ||
let merkle_payload_header = DiskAddress::null(); | ||
|
||
let merkle_payload_header_ref = StoredView::ptr_to_obj( | ||
&PlainMem::new(2 * firewood_shale::compact::CompactHeader::MSIZE, 9), | ||
merkle_payload_header, | ||
firewood_shale::compact::CompactHeader::MSIZE, | ||
) | ||
.unwrap(); | ||
|
||
let store = firewood_shale::compact::CompactSpace::new( | ||
PlainMem::new(TEST_MEM_SIZE, 0).into(), | ||
PlainMem::new(TEST_MEM_SIZE, 1).into(), | ||
merkle_payload_header_ref, | ||
firewood_shale::ObjCache::new(1 << 20), | ||
4096, | ||
4096, | ||
) | ||
.unwrap(); | ||
let mut merkle = Merkle::new(Box::new(store)); | ||
let root = merkle.init_root().unwrap(); | ||
|
||
let mut rng = rand::rngs::StdRng::seed_from_u64(1234); | ||
const KEY_LEN: usize = 4; | ||
b.iter(|| { | ||
// generate a random key | ||
let k = (&mut rng) | ||
.sample_iter(&Alphanumeric) | ||
.take(KEY_LEN) | ||
.collect::<Vec<u8>>(); | ||
merkle.insert(k, vec![b'v'], root).unwrap(); | ||
}); | ||
#[cfg(trace)] | ||
{ | ||
merkle.dump(root, &mut io::std::stdout().lock()).unwrap(); | ||
println!("done\n---\n\n"); | ||
} | ||
let mut rng = StdRng::seed_from_u64(1234); | ||
|
||
criterion | ||
.benchmark_group("firewood") | ||
.sample_size(30) | ||
.bench_function("insert", |b| { | ||
b.iter_batched( | ||
|| { | ||
let merkle_payload_header = DiskAddress::from(0); | ||
|
||
let merkle_payload_header_ref = StoredView::ptr_to_obj( | ||
&PlainMem::new(2 * CompactHeader::MSIZE, 9), | ||
merkle_payload_header, | ||
CompactHeader::MSIZE, | ||
) | ||
.unwrap(); | ||
|
||
let store = firewood_shale::compact::CompactSpace::new( | ||
PlainMem::new(TEST_MEM_SIZE, 0).into(), | ||
PlainMem::new(TEST_MEM_SIZE, 1).into(), | ||
merkle_payload_header_ref, | ||
ObjCache::new(1 << 20), | ||
4096, | ||
4096, | ||
) | ||
.unwrap(); | ||
|
||
let merkle = Merkle::new(Box::new(store)); | ||
let root = merkle.init_root().unwrap(); | ||
|
||
// generate a random key | ||
let keys: Vec<Vec<u8>> = repeat_with(|| { | ||
(&mut rng) | ||
.sample_iter(&Alphanumeric) | ||
.take(KEY_LEN) | ||
.collect() | ||
}) | ||
.take(N) | ||
.collect(); | ||
|
||
(merkle, root, keys) | ||
}, | ||
|(mut merkle, root, keys)| { | ||
keys.into_iter() | ||
.for_each(|key| merkle.insert(key, vec![b'v'], root).unwrap()) | ||
}, | ||
BatchSize::SmallInput, | ||
); | ||
}); | ||
} | ||
|
||
criterion_group! { | ||
name = benches; | ||
config = Criterion::default().with_profiler(FlamegraphProfiler::Init(100)); | ||
targets = bench_dehydrate, bench_hydrate, bench_insert::<1> | ||
} | ||
|
||
benchmark_group!(benches, bench_dehydrate, bench_hydrate, bench_insert); | ||
benchmark_main!(benches); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters