From 7f9d316fe19927ba3aa5d370b33e3741c437cc01 Mon Sep 17 00:00:00 2001 From: Tao Zhu Date: Thu, 31 Oct 2024 16:15:20 -0500 Subject: [PATCH] replace bencher with criterion --- Cargo.lock | 1 + programs/stake/Cargo.toml | 2 + programs/stake/benches/stake.rs | 179 ++++++++++++++++++++------------ 3 files changed, 114 insertions(+), 68 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index efa8b74c85b856..d5ca67cd6e266b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8192,6 +8192,7 @@ version = "2.1.0" dependencies = [ "assert_matches", "bincode", + "criterion", "log", "proptest", "solana-compute-budget", diff --git a/programs/stake/Cargo.toml b/programs/stake/Cargo.toml index a647a9081ba60b..8e29f3b3e75919 100644 --- a/programs/stake/Cargo.toml +++ b/programs/stake/Cargo.toml @@ -22,6 +22,7 @@ solana-vote-program = { workspace = true } [dev-dependencies] assert_matches = { workspace = true } +criterion = { workspace = true } proptest = { workspace = true } solana-compute-budget = { workspace = true } solana-logger = { workspace = true } @@ -36,6 +37,7 @@ targets = ["x86_64-unknown-linux-gnu"] [[bench]] name = "stake" +harness = false [lints] workspace = true diff --git a/programs/stake/benches/stake.rs b/programs/stake/benches/stake.rs index ca976ae902449c..a17950013bc754 100644 --- a/programs/stake/benches/stake.rs +++ b/programs/stake/benches/stake.rs @@ -1,8 +1,6 @@ -#![feature(test)] -extern crate test; - use { bincode::serialize, + criterion::{black_box, criterion_group, criterion_main, Criterion}, solana_feature_set::FeatureSet, solana_program_runtime::invoke_context::mock_process_instruction, solana_sdk::{ @@ -31,7 +29,6 @@ use { }, solana_vote_program::vote_state::{self, VoteState, VoteStateVersions}, std::sync::Arc, - test::Bencher, }; struct TestSetup { @@ -205,8 +202,7 @@ impl TestSetup { } } -#[bench] -fn bench_initialize(bencher: &mut Bencher) { +fn bench_initialize(c: &mut Criterion) { let mut test_setup = TestSetup::new(); test_setup.add_account( solana_sdk::sysvar::rent::id(), @@ -218,12 +214,12 @@ fn bench_initialize(bencher: &mut Bencher) { Lockup::default(), )) .unwrap(); - - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("initialize", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_initialize_checked(bencher: &mut Bencher) { +fn bench_initialize_checked(c: &mut Criterion) { let mut test_setup = TestSetup::new(); test_setup.add_account( solana_sdk::sysvar::rent::id(), @@ -236,11 +232,12 @@ fn bench_initialize_checked(bencher: &mut Bencher) { let instruction_data = serialize(&StakeInstruction::InitializeChecked).unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("initialize_checked", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_authorize_staker(bencher: &mut Bencher) { +fn bench_authorize_staker(c: &mut Criterion) { let mut test_setup = TestSetup::new(); test_setup.initialize_stake_account(); test_setup.add_account( @@ -256,11 +253,12 @@ fn bench_authorize_staker(bencher: &mut Bencher) { )) .unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("authorize_staker", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_authorize_withdrawer(bencher: &mut Bencher) { +fn bench_authorize_withdrawer(c: &mut Criterion) { let mut test_setup = TestSetup::new(); test_setup.initialize_stake_account(); test_setup.add_account( @@ -277,11 +275,12 @@ fn bench_authorize_withdrawer(bencher: &mut Bencher) { )) .unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("authorize_withdrawer", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_authorize_staker_with_seed(bencher: &mut Bencher) { +fn bench_authorize_staker_with_seed(c: &mut Criterion) { let seed = "test test"; let authorize_address = Pubkey::new_unique(); @@ -303,11 +302,12 @@ fn bench_authorize_staker_with_seed(bencher: &mut Bencher) { )) .unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("authorize_staker_with_seed", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_authorize_withdrawer_with_seed(bencher: &mut Bencher) { +fn bench_authorize_withdrawer_with_seed(c: &mut Criterion) { let seed = "test test"; let authorize_address = Pubkey::new_unique(); @@ -329,11 +329,12 @@ fn bench_authorize_withdrawer_with_seed(bencher: &mut Bencher) { )) .unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("authorize_withdrawer_with_seed", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_authorize_staker_checked(bencher: &mut Bencher) { +fn bench_authorize_staker_checked(c: &mut Criterion) { let mut test_setup = TestSetup::new(); test_setup.initialize_stake_account(); test_setup.add_account( @@ -348,11 +349,12 @@ fn bench_authorize_staker_checked(bencher: &mut Bencher) { let instruction_data = serialize(&StakeInstruction::AuthorizeChecked(StakeAuthorize::Staker)).unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("authorize_staker_checked", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_authorize_withdrawer_checked(bencher: &mut Bencher) { +fn bench_authorize_withdrawer_checked(c: &mut Criterion) { let mut test_setup = TestSetup::new(); test_setup.initialize_stake_account(); test_setup.add_account( @@ -369,11 +371,12 @@ fn bench_authorize_withdrawer_checked(bencher: &mut Bencher) { )) .unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("authorize_withdrawer_checked", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_authorize_staker_checked_with_seed(bencher: &mut Bencher) { +fn bench_authorize_staker_checked_with_seed(c: &mut Criterion) { let seed = "test test"; let authorize_address = Pubkey::new_unique(); @@ -397,11 +400,12 @@ fn bench_authorize_staker_checked_with_seed(bencher: &mut Bencher) { )) .unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("authorize_staker_checked_with_seed", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_authorize_withdrawer_checked_with_seed(bencher: &mut Bencher) { +fn bench_authorize_withdrawer_checked_with_seed(c: &mut Criterion) { let seed = "test test"; let authorize_address = Pubkey::new_unique(); @@ -425,11 +429,12 @@ fn bench_authorize_withdrawer_checked_with_seed(bencher: &mut Bencher) { )) .unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("authorize_withdrawer_checked_with_seed", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_set_lockup(bencher: &mut Bencher) { +fn bench_set_lockup(c: &mut Criterion) { let mut test_setup = TestSetup::new(); test_setup.initialize_stake_account(); test_setup.add_account( @@ -444,11 +449,12 @@ fn bench_set_lockup(bencher: &mut Bencher) { })) .unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("set_lockup", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_set_lockup_checked(bencher: &mut Bencher) { +fn bench_set_lockup_checked(c: &mut Criterion) { let mut test_setup = TestSetup::new(); test_setup.initialize_stake_account(); test_setup.add_account( @@ -462,11 +468,12 @@ fn bench_set_lockup_checked(bencher: &mut Bencher) { })) .unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("set_lockup_checked", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_withdraw(bencher: &mut Bencher) { +fn bench_withdraw(c: &mut Criterion) { let mut test_setup = TestSetup::new(); test_setup.initialize_stake_account(); let withdraw_authority_address = test_setup.config_withdraw_authority(); @@ -488,11 +495,12 @@ fn bench_withdraw(bencher: &mut Bencher) { let instruction_data = serialize(&StakeInstruction::Withdraw(1)).unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("withdraw", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_delegate_stake(bencher: &mut Bencher) { +fn bench_delegate_stake(c: &mut Criterion) { let mut test_setup = TestSetup::new(); test_setup.initialize_stake_account(); @@ -511,11 +519,12 @@ fn bench_delegate_stake(bencher: &mut Bencher) { test_setup.add_account(Pubkey::new_unique(), AccountSharedData::default()); let instruction_data = serialize(&StakeInstruction::DelegateStake).unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("delegate_stake", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_deactivate(bencher: &mut Bencher) { +fn bench_deactivate(c: &mut Criterion) { let mut test_setup = TestSetup::new(); test_setup.initialize_stake_account(); test_setup.delegate_stake(); @@ -527,11 +536,12 @@ fn bench_deactivate(bencher: &mut Bencher) { let instruction_data = serialize(&StakeInstruction::Deactivate).unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("deactivate", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_split(bencher: &mut Bencher) { +fn bench_split(c: &mut Criterion) { let mut test_setup = TestSetup::new(); test_setup.initialize_stake_account(); @@ -563,11 +573,12 @@ fn bench_split(bencher: &mut Bencher) { let instruction_data = serialize(&StakeInstruction::Split(1)).unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("split", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_merge(bencher: &mut Bencher) { +fn bench_merge(c: &mut Criterion) { let mut test_setup = TestSetup::new(); test_setup.initialize_stake_account(); @@ -594,19 +605,21 @@ fn bench_merge(bencher: &mut Bencher) { let instruction_data = serialize(&StakeInstruction::Merge).unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("merge", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_get_minimum_delegation(bencher: &mut Bencher) { +fn bench_get_minimum_delegation(c: &mut Criterion) { let test_setup = TestSetup::new(); let instruction_data = serialize(&StakeInstruction::GetMinimumDelegation).unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("get_minimum_delegation", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_deactivate_delinquent(bencher: &mut Bencher) { +fn bench_deactivate_delinquent(c: &mut Criterion) { let mut test_setup = TestSetup::new(); // reference vote account has been consistently voting @@ -654,11 +667,12 @@ fn bench_deactivate_delinquent(bencher: &mut Bencher) { let instruction_data = serialize(&StakeInstruction::DeactivateDelinquent).unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("deactivate_delinquent", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_move_stake(bencher: &mut Bencher) { +fn bench_move_stake(c: &mut Criterion) { let mut test_setup = TestSetup::new(); test_setup.initialize_stake_account(); test_setup.delegate_stake(); @@ -682,11 +696,12 @@ fn bench_move_stake(bencher: &mut Bencher) { let instruction_data = serialize(&StakeInstruction::MoveStake(1)).unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("move_stake", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } -#[bench] -fn bench_move_lamports(bencher: &mut Bencher) { +fn bench_move_lamports(c: &mut Criterion) { let mut test_setup = TestSetup::new(); test_setup.initialize_stake_account(); test_setup.delegate_stake(); @@ -710,5 +725,33 @@ fn bench_move_lamports(bencher: &mut Bencher) { let instruction_data = serialize(&StakeInstruction::MoveLamports(1)).unwrap(); - bencher.iter(|| test_setup.run(&instruction_data)); + c.bench_function("move_lamports", |bencher| { + bencher.iter(|| test_setup.run(black_box(&instruction_data))) + }); } +// */ +criterion_group!( + benches, + bench_initialize, + bench_initialize_checked, + bench_authorize_staker, + bench_authorize_withdrawer, + bench_authorize_staker_with_seed, + bench_authorize_withdrawer_with_seed, + bench_authorize_staker_checked, + bench_authorize_withdrawer_checked, + bench_authorize_staker_checked_with_seed, + bench_authorize_withdrawer_checked_with_seed, + bench_set_lockup, + bench_set_lockup_checked, + bench_withdraw, + bench_delegate_stake, + bench_deactivate, + bench_split, + bench_merge, + bench_get_minimum_delegation, + bench_deactivate_delinquent, + bench_move_stake, + bench_move_lamports, +); +criterion_main!(benches);