e54784bfeb
# Conflicts: # dapps/src/tests/helpers/registrar.rs # ethcore/evm/src/interpreter/shared_cache.rs # ethcore/light/src/client/header_chain.rs # ethcore/light/src/client/mod.rs # ethcore/light/src/net/mod.rs # ethcore/light/src/on_demand/request.rs # ethcore/light/src/on_demand/tests.rs # ethcore/light/src/provider.rs # ethcore/node_filter/src/lib.rs # ethcore/src/block.rs # ethcore/src/blockchain/blockchain.rs # ethcore/src/client/test_client.rs # ethcore/src/engines/authority_round/mod.rs # ethcore/src/engines/basic_authority.rs # ethcore/src/engines/mod.rs # ethcore/src/engines/tendermint/mod.rs # ethcore/src/engines/validator_set/contract.rs # ethcore/src/engines/validator_set/multi.rs # ethcore/src/engines/validator_set/safe_contract.rs # ethcore/src/engines/vote_collector.rs # ethcore/src/miner/external.rs # ethcore/src/miner/miner.rs # ethcore/src/miner/service_transaction_checker.rs # ethcore/src/miner/work_notify.rs # ethcore/src/pod_account.rs # ethcore/src/pod_state.rs # ethcore/src/snapshot/block.rs # ethcore/src/snapshot/consensus/work.rs # ethcore/src/snapshot/mod.rs # ethcore/src/snapshot/service.rs # ethcore/src/spec/spec.rs # ethcore/src/state/backend.rs # ethcore/src/trace/db.rs # ethcore/src/verification/queue/mod.rs # ethcore/src/verification/verification.rs # parity/informant.rs # rpc/src/v1/helpers/dispatch.rs # rpc/src/v1/helpers/light_fetch.rs # rpc/src/v1/helpers/signing_queue.rs # rpc/src/v1/impls/eth.rs # rpc/src/v1/impls/eth_filter.rs # rpc/src/v1/impls/eth_pubsub.rs # rpc/src/v1/impls/light/eth.rs # rpc/src/v1/impls/signing.rs # rpc/src/v1/tests/helpers/miner_service.rs # rpc/src/v1/tests/helpers/snapshot_service.rs # rpc/src/v1/tests/helpers/sync_provider.rs # rpc/src/v1/tests/mocked/eth.rs # stratum/src/lib.rs # sync/src/blocks.rs # sync/src/chain.rs # sync/src/light_sync/mod.rs # sync/src/tests/helpers.rs # sync/src/tests/snapshot.rs # updater/src/updater.rs # util/src/lib.rs # util/triehash/src/lib.rs
319 lines
7.2 KiB
Rust
319 lines
7.2 KiB
Rust
// Copyright 2015-2017 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 <http://www.gnu.org/licenses/>.
|
|
|
|
#![feature(test)]
|
|
|
|
extern crate test;
|
|
extern crate triehash;
|
|
extern crate ethcore_util;
|
|
extern crate ethcore_bigint;
|
|
#[macro_use]
|
|
extern crate log;
|
|
extern crate hash;
|
|
|
|
use test::{Bencher, black_box};
|
|
use ethcore_bigint::hash::*;
|
|
use ethcore_util::bytes::*;
|
|
use ethcore_util::trie::*;
|
|
use ethcore_util::memorydb::*;
|
|
use triehash::*;
|
|
use hash::keccak;
|
|
|
|
fn random_word(alphabet: &[u8], min_count: usize, diff_count: usize, seed: &mut H256) -> Vec<u8> {
|
|
assert!(min_count + diff_count <= 32);
|
|
*seed = keccak(&seed);
|
|
let r = min_count + (seed[31] as usize % (diff_count + 1));
|
|
let mut ret: Vec<u8> = 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<u8> {
|
|
assert!(min_count + diff_count <= 32);
|
|
*seed = keccak(&seed);
|
|
let r = min_count + (seed[31] as usize % (diff_count + 1));
|
|
seed[0..r].to_vec()
|
|
}
|
|
|
|
fn random_value(seed: &mut H256) -> Bytes {
|
|
*seed = keccak(&seed);
|
|
match seed[0] % 2 {
|
|
1 => vec![seed[31];1],
|
|
_ => seed.to_vec(),
|
|
}
|
|
}
|
|
|
|
#[bench]
|
|
fn trie_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();
|
|
let mut hash_count = 0usize;
|
|
b.iter(&mut ||{
|
|
let mut memdb = MemoryDB::new();
|
|
let mut root = H256::new();
|
|
let mut t = TrieDBMut::new(&mut memdb, &mut root);
|
|
for i in d.iter() {
|
|
t.insert(&i.0, &i.1).unwrap();
|
|
}
|
|
hash_count = t.hash_count;
|
|
});
|
|
// println!("hash_count: {}", hash_count);
|
|
}
|
|
#[bench]
|
|
fn trie_iter(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();
|
|
let mut memdb = MemoryDB::new();
|
|
let mut root = H256::new();
|
|
{
|
|
let mut t = TrieDBMut::new(&mut memdb, &mut root);
|
|
for i in d.iter() {
|
|
t.insert(&i.0, &i.1).unwrap();
|
|
}
|
|
}
|
|
|
|
b.iter(&mut ||{
|
|
let t = TrieDB::new(&memdb, &root).unwrap();
|
|
for n in t.iter().unwrap() {
|
|
black_box(n).unwrap();
|
|
}
|
|
});
|
|
}
|
|
|
|
#[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 ||{
|
|
trie_root(d.clone()).clone();
|
|
});
|
|
}
|
|
|
|
#[bench]
|
|
fn trie_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();
|
|
let mut hash_count = 0usize;
|
|
let mut r = H256::new();
|
|
b.iter(&mut ||{
|
|
let mut memdb = MemoryDB::new();
|
|
let mut root = H256::new();
|
|
let mut t = TrieDBMut::new(&mut memdb, &mut root);
|
|
for i in d.iter() {
|
|
t.insert(&i.0, &i.1).unwrap();
|
|
}
|
|
hash_count = t.hash_count;
|
|
r = t.root().clone();
|
|
});
|
|
// println!("result: {}", hash_count);
|
|
}
|
|
|
|
#[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 ||{
|
|
trie_root(d.clone()).clone();
|
|
});
|
|
}
|
|
|
|
#[bench]
|
|
fn trie_insertions_six_high(b: &mut Bencher) {
|
|
let mut d: Vec<(Bytes, Bytes)> = 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 mut memdb = MemoryDB::new();
|
|
let mut root = H256::new();
|
|
let mut t = TrieDBMut::new(&mut memdb, &mut root);
|
|
for i in d.iter() {
|
|
t.insert(&i.0, &i.1).unwrap();
|
|
}
|
|
})
|
|
}
|
|
|
|
#[bench]
|
|
fn triehash_insertions_six_high(b: &mut Bencher) {
|
|
let mut d: Vec<(Bytes, Bytes)> = 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(&||{
|
|
trie_root(d.clone());
|
|
})
|
|
}
|
|
|
|
#[bench]
|
|
fn trie_insertions_six_mid(b: &mut Bencher) {
|
|
let alphabet = b"@QWERTYUIOPASDFGHJKLZXCVBNM[/]^_";
|
|
let mut d: Vec<(Bytes, Bytes)> = 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 mut memdb = MemoryDB::new();
|
|
let mut root = H256::new();
|
|
let mut t = TrieDBMut::new(&mut memdb, &mut root);
|
|
for i in d.iter() {
|
|
t.insert(&i.0, &i.1).unwrap();
|
|
}
|
|
debug!("hash_count={:?}", t.hash_count);
|
|
})
|
|
}
|
|
|
|
#[bench]
|
|
fn triehash_insertions_six_mid(b: &mut Bencher) {
|
|
let alphabet = b"@QWERTYUIOPASDFGHJKLZXCVBNM[/]^_";
|
|
let mut d: Vec<(Bytes, Bytes)> = 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(||{
|
|
trie_root(d.clone());
|
|
})
|
|
}
|
|
|
|
#[bench]
|
|
fn trie_insertions_random_mid(b: &mut Bencher) {
|
|
let alphabet = b"@QWERTYUIOPASDFGHJKLZXCVBNM[/]^_";
|
|
let mut d: Vec<(Bytes, Bytes)> = 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 mut memdb = MemoryDB::new();
|
|
let mut root = H256::new();
|
|
let mut t = TrieDBMut::new(&mut memdb, &mut root);
|
|
for i in d.iter() {
|
|
t.insert(&i.0, &i.1).unwrap();
|
|
}
|
|
})
|
|
}
|
|
|
|
#[bench]
|
|
fn triehash_insertions_random_mid(b: &mut Bencher) {
|
|
let alphabet = b"@QWERTYUIOPASDFGHJKLZXCVBNM[/]^_";
|
|
let mut d: Vec<(Bytes, Bytes)> = 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(||{
|
|
trie_root(d.clone());
|
|
})
|
|
}
|
|
|
|
#[bench]
|
|
fn trie_insertions_six_low(b: &mut Bencher) {
|
|
let alphabet = b"abcdef";
|
|
let mut d: Vec<(Bytes, Bytes)> = 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 mut memdb = MemoryDB::new();
|
|
let mut root = H256::new();
|
|
let mut t = TrieDBMut::new(&mut memdb, &mut root);
|
|
for i in d.iter() {
|
|
t.insert(&i.0, &i.1).unwrap();
|
|
}
|
|
})
|
|
}
|
|
|
|
#[bench]
|
|
fn triehash_insertions_six_low(b: &mut Bencher) {
|
|
let alphabet = b"abcdef";
|
|
let mut d: Vec<(Bytes, Bytes)> = 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(||{
|
|
trie_root(d.clone());
|
|
})
|
|
}
|
|
|
|
#[bench]
|
|
fn keccakx10000(b: &mut Bencher) {
|
|
b.iter(||{
|
|
let mut seed = H256::new();
|
|
for _ in 0..10000 {
|
|
seed = keccak(&seed);
|
|
}
|
|
})
|
|
}
|