|
| 1 | +#![feature(test)] |
| 2 | +#![cfg(feature = "sbf_rust")] |
| 3 | + |
| 4 | +use solana_sbf_rust_bench::instructions::Recurse; |
| 5 | + |
| 6 | +extern crate test; |
| 7 | + |
| 8 | +use { |
| 9 | + solana_bpf_loader_program::solana_bpf_loader_program, |
| 10 | + solana_runtime::{ |
| 11 | + bank::Bank, |
| 12 | + bank_client::BankClient, |
| 13 | + genesis_utils::{create_genesis_config, GenesisConfigInfo}, |
| 14 | + loader_utils::load_program, |
| 15 | + }, |
| 16 | + solana_sbf_rust_bench::instructions::{BenchInstruction, ReadAccounts, WriteAccounts}, |
| 17 | + solana_sdk::{ |
| 18 | + account::AccountSharedData, |
| 19 | + bpf_loader, |
| 20 | + client::SyncClient, |
| 21 | + instruction::{AccountMeta, Instruction}, |
| 22 | + message::Message, |
| 23 | + pubkey::Pubkey, |
| 24 | + signature::Signer, |
| 25 | + }, |
| 26 | + std::sync::Arc, |
| 27 | + test::Bencher, |
| 28 | +}; |
| 29 | + |
| 30 | +fn bench_accounts( |
| 31 | + bencher: &mut Bencher, |
| 32 | + num_accounts: usize, |
| 33 | + account_size: usize, |
| 34 | + instruction: BenchInstruction, |
| 35 | +) { |
| 36 | + solana_logger::setup(); |
| 37 | + |
| 38 | + let GenesisConfigInfo { |
| 39 | + genesis_config, |
| 40 | + mint_keypair, |
| 41 | + .. |
| 42 | + } = create_genesis_config(50); |
| 43 | + let mut bank = Bank::new_for_benches(&genesis_config); |
| 44 | + let (name, id, entrypoint) = solana_bpf_loader_program!(); |
| 45 | + bank.add_builtin(&name, &id, entrypoint); |
| 46 | + let bank = Arc::new(bank); |
| 47 | + let bank_client = BankClient::new_shared(&bank); |
| 48 | + |
| 49 | + let program_id = load_program( |
| 50 | + &bank_client, |
| 51 | + &bpf_loader::id(), |
| 52 | + &mint_keypair, |
| 53 | + "solana_sbf_rust_bench", |
| 54 | + ); |
| 55 | + |
| 56 | + let accounts = (0..num_accounts) |
| 57 | + .map(|_| { |
| 58 | + ( |
| 59 | + Pubkey::new_unique(), |
| 60 | + AccountSharedData::new(10, account_size, &program_id), |
| 61 | + ) |
| 62 | + }) |
| 63 | + .collect::<Vec<_>>(); |
| 64 | + |
| 65 | + for (pubkey, account) in &accounts { |
| 66 | + bank.store_account(pubkey, account); |
| 67 | + } |
| 68 | + |
| 69 | + let mint_pubkey = mint_keypair.pubkey(); |
| 70 | + let mut account_metas = accounts |
| 71 | + .iter() |
| 72 | + .map(|(pubkey, _)| AccountMeta::new(*pubkey, false)) |
| 73 | + .collect::<Vec<_>>(); |
| 74 | + account_metas.push(AccountMeta::new(program_id, false)); |
| 75 | + |
| 76 | + let instruction = Instruction { |
| 77 | + program_id, |
| 78 | + accounts: account_metas, |
| 79 | + data: instruction.to_bytes(), |
| 80 | + }; |
| 81 | + let message = Message::new(&[instruction], Some(&mint_pubkey)); |
| 82 | + |
| 83 | + bank_client |
| 84 | + .send_and_confirm_message(&[&mint_keypair], message.clone()) |
| 85 | + .unwrap(); |
| 86 | + |
| 87 | + bencher.iter(|| { |
| 88 | + bank.clear_signatures(); |
| 89 | + bank_client |
| 90 | + .send_and_confirm_message(&[&mint_keypair], message.clone()) |
| 91 | + .unwrap(); |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +macro_rules! bench_program_execute_read_accounts { |
| 96 | + ($($name:ident: $num_accounts:expr, $account_size:expr, $read_size:expr),+) => { |
| 97 | + $( |
| 98 | + #[bench] |
| 99 | + fn $name(bencher: &mut Bencher) { |
| 100 | + bench_accounts(bencher, $num_accounts, $account_size, BenchInstruction::ReadAccounts(ReadAccounts { |
| 101 | + num_accounts: $num_accounts, |
| 102 | + size: $read_size, |
| 103 | + })) |
| 104 | + } |
| 105 | + )+ |
| 106 | + }; |
| 107 | +} |
| 108 | + |
| 109 | +bench_program_execute_read_accounts!( |
| 110 | + bench_program_execute_read_1_1k_account: 1, 1024, 1, |
| 111 | + bench_program_execute_read_1_100k_account: 1, 100 * 1024, 1, |
| 112 | + bench_program_execute_read_1_1mb_account: 1, 1024 * 1024, 1, |
| 113 | + bench_program_execute_read_1_10mb_account: 1, 10 * 1024 * 1024, 1, |
| 114 | + |
| 115 | + bench_program_execute_read_10_1k_account: 10, 1024, 1, |
| 116 | + bench_program_execute_read_10_100k_account: 10, 100 * 1024, 1, |
| 117 | + bench_program_execute_read_10_1mb_account: 10, 1024 * 1024, 1, |
| 118 | + |
| 119 | + bench_program_execute_read_126_1k_account: 126, 1024, 1, |
| 120 | + bench_program_execute_read_126_100k_account: 126, 100 * 1024, 1, |
| 121 | + bench_program_execute_read_126_500k_account: 126, 500 * 1024, 1 |
| 122 | +); |
| 123 | + |
| 124 | +macro_rules! bench_program_execute_write_accounts { |
| 125 | + ($($name:ident: $num_accounts:expr, $account_size:expr, $write_size:expr),+) => { |
| 126 | + $( |
| 127 | + #[bench] |
| 128 | + fn $name(bencher: &mut Bencher) { |
| 129 | + bench_accounts(bencher, $num_accounts, $account_size, BenchInstruction::WriteAccounts(WriteAccounts { |
| 130 | + num_accounts: 1, |
| 131 | + size: $write_size, |
| 132 | + })) |
| 133 | + } |
| 134 | + )+ |
| 135 | + }; |
| 136 | +} |
| 137 | + |
| 138 | +bench_program_execute_write_accounts!( |
| 139 | + bench_program_execute_write_1_1k_account: 1, 1024, 1, |
| 140 | + bench_program_execute_write_1_100k_account: 1, 100 * 1024, 1, |
| 141 | + bench_program_execute_write_1_1mb_account: 1, 1024 * 1024, 1, |
| 142 | + bench_program_execute_write_1_10mb_account: 1, 10 * 1024 * 1024, 1, |
| 143 | + |
| 144 | + bench_program_execute_write_10_1k_account: 10, 1024, 1, |
| 145 | + bench_program_execute_write_10_100k_account: 10, 100 * 1024, 1, |
| 146 | + bench_program_execute_write_10_1mb_account: 10, 1024 * 1024, 1, |
| 147 | + |
| 148 | + bench_program_execute_write_126_1k_account: 126, 1024, 1, |
| 149 | + bench_program_execute_write_126_100k_account: 126, 100 * 1024, 1, |
| 150 | + bench_program_execute_write_126_500k_account: 126, 500 * 1024, 1 |
| 151 | +); |
| 152 | +macro_rules! bench_program_execute_recurse { |
| 153 | + ($($name:ident: $num_accounts:expr, $account_size:expr, $n:expr),+) => { |
| 154 | + $( |
| 155 | + #[bench] |
| 156 | + fn $name(bencher: &mut Bencher) { |
| 157 | + bench_accounts(bencher, $num_accounts, $account_size, BenchInstruction::Recurse(Recurse { |
| 158 | + n: $n |
| 159 | + })) |
| 160 | + } |
| 161 | + )+ |
| 162 | + }; |
| 163 | +} |
| 164 | + |
| 165 | +bench_program_execute_recurse!( |
| 166 | + bench_program_execute_recurse_1_1k_account: 1, 1024, 4, |
| 167 | + bench_program_execute_recurse_1_100k_account: 1, 100 * 1024, 4, |
| 168 | + bench_program_execute_recurse_1_1mb_account: 1, 1024 * 1024, 4, |
| 169 | + bench_program_execute_recurse_1_10mb_account: 1, 10 * 1024 * 1024, 4, |
| 170 | + |
| 171 | + bench_program_execute_recurse_10_1k_account: 10, 1024, 4, |
| 172 | + bench_program_execute_recurse_10_100k_account: 10, 100 * 1024, 4, |
| 173 | + |
| 174 | + bench_program_execute_recurse_50_1k_account: 50, 1024, 1, |
| 175 | + bench_program_execute_recurse_50_100k_account: 50, 100 * 1024, 1 |
| 176 | +); |
0 commit comments