// Copyright 2015-2018 Parity Technologies (UK) Ltd. // This file is part of Parity. // Parity is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // Parity is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with Parity. If not, see . #![feature(test)] extern crate ethereum_types; extern crate keccak_hasher; extern crate test; extern crate tiny_keccak; extern crate trie_standardmap; extern crate triehash; use ethereum_types::H256; use keccak_hasher::KeccakHasher; use test::Bencher; use tiny_keccak::keccak256; use trie_standardmap::{Alphabet, ValueMode, StandardMap}; use triehash::trie_root; fn random_word(alphabet: &[u8], min_count: usize, diff_count: usize, seed: &mut H256) -> Vec { assert!(min_count + diff_count <= 32); *seed = H256(keccak256(&seed)); let r = min_count + (seed[31] as usize % (diff_count + 1)); let mut ret: Vec = Vec::with_capacity(r); for i in 0..r { ret.push(alphabet[seed[i] as usize % alphabet.len()]); } ret } fn random_bytes(min_count: usize, diff_count: usize, seed: &mut H256) -> Vec { assert!(min_count + diff_count <= 32); *seed = H256(keccak256(&seed)); let r = min_count + (seed[31] as usize % (diff_count + 1)); seed[0..r].to_vec() } fn random_value(seed: &mut H256) -> Vec { *seed = H256(keccak256(&seed)); match seed[0] % 2 { 1 => vec![seed[31];1], _ => seed.to_vec(), } } #[bench] fn triehash_insertions_32_mir_1k(b: &mut Bencher) { let st = StandardMap { alphabet: Alphabet::All, min_key: 32, journal_key: 0, value_mode: ValueMode::Mirror, count: 1000, }; let d = st.make(); b.iter(&mut ||{ let _ = trie_root::(d.clone()).clone(); }); } #[bench] fn triehash_insertions_32_ran_1k(b: &mut Bencher) { let st = StandardMap { alphabet: Alphabet::All, min_key: 32, journal_key: 0, value_mode: ValueMode::Random, count: 1000, }; let d = st.make(); b.iter(&mut ||{ let _ = trie_root::(d.clone()).clone(); }); } #[bench] fn triehash_insertions_six_high(b: &mut Bencher) { let mut d: Vec<(Vec, Vec)> = Vec::new(); let mut seed = H256::new(); for _ in 0..1000 { let k = random_bytes(6, 0, &mut seed); let v = random_value(&mut seed); d.push((k, v)) } b.iter(&||{ let _ = trie_root::(d.clone()); }) } #[bench] fn triehash_insertions_six_mid(b: &mut Bencher) { let alphabet = b"@QWERTYUIOPASDFGHJKLZXCVBNM[/]^_"; let mut d: Vec<(Vec, Vec)> = Vec::new(); let mut seed = H256::new(); for _ in 0..1000 { let k = random_word(alphabet, 6, 0, &mut seed); let v = random_value(&mut seed); d.push((k, v)) } b.iter(||{ let _ = trie_root::(d.clone()); }) } #[bench] fn triehash_insertions_random_mid(b: &mut Bencher) { let alphabet = b"@QWERTYUIOPASDFGHJKLZXCVBNM[/]^_"; let mut d: Vec<(Vec, Vec)> = Vec::new(); let mut seed = H256::new(); for _ in 0..1000 { let k = random_word(alphabet, 1, 5, &mut seed); let v = random_value(&mut seed); d.push((k, v)) } b.iter(||{ let _ = trie_root::(d.clone()); }) } #[bench] fn triehash_insertions_six_low(b: &mut Bencher) { let alphabet = b"abcdef"; let mut d: Vec<(Vec, Vec)> = Vec::new(); let mut seed = H256::new(); for _ in 0..1000 { let k = random_word(alphabet, 6, 0, &mut seed); let v = random_value(&mut seed); d.push((k, v)) } b.iter(||{ let _ = trie_root::(d.clone()); }) }