solana/bloom/benches/bloom.rs

146 lines
4.1 KiB
Rust
Raw Normal View History

#![feature(test)]
extern crate test;
use {
bv::BitVec,
fnv::FnvHasher,
rand::Rng,
solana_bloom::bloom::{AtomicBloom, Bloom, BloomHashIndex},
solana_sdk::{
hash::{hash, Hash},
signature::Signature,
},
std::{collections::HashSet, hash::Hasher},
test::Bencher,
2020-01-28 17:03:20 -08:00
};
#[bench]
#[ignore]
fn bench_bits_set(bencher: &mut Bencher) {
2020-12-13 17:26:34 -08:00
let mut bits: BitVec<u8> = BitVec::new_fill(false, 38_340_234_u64);
2019-02-09 09:20:43 -08:00
let mut hasher = FnvHasher::default();
bencher.iter(|| {
let idx = hasher.finish() % bits.len();
bits.set(idx, true);
hasher.write_u64(idx);
});
// subtract the next bencher result from this one to get a number for raw
// bits.set()
}
#[bench]
#[ignore]
fn bench_bits_set_hasher(bencher: &mut Bencher) {
2020-12-13 17:26:34 -08:00
let bits: BitVec<u8> = BitVec::new_fill(false, 38_340_234_u64);
2019-02-09 09:20:43 -08:00
let mut hasher = FnvHasher::default();
bencher.iter(|| {
let idx = hasher.finish() % bits.len();
hasher.write_u64(idx);
});
}
#[bench]
#[ignore]
fn bench_sigs_bloom(bencher: &mut Bencher) {
// 1M TPS * 1s (length of block in sigs) == 1M items in filter
// 1.0E-8 false positive rate
// https://hur.st/bloomfilter/?n=1000000&p=1.0E-8&m=&k=
2019-03-02 10:25:16 -08:00
let blockhash = hash(Hash::default().as_ref());
2019-03-05 13:26:59 -08:00
// info!("blockhash = {:?}", blockhash);
let keys = (0..27).map(|i| blockhash.hash_at_index(i)).collect();
let mut sigs: Bloom<Signature> = Bloom::new(38_340_234, keys);
2019-03-02 10:25:16 -08:00
let mut id = blockhash;
let mut falses = 0;
let mut iterations = 0;
bencher.iter(|| {
id = hash(id.as_ref());
let mut sigbytes = Vec::from(id.as_ref());
id = hash(id.as_ref());
sigbytes.extend(id.as_ref());
let sig = Signature::try_from(sigbytes).unwrap();
if sigs.contains(&sig) {
falses += 1;
}
sigs.add(&sig);
sigs.contains(&sig);
iterations += 1;
});
assert_eq!(falses, 0);
}
#[bench]
#[ignore]
fn bench_sigs_hashmap(bencher: &mut Bencher) {
// same structure as above, new
2019-03-02 10:25:16 -08:00
let blockhash = hash(Hash::default().as_ref());
2019-03-05 13:26:59 -08:00
// info!("blockhash = {:?}", blockhash);
let mut sigs: HashSet<Signature> = HashSet::new();
2019-03-02 10:25:16 -08:00
let mut id = blockhash;
let mut falses = 0;
let mut iterations = 0;
bencher.iter(|| {
id = hash(id.as_ref());
let mut sigbytes = Vec::from(id.as_ref());
id = hash(id.as_ref());
sigbytes.extend(id.as_ref());
let sig = Signature::try_from(sigbytes).unwrap();
if sigs.contains(&sig) {
falses += 1;
}
sigs.insert(sig);
sigs.contains(&sig);
iterations += 1;
});
assert_eq!(falses, 0);
}
#[bench]
fn bench_add_hash(bencher: &mut Bencher) {
let mut rng = rand::thread_rng();
Bump rand to 0.8, rand_chacha to 0.3, getrandom to 0.2 (#32871) * sdk: Add concurrent support for rand 0.7 and 0.8 * Update rand, rand_chacha, and getrandom versions * Run command to replace `gen_range` Run `git grep -l gen_range | xargs sed -i'' -e 's/gen_range(\(\S*\), /gen_range(\1../' * sdk: Fix users of older `gen_range` * Replace `hash::new_rand` with `hash::new_with_thread_rng` Run: ``` git grep -l hash::new_rand | xargs sed -i'' -e 's/hash::new_rand([^)]*/hash::new_with_thread_rng(/' ``` * perf: Use `Keypair::new()` instead of `generate` * Use older rand version in zk-token-sdk * program-runtime: Inline random key generation * bloom: Fix clippy warnings in tests * streamer: Scope rng usage correctly * perf: Fix clippy warning * accounts-db: Map to char to generate a random string * Remove `from_secret_key_bytes`, it's just `keypair_from_seed` * ledger: Generate keypairs by hand * ed25519-tests: Use new rand * runtime: Use new rand in all tests * gossip: Clean up clippy and inline keypair generators * core: Inline keypair generation for tests * Push sbf lockfile change * sdk: Sort dependencies correctly * Remove `hash::new_with_thread_rng`, use `Hash::new_unique()` * Use Keypair::new where chacha isn't used * sdk: Fix build by marking rand 0.7 optional * Hardcode secret key length, add static assertion * Unify `getrandom` crate usage to fix linking errors * bloom: Fix tests that require a random hash * Remove some dependencies, try to unify others * Remove unnecessary uses of rand and rand_core * Update lockfiles * Add back some dependencies to reduce rebuilds * Increase max rebuilds from 14 to 15 * frozen-abi: Remove `getrandom` * Bump rebuilds to 17 * Remove getrandom from zk-token-proof
2023-08-21 10:11:21 -07:00
let hash_values: Vec<_> = std::iter::repeat_with(Hash::new_unique)
.take(1200)
.collect();
let mut fail = 0;
bencher.iter(|| {
let mut bloom = Bloom::random(1287, 0.1, 7424);
for hash_value in &hash_values {
bloom.add(hash_value);
}
Bump rand to 0.8, rand_chacha to 0.3, getrandom to 0.2 (#32871) * sdk: Add concurrent support for rand 0.7 and 0.8 * Update rand, rand_chacha, and getrandom versions * Run command to replace `gen_range` Run `git grep -l gen_range | xargs sed -i'' -e 's/gen_range(\(\S*\), /gen_range(\1../' * sdk: Fix users of older `gen_range` * Replace `hash::new_rand` with `hash::new_with_thread_rng` Run: ``` git grep -l hash::new_rand | xargs sed -i'' -e 's/hash::new_rand([^)]*/hash::new_with_thread_rng(/' ``` * perf: Use `Keypair::new()` instead of `generate` * Use older rand version in zk-token-sdk * program-runtime: Inline random key generation * bloom: Fix clippy warnings in tests * streamer: Scope rng usage correctly * perf: Fix clippy warning * accounts-db: Map to char to generate a random string * Remove `from_secret_key_bytes`, it's just `keypair_from_seed` * ledger: Generate keypairs by hand * ed25519-tests: Use new rand * runtime: Use new rand in all tests * gossip: Clean up clippy and inline keypair generators * core: Inline keypair generation for tests * Push sbf lockfile change * sdk: Sort dependencies correctly * Remove `hash::new_with_thread_rng`, use `Hash::new_unique()` * Use Keypair::new where chacha isn't used * sdk: Fix build by marking rand 0.7 optional * Hardcode secret key length, add static assertion * Unify `getrandom` crate usage to fix linking errors * bloom: Fix tests that require a random hash * Remove some dependencies, try to unify others * Remove unnecessary uses of rand and rand_core * Update lockfiles * Add back some dependencies to reduce rebuilds * Increase max rebuilds from 14 to 15 * frozen-abi: Remove `getrandom` * Bump rebuilds to 17 * Remove getrandom from zk-token-proof
2023-08-21 10:11:21 -07:00
let index = rng.gen_range(0..hash_values.len());
if !bloom.contains(&hash_values[index]) {
fail += 1;
}
});
assert_eq!(fail, 0);
}
#[bench]
fn bench_add_hash_atomic(bencher: &mut Bencher) {
let mut rng = rand::thread_rng();
Bump rand to 0.8, rand_chacha to 0.3, getrandom to 0.2 (#32871) * sdk: Add concurrent support for rand 0.7 and 0.8 * Update rand, rand_chacha, and getrandom versions * Run command to replace `gen_range` Run `git grep -l gen_range | xargs sed -i'' -e 's/gen_range(\(\S*\), /gen_range(\1../' * sdk: Fix users of older `gen_range` * Replace `hash::new_rand` with `hash::new_with_thread_rng` Run: ``` git grep -l hash::new_rand | xargs sed -i'' -e 's/hash::new_rand([^)]*/hash::new_with_thread_rng(/' ``` * perf: Use `Keypair::new()` instead of `generate` * Use older rand version in zk-token-sdk * program-runtime: Inline random key generation * bloom: Fix clippy warnings in tests * streamer: Scope rng usage correctly * perf: Fix clippy warning * accounts-db: Map to char to generate a random string * Remove `from_secret_key_bytes`, it's just `keypair_from_seed` * ledger: Generate keypairs by hand * ed25519-tests: Use new rand * runtime: Use new rand in all tests * gossip: Clean up clippy and inline keypair generators * core: Inline keypair generation for tests * Push sbf lockfile change * sdk: Sort dependencies correctly * Remove `hash::new_with_thread_rng`, use `Hash::new_unique()` * Use Keypair::new where chacha isn't used * sdk: Fix build by marking rand 0.7 optional * Hardcode secret key length, add static assertion * Unify `getrandom` crate usage to fix linking errors * bloom: Fix tests that require a random hash * Remove some dependencies, try to unify others * Remove unnecessary uses of rand and rand_core * Update lockfiles * Add back some dependencies to reduce rebuilds * Increase max rebuilds from 14 to 15 * frozen-abi: Remove `getrandom` * Bump rebuilds to 17 * Remove getrandom from zk-token-proof
2023-08-21 10:11:21 -07:00
let hash_values: Vec<_> = std::iter::repeat_with(Hash::new_unique)
.take(1200)
.collect();
let mut fail = 0;
bencher.iter(|| {
let bloom: AtomicBloom<_> = Bloom::random(1287, 0.1, 7424).into();
// Intentionally not using parallelism here, so that this and above
// benchmark only compare the bit-vector ops.
// For benchmarking the parallel code, change bellow for loop to:
// hash_values.par_iter().for_each(|v| bloom.add(v));
for hash_value in &hash_values {
bloom.add(hash_value);
}
Bump rand to 0.8, rand_chacha to 0.3, getrandom to 0.2 (#32871) * sdk: Add concurrent support for rand 0.7 and 0.8 * Update rand, rand_chacha, and getrandom versions * Run command to replace `gen_range` Run `git grep -l gen_range | xargs sed -i'' -e 's/gen_range(\(\S*\), /gen_range(\1../' * sdk: Fix users of older `gen_range` * Replace `hash::new_rand` with `hash::new_with_thread_rng` Run: ``` git grep -l hash::new_rand | xargs sed -i'' -e 's/hash::new_rand([^)]*/hash::new_with_thread_rng(/' ``` * perf: Use `Keypair::new()` instead of `generate` * Use older rand version in zk-token-sdk * program-runtime: Inline random key generation * bloom: Fix clippy warnings in tests * streamer: Scope rng usage correctly * perf: Fix clippy warning * accounts-db: Map to char to generate a random string * Remove `from_secret_key_bytes`, it's just `keypair_from_seed` * ledger: Generate keypairs by hand * ed25519-tests: Use new rand * runtime: Use new rand in all tests * gossip: Clean up clippy and inline keypair generators * core: Inline keypair generation for tests * Push sbf lockfile change * sdk: Sort dependencies correctly * Remove `hash::new_with_thread_rng`, use `Hash::new_unique()` * Use Keypair::new where chacha isn't used * sdk: Fix build by marking rand 0.7 optional * Hardcode secret key length, add static assertion * Unify `getrandom` crate usage to fix linking errors * bloom: Fix tests that require a random hash * Remove some dependencies, try to unify others * Remove unnecessary uses of rand and rand_core * Update lockfiles * Add back some dependencies to reduce rebuilds * Increase max rebuilds from 14 to 15 * frozen-abi: Remove `getrandom` * Bump rebuilds to 17 * Remove getrandom from zk-token-proof
2023-08-21 10:11:21 -07:00
let index = rng.gen_range(0..hash_values.len());
if !bloom.contains(&hash_values[index]) {
fail += 1;
}
});
assert_eq!(fail, 0);
}