diff --git a/Cargo.lock b/Cargo.lock index cbe3fb20815121..c36247340867af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6514,6 +6514,7 @@ dependencies = [ "clap 2.33.3", "console", "const_format", + "criterion", "criterion-stats", "crossbeam-channel", "ctrlc", @@ -6527,12 +6528,14 @@ dependencies = [ "serde", "serde_derive", "serde_json", + "solana-account", "solana-account-decoder", "solana-bpf-loader-program", "solana-clap-utils", "solana-cli-config", "solana-cli-output", "solana-client", + "solana-clock", "solana-commitment-config", "solana-compute-budget", "solana-config-program", @@ -6541,6 +6544,7 @@ dependencies = [ "solana-faucet", "solana-feature-gate-client", "solana-feature-set", + "solana-instruction", "solana-loader-v4-program", "solana-logger", "solana-program-runtime", @@ -6548,12 +6552,14 @@ dependencies = [ "solana-pubsub-client", "solana-quic-client", "solana-remote-wallet", + "solana-rent", "solana-rpc", "solana-rpc-client", "solana-rpc-client-api", "solana-rpc-client-nonce-utils", "solana-sbpf", "solana-sdk", + "solana-sdk-ids", "solana-streamer", "solana-test-validator", "solana-tps-client", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 25582e542a6bd7..e1533a36b66906 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -66,13 +66,23 @@ tiny-bip39 = { workspace = true } [dev-dependencies] assert_matches = { workspace = true } +criterion = { workspace = true } +solana-account = { workspace = true } +solana-clock = { workspace = true } solana-faucet = { workspace = true } +solana-instruction = { workspace = true } +solana-rent = { workspace = true } solana-rpc = { workspace = true } +solana-sdk-ids = { workspace = true } solana-streamer = { workspace = true } solana-test-validator = { workspace = true } tempfile = { workspace = true } test-case = { workspace = true } +[[bench]] +name = "vote" +harness = false + [[bin]] name = "solana" path = "src/main.rs" diff --git a/cli/benches/vote.rs b/cli/benches/vote.rs new file mode 100644 index 00000000000000..3c2a22904a47c9 --- /dev/null +++ b/cli/benches/vote.rs @@ -0,0 +1,110 @@ +use { + bincode::serialize, + criterion::{criterion_group, criterion_main, Criterion}, + solana_account::{self as account, AccountSharedData}, + solana_clock::Clock, + solana_instruction::{error::InstructionError, AccountMeta}, + solana_program_runtime::invoke_context::mock_process_instruction, + solana_pubkey::Pubkey, + solana_rent::Rent, + solana_sdk::sysvar, + solana_sdk_ids::vote::id, + solana_vote_program::{ + vote_instruction::VoteInstruction, + vote_processor::Entrypoint, + vote_state::{VoteInit, VoteState}, + }, +}; + +fn create_default_rent_account() -> AccountSharedData { + account::create_account_shared_data_for_test(&Rent::free()) +} + +fn create_default_clock_account() -> AccountSharedData { + account::create_account_shared_data_for_test(&Clock::default()) +} + +struct TestSetup {} + +impl TestSetup { + fn new() -> TestSetup { + Self {} + } + + fn process_instruction( + instruction_data: &[u8], + transaction_accounts: Vec<(Pubkey, AccountSharedData)>, + instruction_accounts: Vec, + expected_result: Result<(), InstructionError>, + ) -> Vec { + mock_process_instruction( + &id(), + Vec::new(), + instruction_data, + transaction_accounts, + instruction_accounts, + expected_result, + Entrypoint::vm, + |_invoke_context| {}, + |_invoke_context| {}, + ) + } + pub fn run(&self) { + let vote_pubkey = solana_pubkey::new_rand(); + let vote_account = AccountSharedData::new(100, VoteState::size_of(), &id()); + let node_pubkey = solana_pubkey::new_rand(); + let node_account = AccountSharedData::default(); + let instruction_data = serialize(&VoteInstruction::InitializeAccount(VoteInit { + node_pubkey, + authorized_voter: vote_pubkey, + authorized_withdrawer: vote_pubkey, + commission: 0, + })) + .unwrap(); + let instruction_accounts = vec![ + AccountMeta { + pubkey: vote_pubkey, + is_signer: false, + is_writable: true, + }, + AccountMeta { + pubkey: sysvar::rent::id(), + is_signer: false, + is_writable: false, + }, + AccountMeta { + pubkey: sysvar::clock::id(), + is_signer: false, + is_writable: false, + }, + AccountMeta { + pubkey: node_pubkey, + is_signer: true, + is_writable: false, + }, + ]; + + // init should pass + let _accounts = Self::process_instruction( + &instruction_data, + vec![ + (vote_pubkey, vote_account.clone()), + (sysvar::rent::id(), create_default_rent_account()), + (sysvar::clock::id(), create_default_clock_account()), + (node_pubkey, node_account.clone()), + ], + instruction_accounts.clone(), + Ok(()), + ); + } +} + +fn bench_create_vote_account(c: &mut Criterion) { + let test_setup = TestSetup::new(); + c.bench_function("create_vote_account", |bencher| { + bencher.iter(|| test_setup.run()) + }); +} + +criterion_group!(benches, bench_create_vote_account,); +criterion_main!(benches);