Skip to content

Commit

Permalink
Add bench
Browse files Browse the repository at this point in the history
  • Loading branch information
carllin committed Oct 7, 2020
1 parent 22faa2d commit 8479f62
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion runtime/benches/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

extern crate test;

use rand::Rng;
use solana_runtime::{
accounts::{create_test_accounts, Accounts},
bank::*,
Expand All @@ -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, thread::Builder};
use test::Bencher;

fn deposit_many(bank: &Bank, pubkeys: &mut Vec<Pubkey>, num: usize) {
Expand Down Expand Up @@ -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<Vec<_>> = 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]);
}
})
}

0 comments on commit 8479f62

Please sign in to comment.