Skip to content

Commit

Permalink
Bench cu BenchWithdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
ksolana committed Feb 1, 2025
1 parent d8539b3 commit 8d650a1
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion programs/vote/benches/vote_instructions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use {
bincode::serialize,
criterion::{criterion_group, criterion_main, Criterion},
solana_account::{self as account, create_account_for_test, Account, AccountSharedData},
solana_account::{
self as account, create_account_for_test, Account, AccountSharedData, ReadableAccount,
},
solana_clock::{Clock, Slot},
solana_hash::Hash,
solana_instruction::{error::InstructionError, AccountMeta},
Expand Down Expand Up @@ -310,6 +312,58 @@ impl BenchVote {
}
}

struct BenchWithdraw {
instruction_data: Vec<u8>,
transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
instruction_accounts: Vec<AccountMeta>,
}

impl BenchWithdraw {
pub fn new() -> Self {
let (vote_pubkey, vote_account) = create_test_account();
let authorized_withdrawer_pubkey = solana_pubkey::new_rand();
let transaction_accounts = vec![
(vote_pubkey, vote_account.clone()),
(sysvar::clock::id(), create_default_clock_account()),
(sysvar::rent::id(), create_default_rent_account()),
(authorized_withdrawer_pubkey, AccountSharedData::default()),
];
let instruction_accounts = vec![
AccountMeta {
pubkey: vote_pubkey,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: sysvar::clock::id(),
is_signer: false,
is_writable: false,
},
];
let instruction_data = serialize(&VoteInstruction::Authorize(
authorized_withdrawer_pubkey,
VoteAuthorize::Withdrawer,
))
.unwrap();

Self {
instruction_data,
transaction_accounts,
instruction_accounts,
}
}

fn run(&self) {
// should pass, withdraw using authorized_withdrawer to authorized_withdrawer's account
let _accounts = process_instruction(
&self.instruction_data,
self.transaction_accounts.clone(),
self.instruction_accounts.clone(),
Ok(()),
);
}
}

fn bench_initialize_account(c: &mut Criterion) {
let test_setup = BenchInitializeAccount::new();
c.bench_function("vote_instruction_initialize_account", |bencher| {
Expand All @@ -331,10 +385,18 @@ fn bench_vote(c: &mut Criterion) {
});
}

fn bench_withdraw(c: &mut Criterion) {
let test_setup = BenchWithdraw::new();
c.bench_function("vote_instruction_withdraw", |bencher| {
bencher.iter(|| test_setup.run())
});
}

criterion_group!(
benches,
bench_initialize_account,
bench_authorize,
bench_vote,
bench_withdraw,
);
criterion_main!(benches);

0 comments on commit 8d650a1

Please sign in to comment.