From ed51a7be74a3f3af4b500f5b9dbab1d0915eaedd Mon Sep 17 00:00:00 2001 From: Carl Lin Date: Tue, 6 Oct 2020 20:00:48 -0700 Subject: [PATCH] Add bench --- runtime/benches/accounts.rs | 54 ++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/runtime/benches/accounts.rs b/runtime/benches/accounts.rs index b1d17b7eaf3f84..fa361566d5531e 100644 --- a/runtime/benches/accounts.rs +++ b/runtime/benches/accounts.rs @@ -2,6 +2,7 @@ extern crate test; +use rand::Rng; use solana_runtime::{ accounts::{create_test_accounts, Accounts}, bank::*, @@ -11,7 +12,7 @@ use solana_sdk::{ genesis_config::{create_genesis_config, ClusterType}, pubkey::Pubkey, }; -use std::{path::PathBuf, sync::Arc}; +use std::{collections::HashMap, path::PathBuf, sync::Arc, sync::RwLock, thread::Builder}; use test::Bencher; fn deposit_many(bank: &Bank, pubkeys: &mut Vec, num: usize) { @@ -140,3 +141,54 @@ fn bench_delete_dependencies(bencher: &mut Bencher) { accounts.accounts_db.clean_accounts(None); }); } + +#[bench] +fn bench_concurrent_read_write(bencher: &mut Bencher) { + let num_readers = 5; + let accounts = Arc::new(Accounts::new( + vec![PathBuf::from("concurrent_read_write")], + &ClusterType::Development, + )); + let num_keys = 1000; + let slot = 0; + accounts.add_root(slot); + let pubkeys: Arc> = Arc::new( + (0..num_keys) + .map(|_| { + let pubkey = Pubkey::new_rand(); + let account = Account::new(1, 0, &Account::default().owner); + accounts.store_slow(slot, &pubkey, &account); + pubkey + }) + .collect(), + ); + let readers: Vec<_> = (0..num_readers) + .map(|_| { + let accounts = accounts.clone(); + let pubkeys = pubkeys.clone(); + Builder::new() + .name("readers".to_string()) + .spawn(move || { + let mut rng = rand::thread_rng(); + let i = rng.gen_range(0, num_keys); + loop { + accounts.load_slow(&HashMap::new(), &pubkeys[i]); + } + }) + .unwrap() + }) + .collect(); + + let num_new_keys = 1000; + let new_accounts: Vec<_> = (0..num_new_keys) + .map(|_| Account::new(1, 0, &Account::default().owner)) + .collect(); + bencher.iter(|| { + for i in 0..new_accounts.len() { + // Write to a different slot than the one being read from. Because + // there's a new account pubkey being written to every time, will + // compete for the accounts index lock on every store + accounts.store_slow(slot + 1, &Pubkey::new_rand(), &new_accounts[i]); + } + }) +}