|
| 1 | +#[macro_use] |
| 2 | +extern crate criterion; |
| 3 | + |
| 4 | +use std::sync::atomic::{AtomicUsize, Ordering}; |
| 5 | + |
| 6 | +use rand::{thread_rng, Rng}; |
| 7 | +use sparse_merkle_tree::{ |
| 8 | + blake2b::Blake2bHasher, |
| 9 | + default_store::DefaultStore, |
| 10 | + error::Error, |
| 11 | + traits::{StoreReadOps, StoreWriteOps}, |
| 12 | + BranchKey, BranchNode, SparseMerkleTree, H256, |
| 13 | +}; |
| 14 | + |
| 15 | +#[derive(Debug, Default)] |
| 16 | +struct DefaultStoreWithCounters<V> { |
| 17 | + store: DefaultStore<V>, |
| 18 | + counters: Counters, |
| 19 | +} |
| 20 | + |
| 21 | +#[derive(Debug, Default)] |
| 22 | +struct Counters { |
| 23 | + get_branch_counter: AtomicUsize, |
| 24 | + get_leaf_counter: AtomicUsize, |
| 25 | + insert_branch_counter: AtomicUsize, |
| 26 | + insert_leaf_counter: AtomicUsize, |
| 27 | + remove_branch_counter: AtomicUsize, |
| 28 | + remove_leaf_counter: AtomicUsize, |
| 29 | +} |
| 30 | + |
| 31 | +impl<V: Clone> StoreReadOps<V> for DefaultStoreWithCounters<V> { |
| 32 | + fn get_branch(&self, branch_key: &BranchKey) -> Result<Option<BranchNode>, Error> { |
| 33 | + self.counters |
| 34 | + .get_branch_counter |
| 35 | + .fetch_add(1, Ordering::SeqCst); |
| 36 | + self.store.get_branch(branch_key) |
| 37 | + } |
| 38 | + fn get_leaf(&self, leaf_key: &H256) -> Result<Option<V>, Error> { |
| 39 | + self.counters |
| 40 | + .get_leaf_counter |
| 41 | + .fetch_add(1, Ordering::SeqCst); |
| 42 | + self.store.get_leaf(leaf_key) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl<V> StoreWriteOps<V> for DefaultStoreWithCounters<V> { |
| 47 | + fn insert_branch(&mut self, branch_key: BranchKey, branch: BranchNode) -> Result<(), Error> { |
| 48 | + self.counters |
| 49 | + .insert_branch_counter |
| 50 | + .fetch_add(1, Ordering::SeqCst); |
| 51 | + self.store.insert_branch(branch_key, branch) |
| 52 | + } |
| 53 | + fn insert_leaf(&mut self, leaf_key: H256, leaf: V) -> Result<(), Error> { |
| 54 | + self.counters |
| 55 | + .insert_leaf_counter |
| 56 | + .fetch_add(1, Ordering::SeqCst); |
| 57 | + self.store.insert_leaf(leaf_key, leaf) |
| 58 | + } |
| 59 | + fn remove_branch(&mut self, branch_key: &BranchKey) -> Result<(), Error> { |
| 60 | + self.counters |
| 61 | + .remove_branch_counter |
| 62 | + .fetch_add(1, Ordering::SeqCst); |
| 63 | + self.store.remove_branch(branch_key) |
| 64 | + } |
| 65 | + fn remove_leaf(&mut self, leaf_key: &H256) -> Result<(), Error> { |
| 66 | + self.counters |
| 67 | + .remove_leaf_counter |
| 68 | + .fetch_add(1, Ordering::SeqCst); |
| 69 | + self.store.remove_leaf(leaf_key) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +#[allow(clippy::upper_case_acronyms)] |
| 74 | +type SMT = SparseMerkleTree<Blake2bHasher, H256, DefaultStoreWithCounters<H256>>; |
| 75 | + |
| 76 | +fn random_h256(rng: &mut impl Rng) -> H256 { |
| 77 | + let mut buf = [0u8; 32]; |
| 78 | + rng.fill(&mut buf); |
| 79 | + buf.into() |
| 80 | +} |
| 81 | + |
| 82 | +fn random_smt(update_count: usize, rng: &mut impl Rng) { |
| 83 | + let mut smt = SMT::default(); |
| 84 | + let mut keys = Vec::with_capacity(update_count); |
| 85 | + for _ in 0..update_count { |
| 86 | + let key = random_h256(rng); |
| 87 | + let value = random_h256(rng); |
| 88 | + smt.update(key, value).unwrap(); |
| 89 | + keys.push(key); |
| 90 | + } |
| 91 | + println!( |
| 92 | + "random update {} keys, store counters: {:?}", |
| 93 | + update_count, |
| 94 | + smt.store().counters |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +fn random_smt_update_all(update_count: usize, rng: &mut impl Rng) { |
| 99 | + let mut smt = SMT::default(); |
| 100 | + let mut kvs = Vec::with_capacity(update_count); |
| 101 | + for _ in 0..update_count { |
| 102 | + let key = random_h256(rng); |
| 103 | + let value = random_h256(rng); |
| 104 | + kvs.push((key, value)); |
| 105 | + } |
| 106 | + smt.update_all(kvs).unwrap(); |
| 107 | + println!( |
| 108 | + "random update_all {} keys, store counters: {:?}", |
| 109 | + update_count, |
| 110 | + smt.store().counters |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +fn main() { |
| 115 | + let mut rng = thread_rng(); |
| 116 | + random_smt(100, &mut rng); |
| 117 | + random_smt(10000, &mut rng); |
| 118 | + random_smt_update_all(100, &mut rng); |
| 119 | + random_smt_update_all(10000, &mut rng); |
| 120 | +} |
0 commit comments