From 574c1ad1957b69a7d5140112cff7ad5ca248c800 Mon Sep 17 00:00:00 2001 From: Brooks Date: Tue, 12 Mar 2024 14:25:47 -0400 Subject: [PATCH] Adds a new bench for accounts delta hash (#210) --- accounts-db/benches/bench_hashing.rs | 50 +++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/accounts-db/benches/bench_hashing.rs b/accounts-db/benches/bench_hashing.rs index 3158f78c7a938f..78df86a97f5168 100644 --- a/accounts-db/benches/bench_hashing.rs +++ b/accounts-db/benches/bench_hashing.rs @@ -1,7 +1,11 @@ use { - criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}, - solana_accounts_db::accounts_db::AccountsDb, - solana_sdk::{account::AccountSharedData, pubkey::Pubkey}, + criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion, Throughput}, + rand::seq::SliceRandom, + solana_accounts_db::{ + accounts_db::AccountsDb, + accounts_hash::{AccountHash, AccountsHasher}, + }, + solana_sdk::{account::AccountSharedData, hash::Hash, pubkey::Pubkey}, }; const KB: usize = 1024; @@ -39,5 +43,43 @@ fn bench_hash_account(c: &mut Criterion) { } } -criterion_group!(benches, bench_hash_account,); +fn bench_accounts_delta_hash(c: &mut Criterion) { + const ACCOUNTS_COUNTS: [usize; 4] = [ + 1, // the smallest count; will bench overhead + 100, // number of accounts written per slot on mnb (with *no* rent rewrites) + 1_000, // number of accounts written slot on mnb (with rent rewrites) + 10_000, // reasonable largest number of accounts written per slot + ]; + + fn create_account_hashes(accounts_count: usize) -> Vec<(Pubkey, AccountHash)> { + let mut account_hashes: Vec<_> = std::iter::repeat_with(|| { + let address = Pubkey::new_unique(); + let hash = AccountHash(Hash::new_unique()); + (address, hash) + }) + .take(accounts_count) + .collect(); + + // since the accounts delta hash needs to sort the accounts first, ensure we're not + // creating a pre-sorted vec. + let mut rng = rand::thread_rng(); + account_hashes.shuffle(&mut rng); + account_hashes + } + + let mut group = c.benchmark_group("accounts_delta_hash"); + for accounts_count in ACCOUNTS_COUNTS { + group.throughput(Throughput::Elements(accounts_count as u64)); + let account_hashes = create_account_hashes(accounts_count); + group.bench_function(BenchmarkId::new("accounts_count", accounts_count), |b| { + b.iter_batched( + || account_hashes.clone(), + AccountsHasher::accumulate_account_hashes, + BatchSize::SmallInput, + ); + }); + } +} + +criterion_group!(benches, bench_hash_account, bench_accounts_delta_hash); criterion_main!(benches);