2018-08-22 07:57:07 -07:00
|
|
|
#![feature(test)]
|
2018-12-08 21:44:20 -08:00
|
|
|
|
2018-08-22 07:57:07 -07:00
|
|
|
extern crate test;
|
2018-07-10 14:19:42 -07:00
|
|
|
|
2019-04-29 13:09:11 -07:00
|
|
|
use log::*;
|
2019-02-18 22:26:22 -08:00
|
|
|
use solana_runtime::bank::*;
|
2019-04-19 06:29:07 -07:00
|
|
|
use solana_runtime::bank_client::BankClient;
|
2019-07-15 12:16:09 -07:00
|
|
|
use solana_runtime::loader_utils::create_invoke_instruction;
|
2019-04-29 13:09:11 -07:00
|
|
|
use solana_sdk::account::KeyedAccount;
|
2019-04-19 06:29:07 -07:00
|
|
|
use solana_sdk::client::AsyncClient;
|
2019-04-29 13:09:11 -07:00
|
|
|
use solana_sdk::client::SyncClient;
|
2019-05-07 11:16:22 -07:00
|
|
|
use solana_sdk::genesis_block::create_genesis_block;
|
2019-04-29 13:09:11 -07:00
|
|
|
use solana_sdk::instruction::InstructionError;
|
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2018-12-03 10:26:28 -08:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
2019-04-19 06:29:07 -07:00
|
|
|
use solana_sdk::transaction::Transaction;
|
2019-04-29 13:09:11 -07:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::thread::sleep;
|
|
|
|
use std::time::Duration;
|
2018-08-22 07:57:07 -07:00
|
|
|
use test::Bencher;
|
2018-07-10 14:19:42 -07:00
|
|
|
|
2019-04-29 13:09:11 -07:00
|
|
|
const BUILTIN_PROGRAM_ID: [u8; 32] = [
|
|
|
|
098, 117, 105, 108, 116, 105, 110, 095, 112, 114, 111, 103, 114, 097, 109, 095, 105, 100, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
];
|
|
|
|
|
2019-07-15 12:16:09 -07:00
|
|
|
const NOOP_PROGRAM_ID: [u8; 32] = [
|
|
|
|
098, 117, 105, 108, 116, 105, 110, 095, 112, 114, 111, 103, 114, 097, 109, 095, 105, 100, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
|
|
|
|
];
|
|
|
|
|
2019-04-29 13:09:11 -07:00
|
|
|
fn process_instruction(
|
|
|
|
_program_id: &Pubkey,
|
|
|
|
_keyed_accounts: &mut [KeyedAccount],
|
|
|
|
_data: &[u8],
|
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_builtin_transactions(
|
|
|
|
bank_client: &BankClient,
|
|
|
|
mint_keypair: &Keypair,
|
|
|
|
) -> Vec<Transaction> {
|
|
|
|
let program_id = Pubkey::new(&BUILTIN_PROGRAM_ID);
|
|
|
|
|
2019-04-19 06:29:07 -07:00
|
|
|
(0..4096)
|
2018-10-04 13:15:54 -07:00
|
|
|
.into_iter()
|
2018-10-12 15:20:53 -07:00
|
|
|
.map(|_| {
|
2019-04-29 13:09:11 -07:00
|
|
|
// Seed the signer account
|
2018-08-09 07:56:04 -07:00
|
|
|
let rando0 = Keypair::new();
|
2019-04-29 13:09:11 -07:00
|
|
|
bank_client
|
|
|
|
.transfer(10_000, &mint_keypair, &rando0.pubkey())
|
|
|
|
.expect(&format!("{}:{}", line!(), file!()));
|
|
|
|
|
|
|
|
let instruction = create_invoke_instruction(rando0.pubkey(), program_id, &1u8);
|
2019-05-13 12:49:37 -07:00
|
|
|
let (blockhash, _fee_calculator) = bank_client.get_recent_blockhash().unwrap();
|
|
|
|
Transaction::new_signed_instructions(&[&rando0], vec![instruction], blockhash)
|
2018-12-07 19:01:28 -08:00
|
|
|
})
|
2019-04-19 06:29:07 -07:00
|
|
|
.collect()
|
|
|
|
}
|
2018-07-10 14:19:42 -07:00
|
|
|
|
2019-04-29 13:09:11 -07:00
|
|
|
pub fn create_native_loader_transactions(
|
|
|
|
bank_client: &BankClient,
|
|
|
|
mint_keypair: &Keypair,
|
|
|
|
) -> Vec<Transaction> {
|
2019-07-15 12:16:09 -07:00
|
|
|
let program_id = Pubkey::new(&NOOP_PROGRAM_ID);
|
2019-04-19 06:29:07 -07:00
|
|
|
|
2019-04-29 13:09:11 -07:00
|
|
|
(0..4096)
|
|
|
|
.into_iter()
|
|
|
|
.map(|_| {
|
|
|
|
// Seed the signer account©41
|
|
|
|
let rando0 = Keypair::new();
|
|
|
|
bank_client
|
|
|
|
.transfer(10_000, &mint_keypair, &rando0.pubkey())
|
|
|
|
.expect(&format!("{}:{}", line!(), file!()));
|
|
|
|
|
|
|
|
let instruction = create_invoke_instruction(rando0.pubkey(), program_id, &1u8);
|
2019-05-13 12:49:37 -07:00
|
|
|
let (blockhash, _fee_calculator) = bank_client.get_recent_blockhash().unwrap();
|
|
|
|
Transaction::new_signed_instructions(&[&rando0], vec![instruction], blockhash)
|
2019-04-29 13:09:11 -07:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sync_bencher(bank: &Arc<Bank>, _bank_client: &BankClient, transactions: &Vec<Transaction>) {
|
2019-04-19 06:29:07 -07:00
|
|
|
let results = bank.process_transactions(&transactions);
|
|
|
|
assert!(results.iter().all(Result::is_ok));
|
2019-04-29 13:09:11 -07:00
|
|
|
}
|
2018-10-17 16:28:26 -07:00
|
|
|
|
2019-04-29 13:09:11 -07:00
|
|
|
fn async_bencher(bank: &Arc<Bank>, bank_client: &BankClient, transactions: &Vec<Transaction>) {
|
|
|
|
for transaction in transactions.clone() {
|
|
|
|
bank_client.async_send_transaction(transaction).unwrap();
|
|
|
|
}
|
|
|
|
for _ in 0..1_000_000_000_u64 {
|
|
|
|
if bank
|
|
|
|
.get_signature_status(&transactions.last().unwrap().signatures.get(0).unwrap())
|
|
|
|
.is_some()
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sleep(Duration::from_nanos(1));
|
2018-10-17 16:28:26 -07:00
|
|
|
}
|
2019-04-29 13:09:11 -07:00
|
|
|
if !bank
|
|
|
|
.get_signature_status(&transactions.last().unwrap().signatures.get(0).unwrap())
|
|
|
|
.unwrap()
|
|
|
|
.is_ok()
|
|
|
|
{
|
|
|
|
error!(
|
|
|
|
"transaction failed: {:?}",
|
|
|
|
bank.get_signature_status(&transactions.last().unwrap().signatures.get(0).unwrap())
|
|
|
|
.unwrap()
|
|
|
|
);
|
|
|
|
assert!(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_bench_transactions(
|
|
|
|
bencher: &mut Bencher,
|
2019-09-19 14:21:09 -07:00
|
|
|
bench_work: &dyn Fn(&Arc<Bank>, &BankClient, &Vec<Transaction>),
|
|
|
|
create_transactions: &dyn Fn(&BankClient, &Keypair) -> Vec<Transaction>,
|
2019-04-29 13:09:11 -07:00
|
|
|
) {
|
|
|
|
solana_logger::setup();
|
|
|
|
let ns_per_s = 1_000_000_000;
|
2019-07-22 13:57:28 -07:00
|
|
|
let (mut genesis_block, mint_keypair) = create_genesis_block(100_000_000);
|
|
|
|
genesis_block.ticks_per_slot = 100;
|
2019-04-29 13:09:11 -07:00
|
|
|
let mut bank = Bank::new(&genesis_block);
|
|
|
|
bank.add_instruction_processor(Pubkey::new(&BUILTIN_PROGRAM_ID), process_instruction);
|
2019-07-15 12:16:09 -07:00
|
|
|
bank.register_native_instruction_processor(
|
|
|
|
"solana_noop_program",
|
|
|
|
&Pubkey::new(&NOOP_PROGRAM_ID),
|
|
|
|
);
|
2019-04-29 13:09:11 -07:00
|
|
|
let bank = Arc::new(bank);
|
|
|
|
let bank_client = BankClient::new_shared(&bank);
|
|
|
|
let transactions = create_transactions(&bank_client, &mint_keypair);
|
|
|
|
|
|
|
|
// Do once to fund accounts, load modules, etc...
|
|
|
|
let results = bank.process_transactions(&transactions);
|
|
|
|
assert!(results.iter().all(Result::is_ok));
|
2018-10-17 16:28:26 -07:00
|
|
|
|
2018-08-22 07:57:07 -07:00
|
|
|
bencher.iter(|| {
|
2019-04-29 13:09:11 -07:00
|
|
|
// Since bencher runs this multiple times, we need to clear the signatures.
|
2018-08-22 07:57:07 -07:00
|
|
|
bank.clear_signatures();
|
2019-04-29 13:09:11 -07:00
|
|
|
bench_work(&bank, &bank_client, &transactions);
|
|
|
|
});
|
|
|
|
|
|
|
|
let summary = bencher.bench(|_bencher| {}).unwrap();
|
|
|
|
info!(" {:?} transactions", transactions.len());
|
|
|
|
info!(" {:?} ns/iter median", summary.median as u64);
|
|
|
|
assert!(0f64 != summary.median);
|
|
|
|
let tps = transactions.len() as u64 * (ns_per_s / summary.median as u64);
|
|
|
|
info!(" {:?} TPS", tps);
|
2018-07-10 15:30:56 -07:00
|
|
|
}
|
2019-04-19 06:29:07 -07:00
|
|
|
|
|
|
|
#[bench]
|
2019-07-30 13:48:46 -07:00
|
|
|
#[ignore]
|
2019-04-29 13:09:11 -07:00
|
|
|
fn bench_bank_sync_process_builtin_transactions(bencher: &mut Bencher) {
|
|
|
|
do_bench_transactions(bencher, &sync_bencher, &create_builtin_transactions);
|
|
|
|
}
|
2019-04-19 06:29:07 -07:00
|
|
|
|
2019-04-29 13:09:11 -07:00
|
|
|
#[bench]
|
2019-07-30 13:48:46 -07:00
|
|
|
#[ignore]
|
2019-04-29 13:09:11 -07:00
|
|
|
fn bench_bank_sync_process_native_loader_transactions(bencher: &mut Bencher) {
|
|
|
|
do_bench_transactions(bencher, &sync_bencher, &create_native_loader_transactions);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2019-07-30 13:48:46 -07:00
|
|
|
#[ignore]
|
2019-04-29 13:09:11 -07:00
|
|
|
fn bench_bank_async_process_builtin_transactions(bencher: &mut Bencher) {
|
|
|
|
do_bench_transactions(bencher, &async_bencher, &create_builtin_transactions);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2019-07-30 13:48:46 -07:00
|
|
|
#[ignore]
|
2019-04-29 13:09:11 -07:00
|
|
|
fn bench_bank_async_process_native_loader_transactions(bencher: &mut Bencher) {
|
|
|
|
do_bench_transactions(bencher, &async_bencher, &create_native_loader_transactions);
|
2019-04-19 06:29:07 -07:00
|
|
|
}
|