From 762799a11574d751e59593d352862517c774e6e7 Mon Sep 17 00:00:00 2001 From: Tao Zhu Date: Wed, 7 Aug 2024 18:41:22 -0500 Subject: [PATCH 1/8] add benches for process_compute_budget_instructions --- Cargo.lock | 1 + runtime-transaction/Cargo.toml | 3 +- .../process_compute_budget_instructions.rs | 141 ++++++++++++++++++ 3 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 runtime-transaction/benches/process_compute_budget_instructions.rs diff --git a/Cargo.lock b/Cargo.lock index ab43623d0cb84a..042b140a76edcd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7344,6 +7344,7 @@ dependencies = [ "log", "rand 0.8.5", "rustc_version 0.4.0", + "solana-builtins-default-costs", "solana-compute-budget", "solana-program", "solana-sdk", diff --git a/runtime-transaction/Cargo.toml b/runtime-transaction/Cargo.toml index 22635d6c121159..200fbd6614220f 100644 --- a/runtime-transaction/Cargo.toml +++ b/runtime-transaction/Cargo.toml @@ -22,7 +22,8 @@ name = "solana_runtime_transaction" [dev-dependencies] bincode = { workspace = true } rand = { workspace = true } -solana-program ={ workspace = true } +solana-builtins-default-costs = { workspace = true } +solana-program = { workspace = true } [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] diff --git a/runtime-transaction/benches/process_compute_budget_instructions.rs b/runtime-transaction/benches/process_compute_budget_instructions.rs new file mode 100644 index 00000000000000..356ab487f9e273 --- /dev/null +++ b/runtime-transaction/benches/process_compute_budget_instructions.rs @@ -0,0 +1,141 @@ +#![feature(test)] +extern crate test; +use { + solana_runtime_transaction::instructions_processor::process_compute_budget_instructions, + solana_sdk::{ + compute_budget::ComputeBudgetInstruction, + instruction::Instruction, + message::Message, + pubkey::Pubkey, + signature::Keypair, + signer::Signer, + system_instruction::{self}, + transaction::{SanitizedTransaction, Transaction}, + }, + test::Bencher, +}; + +const NUM_TRANSACTIONS_PER_ITER: usize = 1024; + +fn build_sanitized_transaction( + payer_keypair: &Keypair, + instructions: &[Instruction], +) -> SanitizedTransaction { + SanitizedTransaction::from_transaction_for_tests(Transaction::new_unsigned(Message::new( + instructions, + Some(&payer_keypair.pubkey()), + ))) +} + +#[bench] +fn bench_process_compute_budget_instructions_empty(bencher: &mut Bencher) { + let tx = build_sanitized_transaction(&Keypair::new(), &[]); + bencher.iter(|| { + (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { + assert!( + process_compute_budget_instructions(tx.message().program_instructions_iter()) + .is_ok() + ) + }) + }); +} + +#[bench] +fn bench_process_compute_budget_instructions_non_builtins(bencher: &mut Bencher) { + let ixs: Vec<_> = (0..4) + .map(|_| { + Instruction::new_with_bincode( + Pubkey::create_program_address(&[b"", &[1]], &solana_sdk::bpf_loader::id()) + .unwrap(), + &0_u8, + vec![], + ) + }) + .collect(); + assert_eq!(4, ixs.len()); + let tx = build_sanitized_transaction(&Keypair::new(), &ixs); + bencher.iter(|| { + (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { + assert!( + process_compute_budget_instructions(tx.message().program_instructions_iter()) + .is_ok() + ) + }) + }); +} + +#[bench] +fn bench_process_compute_budget_instructions_compute_budgets(bencher: &mut Bencher) { + let ixs = vec![ + ComputeBudgetInstruction::request_heap_frame(40 * 1024), + ComputeBudgetInstruction::set_compute_unit_limit(u32::MAX), + ComputeBudgetInstruction::set_compute_unit_price(u64::MAX), + ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(u32::MAX), + ]; + assert_eq!(4, ixs.len()); + let tx = build_sanitized_transaction(&Keypair::new(), &ixs); + bencher.iter(|| { + (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { + assert!( + process_compute_budget_instructions(tx.message().program_instructions_iter()) + .is_ok() + ) + }) + }); +} + +#[bench] +fn bench_process_compute_budget_instructions_builtins(bencher: &mut Bencher) { + let ixs = vec![ + Instruction::new_with_bincode(solana_sdk::bpf_loader::id(), &0_u8, vec![]), + Instruction::new_with_bincode(solana_sdk::secp256k1_program::id(), &0_u8, vec![]), + Instruction::new_with_bincode( + solana_sdk::address_lookup_table::program::id(), + &0_u8, + vec![], + ), + Instruction::new_with_bincode(solana_sdk::loader_v4::id(), &0_u8, vec![]), + ]; + assert_eq!(4, ixs.len()); + let tx = build_sanitized_transaction(&Keypair::new(), &ixs); + bencher.iter(|| { + (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { + assert!( + process_compute_budget_instructions(tx.message().program_instructions_iter()) + .is_ok() + ) + }) + }); +} +#[bench] +fn bench_process_compute_budget_instructions_mixed(bencher: &mut Bencher) { + let payer_keypair = Keypair::new(); + let mut ixs: Vec<_> = (0..128) + .map(|_| { + Instruction::new_with_bincode( + Pubkey::create_program_address(&[b"", &[1]], &solana_sdk::bpf_loader::id()) + .unwrap(), + &0_u8, + vec![], + ) + }) + .collect(); + ixs.extend(vec![ + ComputeBudgetInstruction::request_heap_frame(40 * 1024), + ComputeBudgetInstruction::set_compute_unit_limit(u32::MAX), + ComputeBudgetInstruction::set_compute_unit_price(u64::MAX), + ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(u32::MAX), + system_instruction::transfer(&payer_keypair.pubkey(), &Pubkey::new_unique(), 1), + ]); + assert_eq!(133, ixs.len()); + let tx = build_sanitized_transaction(&payer_keypair, &ixs); + + bencher.iter(|| { + (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { + assert!( + process_compute_budget_instructions(tx.message().program_instructions_iter()) + .is_ok() + ) + }) + }); +} From f61363706d8ff66ce44a15770cb40cabeabc1d62 Mon Sep 17 00:00:00 2001 From: Tao Zhu Date: Thu, 8 Aug 2024 18:15:40 -0500 Subject: [PATCH 2/8] use dummy program id that will not be filtered as maybe_builtin --- .../process_compute_budget_instructions.rs | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/runtime-transaction/benches/process_compute_budget_instructions.rs b/runtime-transaction/benches/process_compute_budget_instructions.rs index 356ab487f9e273..ca146bfe6b149d 100644 --- a/runtime-transaction/benches/process_compute_budget_instructions.rs +++ b/runtime-transaction/benches/process_compute_budget_instructions.rs @@ -16,6 +16,7 @@ use { }; const NUM_TRANSACTIONS_PER_ITER: usize = 1024; +const DUMMY_PROGRAM_ID: &str = "dummmy1111111111111111111111111111111111111"; fn build_sanitized_transaction( payer_keypair: &Keypair, @@ -43,14 +44,7 @@ fn bench_process_compute_budget_instructions_empty(bencher: &mut Bencher) { #[bench] fn bench_process_compute_budget_instructions_non_builtins(bencher: &mut Bencher) { let ixs: Vec<_> = (0..4) - .map(|_| { - Instruction::new_with_bincode( - Pubkey::create_program_address(&[b"", &[1]], &solana_sdk::bpf_loader::id()) - .unwrap(), - &0_u8, - vec![], - ) - }) + .map(|_| Instruction::new_with_bincode(DUMMY_PROGRAM_ID.parse().unwrap(), &0_u8, vec![])) .collect(); assert_eq!(4, ixs.len()); let tx = build_sanitized_transaction(&Keypair::new(), &ixs); @@ -111,14 +105,7 @@ fn bench_process_compute_budget_instructions_builtins(bencher: &mut Bencher) { fn bench_process_compute_budget_instructions_mixed(bencher: &mut Bencher) { let payer_keypair = Keypair::new(); let mut ixs: Vec<_> = (0..128) - .map(|_| { - Instruction::new_with_bincode( - Pubkey::create_program_address(&[b"", &[1]], &solana_sdk::bpf_loader::id()) - .unwrap(), - &0_u8, - vec![], - ) - }) + .map(|_| Instruction::new_with_bincode(DUMMY_PROGRAM_ID.parse().unwrap(), &0_u8, vec![])) .collect(); ixs.extend(vec![ ComputeBudgetInstruction::request_heap_frame(40 * 1024), From 4d1b178d81501792ac9bea1d0fd07b7f11bbc031 Mon Sep 17 00:00:00 2001 From: Tao Zhu Date: Thu, 8 Aug 2024 18:37:33 -0500 Subject: [PATCH 3/8] add black-boxing, remove debugging assert --- .../process_compute_budget_instructions.rs | 36 +++++++++---------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/runtime-transaction/benches/process_compute_budget_instructions.rs b/runtime-transaction/benches/process_compute_budget_instructions.rs index ca146bfe6b149d..98b77c16ed93c2 100644 --- a/runtime-transaction/benches/process_compute_budget_instructions.rs +++ b/runtime-transaction/benches/process_compute_budget_instructions.rs @@ -12,7 +12,7 @@ use { system_instruction::{self}, transaction::{SanitizedTransaction, Transaction}, }, - test::Bencher, + test::{black_box, Bencher}, }; const NUM_TRANSACTIONS_PER_ITER: usize = 1024; @@ -33,10 +33,10 @@ fn bench_process_compute_budget_instructions_empty(bencher: &mut Bencher) { let tx = build_sanitized_transaction(&Keypair::new(), &[]); bencher.iter(|| { (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { - assert!( - process_compute_budget_instructions(tx.message().program_instructions_iter()) - .is_ok() + assert!(process_compute_budget_instructions( + black_box(tx.message()).program_instructions_iter() ) + .is_ok()) }) }); } @@ -46,14 +46,13 @@ fn bench_process_compute_budget_instructions_non_builtins(bencher: &mut Bencher) let ixs: Vec<_> = (0..4) .map(|_| Instruction::new_with_bincode(DUMMY_PROGRAM_ID.parse().unwrap(), &0_u8, vec![])) .collect(); - assert_eq!(4, ixs.len()); let tx = build_sanitized_transaction(&Keypair::new(), &ixs); bencher.iter(|| { (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { - assert!( - process_compute_budget_instructions(tx.message().program_instructions_iter()) - .is_ok() + assert!(process_compute_budget_instructions( + black_box(tx.message()).program_instructions_iter() ) + .is_ok()) }) }); } @@ -66,14 +65,13 @@ fn bench_process_compute_budget_instructions_compute_budgets(bencher: &mut Bench ComputeBudgetInstruction::set_compute_unit_price(u64::MAX), ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(u32::MAX), ]; - assert_eq!(4, ixs.len()); let tx = build_sanitized_transaction(&Keypair::new(), &ixs); bencher.iter(|| { (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { - assert!( - process_compute_budget_instructions(tx.message().program_instructions_iter()) - .is_ok() + assert!(process_compute_budget_instructions( + black_box(tx.message()).program_instructions_iter() ) + .is_ok()) }) }); } @@ -90,14 +88,13 @@ fn bench_process_compute_budget_instructions_builtins(bencher: &mut Bencher) { ), Instruction::new_with_bincode(solana_sdk::loader_v4::id(), &0_u8, vec![]), ]; - assert_eq!(4, ixs.len()); let tx = build_sanitized_transaction(&Keypair::new(), &ixs); bencher.iter(|| { (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { - assert!( - process_compute_budget_instructions(tx.message().program_instructions_iter()) - .is_ok() + assert!(process_compute_budget_instructions( + black_box(tx.message()).program_instructions_iter() ) + .is_ok()) }) }); } @@ -114,15 +111,14 @@ fn bench_process_compute_budget_instructions_mixed(bencher: &mut Bencher) { ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(u32::MAX), system_instruction::transfer(&payer_keypair.pubkey(), &Pubkey::new_unique(), 1), ]); - assert_eq!(133, ixs.len()); let tx = build_sanitized_transaction(&payer_keypair, &ixs); bencher.iter(|| { (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { - assert!( - process_compute_budget_instructions(tx.message().program_instructions_iter()) - .is_ok() + assert!(process_compute_budget_instructions( + black_box(tx.message()).program_instructions_iter() ) + .is_ok()) }) }); } From ec916373e7b6119d38e61cd29fb2cda1b857ac7a Mon Sep 17 00:00:00 2001 From: Tao Zhu Date: Fri, 9 Aug 2024 16:36:36 -0500 Subject: [PATCH 4/8] use criterion --- Cargo.lock | 1 + runtime-transaction/Cargo.toml | 5 + .../process_compute_budget_instructions.rs | 184 ++++++++++-------- 3 files changed, 107 insertions(+), 83 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 042b140a76edcd..b05ec9e86eacd3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7341,6 +7341,7 @@ name = "solana-runtime-transaction" version = "2.1.0" dependencies = [ "bincode", + "criterion", "log", "rand 0.8.5", "rustc_version 0.4.0", diff --git a/runtime-transaction/Cargo.toml b/runtime-transaction/Cargo.toml index 200fbd6614220f..2965564e63d3dc 100644 --- a/runtime-transaction/Cargo.toml +++ b/runtime-transaction/Cargo.toml @@ -21,6 +21,7 @@ name = "solana_runtime_transaction" [dev-dependencies] bincode = { workspace = true } +criterion = { workspace = true } rand = { workspace = true } solana-builtins-default-costs = { workspace = true } solana-program = { workspace = true } @@ -30,3 +31,7 @@ targets = ["x86_64-unknown-linux-gnu"] [build-dependencies] rustc_version = { workspace = true, optional = true } + +[[bench]] +name = "process_compute_budget_instructions" +harness = false diff --git a/runtime-transaction/benches/process_compute_budget_instructions.rs b/runtime-transaction/benches/process_compute_budget_instructions.rs index 98b77c16ed93c2..3fd742e4dec8cb 100644 --- a/runtime-transaction/benches/process_compute_budget_instructions.rs +++ b/runtime-transaction/benches/process_compute_budget_instructions.rs @@ -1,6 +1,5 @@ -#![feature(test)] -extern crate test; use { + criterion::{black_box, criterion_group, criterion_main, Criterion}, solana_runtime_transaction::instructions_processor::process_compute_budget_instructions, solana_sdk::{ compute_budget::ComputeBudgetInstruction, @@ -12,7 +11,6 @@ use { system_instruction::{self}, transaction::{SanitizedTransaction, Transaction}, }, - test::{black_box, Bencher}, }; const NUM_TRANSACTIONS_PER_ITER: usize = 1024; @@ -28,97 +26,117 @@ fn build_sanitized_transaction( ))) } -#[bench] -fn bench_process_compute_budget_instructions_empty(bencher: &mut Bencher) { - let tx = build_sanitized_transaction(&Keypair::new(), &[]); - bencher.iter(|| { - (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { - assert!(process_compute_budget_instructions( - black_box(tx.message()).program_instructions_iter() - ) - .is_ok()) - }) +fn bench_process_compute_budget_instructions_empty(c: &mut Criterion) { + c.bench_function("No instructions", |bencher| { + let tx = build_sanitized_transaction(&Keypair::new(), &[]); + bencher.iter(|| { + (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { + assert!(process_compute_budget_instructions(black_box( + tx.message().program_instructions_iter() + )) + .is_ok()) + }) + }); }); } -#[bench] -fn bench_process_compute_budget_instructions_non_builtins(bencher: &mut Bencher) { - let ixs: Vec<_> = (0..4) - .map(|_| Instruction::new_with_bincode(DUMMY_PROGRAM_ID.parse().unwrap(), &0_u8, vec![])) - .collect(); - let tx = build_sanitized_transaction(&Keypair::new(), &ixs); - bencher.iter(|| { - (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { - assert!(process_compute_budget_instructions( - black_box(tx.message()).program_instructions_iter() - ) - .is_ok()) - }) +fn bench_process_compute_budget_instructions_no_builtins(c: &mut Criterion) { + c.bench_function("No builtins", |bencher| { + let ixs: Vec<_> = (0..4) + .map(|_| { + Instruction::new_with_bincode(DUMMY_PROGRAM_ID.parse().unwrap(), &0_u8, vec![]) + }) + .collect(); + let tx = build_sanitized_transaction(&Keypair::new(), &ixs); + bencher.iter(|| { + (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { + assert!(process_compute_budget_instructions(black_box( + tx.message().program_instructions_iter() + )) + .is_ok()) + }) + }); }); } -#[bench] -fn bench_process_compute_budget_instructions_compute_budgets(bencher: &mut Bencher) { - let ixs = vec![ - ComputeBudgetInstruction::request_heap_frame(40 * 1024), - ComputeBudgetInstruction::set_compute_unit_limit(u32::MAX), - ComputeBudgetInstruction::set_compute_unit_price(u64::MAX), - ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(u32::MAX), - ]; - let tx = build_sanitized_transaction(&Keypair::new(), &ixs); - bencher.iter(|| { - (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { - assert!(process_compute_budget_instructions( - black_box(tx.message()).program_instructions_iter() - ) - .is_ok()) - }) +fn bench_process_compute_budget_instructions_compute_budgets(c: &mut Criterion) { + c.bench_function("Only compute-budget instructions", |bencher| { + let ixs = vec![ + ComputeBudgetInstruction::request_heap_frame(40 * 1024), + ComputeBudgetInstruction::set_compute_unit_limit(u32::MAX), + ComputeBudgetInstruction::set_compute_unit_price(u64::MAX), + ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(u32::MAX), + ]; + let tx = build_sanitized_transaction(&Keypair::new(), &ixs); + bencher.iter(|| { + (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { + assert!(process_compute_budget_instructions(black_box( + tx.message().program_instructions_iter() + )) + .is_ok()) + }) + }); }); } -#[bench] -fn bench_process_compute_budget_instructions_builtins(bencher: &mut Bencher) { - let ixs = vec![ - Instruction::new_with_bincode(solana_sdk::bpf_loader::id(), &0_u8, vec![]), - Instruction::new_with_bincode(solana_sdk::secp256k1_program::id(), &0_u8, vec![]), - Instruction::new_with_bincode( - solana_sdk::address_lookup_table::program::id(), - &0_u8, - vec![], - ), - Instruction::new_with_bincode(solana_sdk::loader_v4::id(), &0_u8, vec![]), - ]; - let tx = build_sanitized_transaction(&Keypair::new(), &ixs); - bencher.iter(|| { - (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { - assert!(process_compute_budget_instructions( - black_box(tx.message()).program_instructions_iter() - ) - .is_ok()) - }) +fn bench_process_compute_budget_instructions_builtins(c: &mut Criterion) { + c.bench_function("Only builtins", |bencher| { + let ixs = vec![ + Instruction::new_with_bincode(solana_sdk::bpf_loader::id(), &0_u8, vec![]), + Instruction::new_with_bincode(solana_sdk::secp256k1_program::id(), &0_u8, vec![]), + Instruction::new_with_bincode( + solana_sdk::address_lookup_table::program::id(), + &0_u8, + vec![], + ), + Instruction::new_with_bincode(solana_sdk::loader_v4::id(), &0_u8, vec![]), + ]; + let tx = build_sanitized_transaction(&Keypair::new(), &ixs); + bencher.iter(|| { + (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { + assert!(process_compute_budget_instructions(black_box( + tx.message().program_instructions_iter() + )) + .is_ok()) + }) + }); }); } -#[bench] -fn bench_process_compute_budget_instructions_mixed(bencher: &mut Bencher) { - let payer_keypair = Keypair::new(); - let mut ixs: Vec<_> = (0..128) - .map(|_| Instruction::new_with_bincode(DUMMY_PROGRAM_ID.parse().unwrap(), &0_u8, vec![])) - .collect(); - ixs.extend(vec![ - ComputeBudgetInstruction::request_heap_frame(40 * 1024), - ComputeBudgetInstruction::set_compute_unit_limit(u32::MAX), - ComputeBudgetInstruction::set_compute_unit_price(u64::MAX), - ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(u32::MAX), - system_instruction::transfer(&payer_keypair.pubkey(), &Pubkey::new_unique(), 1), - ]); - let tx = build_sanitized_transaction(&payer_keypair, &ixs); - bencher.iter(|| { - (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { - assert!(process_compute_budget_instructions( - black_box(tx.message()).program_instructions_iter() - ) - .is_ok()) - }) +fn bench_process_compute_budget_instructions_mixed(c: &mut Criterion) { + c.bench_function("Mixed instructions", |bencher| { + let payer_keypair = Keypair::new(); + let mut ixs: Vec<_> = (0..128) + .map(|_| { + Instruction::new_with_bincode(DUMMY_PROGRAM_ID.parse().unwrap(), &0_u8, vec![]) + }) + .collect(); + ixs.extend(vec![ + ComputeBudgetInstruction::request_heap_frame(40 * 1024), + ComputeBudgetInstruction::set_compute_unit_limit(u32::MAX), + ComputeBudgetInstruction::set_compute_unit_price(u64::MAX), + ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(u32::MAX), + system_instruction::transfer(&payer_keypair.pubkey(), &Pubkey::new_unique(), 1), + ]); + let tx = build_sanitized_transaction(&payer_keypair, &ixs); + + bencher.iter(|| { + (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { + assert!(process_compute_budget_instructions(black_box( + tx.message().program_instructions_iter() + )) + .is_ok()) + }) + }); }); } + +criterion_group!( + benches, + bench_process_compute_budget_instructions_empty, + bench_process_compute_budget_instructions_no_builtins, + bench_process_compute_budget_instructions_compute_budgets, + bench_process_compute_budget_instructions_builtins, + bench_process_compute_budget_instructions_mixed, +); +criterion_main!(benches); From d2b0134f77e20d308210718d32dfe787994c8243 Mon Sep 17 00:00:00 2001 From: Tao Zhu Date: Mon, 12 Aug 2024 11:34:08 -0500 Subject: [PATCH 5/8] no data for dummy ix --- .../process_compute_budget_instructions.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/runtime-transaction/benches/process_compute_budget_instructions.rs b/runtime-transaction/benches/process_compute_budget_instructions.rs index 3fd742e4dec8cb..479b0b1108a888 100644 --- a/runtime-transaction/benches/process_compute_budget_instructions.rs +++ b/runtime-transaction/benches/process_compute_budget_instructions.rs @@ -43,9 +43,7 @@ fn bench_process_compute_budget_instructions_empty(c: &mut Criterion) { fn bench_process_compute_budget_instructions_no_builtins(c: &mut Criterion) { c.bench_function("No builtins", |bencher| { let ixs: Vec<_> = (0..4) - .map(|_| { - Instruction::new_with_bincode(DUMMY_PROGRAM_ID.parse().unwrap(), &0_u8, vec![]) - }) + .map(|_| Instruction::new_with_bincode(DUMMY_PROGRAM_ID.parse().unwrap(), &(), vec![])) .collect(); let tx = build_sanitized_transaction(&Keypair::new(), &ixs); bencher.iter(|| { @@ -82,14 +80,14 @@ fn bench_process_compute_budget_instructions_compute_budgets(c: &mut Criterion) fn bench_process_compute_budget_instructions_builtins(c: &mut Criterion) { c.bench_function("Only builtins", |bencher| { let ixs = vec![ - Instruction::new_with_bincode(solana_sdk::bpf_loader::id(), &0_u8, vec![]), - Instruction::new_with_bincode(solana_sdk::secp256k1_program::id(), &0_u8, vec![]), + Instruction::new_with_bincode(solana_sdk::bpf_loader::id(), &(), vec![]), + Instruction::new_with_bincode(solana_sdk::secp256k1_program::id(), &(), vec![]), Instruction::new_with_bincode( solana_sdk::address_lookup_table::program::id(), - &0_u8, + &(), vec![], ), - Instruction::new_with_bincode(solana_sdk::loader_v4::id(), &0_u8, vec![]), + Instruction::new_with_bincode(solana_sdk::loader_v4::id(), &(), vec![]), ]; let tx = build_sanitized_transaction(&Keypair::new(), &ixs); bencher.iter(|| { @@ -107,9 +105,7 @@ fn bench_process_compute_budget_instructions_mixed(c: &mut Criterion) { c.bench_function("Mixed instructions", |bencher| { let payer_keypair = Keypair::new(); let mut ixs: Vec<_> = (0..128) - .map(|_| { - Instruction::new_with_bincode(DUMMY_PROGRAM_ID.parse().unwrap(), &0_u8, vec![]) - }) + .map(|_| Instruction::new_with_bincode(DUMMY_PROGRAM_ID.parse().unwrap(), &(), vec![])) .collect(); ixs.extend(vec![ ComputeBudgetInstruction::request_heap_frame(40 * 1024), From 4274f6c4ffcdf91e482bfc6d9584c2e67f0b32f1 Mon Sep 17 00:00:00 2001 From: Tao Zhu Date: Tue, 13 Aug 2024 10:52:39 -0500 Subject: [PATCH 6/8] customize criterion to use Throughput option --- .../process_compute_budget_instructions.rs | 176 ++++++++++-------- 1 file changed, 95 insertions(+), 81 deletions(-) diff --git a/runtime-transaction/benches/process_compute_budget_instructions.rs b/runtime-transaction/benches/process_compute_budget_instructions.rs index 479b0b1108a888..a1a8661fc7f21c 100644 --- a/runtime-transaction/benches/process_compute_budget_instructions.rs +++ b/runtime-transaction/benches/process_compute_budget_instructions.rs @@ -1,5 +1,5 @@ use { - criterion::{black_box, criterion_group, criterion_main, Criterion}, + criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput}, solana_runtime_transaction::instructions_processor::process_compute_budget_instructions, solana_sdk::{ compute_budget::ComputeBudgetInstruction, @@ -27,104 +27,118 @@ fn build_sanitized_transaction( } fn bench_process_compute_budget_instructions_empty(c: &mut Criterion) { - c.bench_function("No instructions", |bencher| { - let tx = build_sanitized_transaction(&Keypair::new(), &[]); - bencher.iter(|| { - (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { - assert!(process_compute_budget_instructions(black_box( - tx.message().program_instructions_iter() - )) - .is_ok()) - }) + c.benchmark_group(format!("bench_size_{NUM_TRANSACTIONS_PER_ITER}")) + .throughput(Throughput::Elements(NUM_TRANSACTIONS_PER_ITER as u64)) + .bench_function("No instructions", |bencher| { + let tx = build_sanitized_transaction(&Keypair::new(), &[]); + bencher.iter(|| { + (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { + assert!(process_compute_budget_instructions(black_box( + tx.message().program_instructions_iter() + )) + .is_ok()) + }) + }); }); - }); } fn bench_process_compute_budget_instructions_no_builtins(c: &mut Criterion) { - c.bench_function("No builtins", |bencher| { - let ixs: Vec<_> = (0..4) - .map(|_| Instruction::new_with_bincode(DUMMY_PROGRAM_ID.parse().unwrap(), &(), vec![])) - .collect(); - let tx = build_sanitized_transaction(&Keypair::new(), &ixs); - bencher.iter(|| { - (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { - assert!(process_compute_budget_instructions(black_box( - tx.message().program_instructions_iter() - )) - .is_ok()) - }) + c.benchmark_group(format!("bench_size_{NUM_TRANSACTIONS_PER_ITER}")) + .throughput(Throughput::Elements(NUM_TRANSACTIONS_PER_ITER as u64)) + .bench_function("No builtins", |bencher| { + let ixs: Vec<_> = (0..4) + .map(|_| { + Instruction::new_with_bincode(DUMMY_PROGRAM_ID.parse().unwrap(), &(), vec![]) + }) + .collect(); + let tx = build_sanitized_transaction(&Keypair::new(), &ixs); + bencher.iter(|| { + (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { + assert!(process_compute_budget_instructions(black_box( + tx.message().program_instructions_iter() + )) + .is_ok()) + }) + }); }); - }); } fn bench_process_compute_budget_instructions_compute_budgets(c: &mut Criterion) { - c.bench_function("Only compute-budget instructions", |bencher| { - let ixs = vec![ - ComputeBudgetInstruction::request_heap_frame(40 * 1024), - ComputeBudgetInstruction::set_compute_unit_limit(u32::MAX), - ComputeBudgetInstruction::set_compute_unit_price(u64::MAX), - ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(u32::MAX), - ]; - let tx = build_sanitized_transaction(&Keypair::new(), &ixs); - bencher.iter(|| { - (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { - assert!(process_compute_budget_instructions(black_box( - tx.message().program_instructions_iter() - )) - .is_ok()) - }) + c.benchmark_group(format!("bench_size_{NUM_TRANSACTIONS_PER_ITER}")) + .throughput(Throughput::Elements(NUM_TRANSACTIONS_PER_ITER as u64)) + .bench_function("Only compute-budget instructions", |bencher| { + let ixs = vec![ + ComputeBudgetInstruction::request_heap_frame(40 * 1024), + ComputeBudgetInstruction::set_compute_unit_limit(u32::MAX), + ComputeBudgetInstruction::set_compute_unit_price(u64::MAX), + ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(u32::MAX), + ]; + let tx = build_sanitized_transaction(&Keypair::new(), &ixs); + bencher.iter(|| { + (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { + assert!(process_compute_budget_instructions(black_box( + tx.message().program_instructions_iter() + )) + .is_ok()) + }) + }); }); - }); } fn bench_process_compute_budget_instructions_builtins(c: &mut Criterion) { - c.bench_function("Only builtins", |bencher| { - let ixs = vec![ - Instruction::new_with_bincode(solana_sdk::bpf_loader::id(), &(), vec![]), - Instruction::new_with_bincode(solana_sdk::secp256k1_program::id(), &(), vec![]), - Instruction::new_with_bincode( - solana_sdk::address_lookup_table::program::id(), - &(), - vec![], - ), - Instruction::new_with_bincode(solana_sdk::loader_v4::id(), &(), vec![]), - ]; - let tx = build_sanitized_transaction(&Keypair::new(), &ixs); - bencher.iter(|| { - (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { - assert!(process_compute_budget_instructions(black_box( - tx.message().program_instructions_iter() - )) - .is_ok()) - }) + c.benchmark_group(format!("bench_size_{NUM_TRANSACTIONS_PER_ITER}")) + .throughput(Throughput::Elements(NUM_TRANSACTIONS_PER_ITER as u64)) + .bench_function("Only builtins", |bencher| { + let ixs = vec![ + Instruction::new_with_bincode(solana_sdk::bpf_loader::id(), &(), vec![]), + Instruction::new_with_bincode(solana_sdk::secp256k1_program::id(), &(), vec![]), + Instruction::new_with_bincode( + solana_sdk::address_lookup_table::program::id(), + &(), + vec![], + ), + Instruction::new_with_bincode(solana_sdk::loader_v4::id(), &(), vec![]), + ]; + let tx = build_sanitized_transaction(&Keypair::new(), &ixs); + bencher.iter(|| { + (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { + assert!(process_compute_budget_instructions(black_box( + tx.message().program_instructions_iter() + )) + .is_ok()) + }) + }); }); - }); } fn bench_process_compute_budget_instructions_mixed(c: &mut Criterion) { - c.bench_function("Mixed instructions", |bencher| { - let payer_keypair = Keypair::new(); - let mut ixs: Vec<_> = (0..128) - .map(|_| Instruction::new_with_bincode(DUMMY_PROGRAM_ID.parse().unwrap(), &(), vec![])) - .collect(); - ixs.extend(vec![ - ComputeBudgetInstruction::request_heap_frame(40 * 1024), - ComputeBudgetInstruction::set_compute_unit_limit(u32::MAX), - ComputeBudgetInstruction::set_compute_unit_price(u64::MAX), - ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(u32::MAX), - system_instruction::transfer(&payer_keypair.pubkey(), &Pubkey::new_unique(), 1), - ]); - let tx = build_sanitized_transaction(&payer_keypair, &ixs); + c.benchmark_group(format!("bench_size_{NUM_TRANSACTIONS_PER_ITER}")) + .throughput(Throughput::Elements(NUM_TRANSACTIONS_PER_ITER as u64)) + .bench_function("Mixed instructions", |bencher| { + let payer_keypair = Keypair::new(); + let mut ixs: Vec<_> = (0..128) + .map(|_| { + Instruction::new_with_bincode(DUMMY_PROGRAM_ID.parse().unwrap(), &(), vec![]) + }) + .collect(); + ixs.extend(vec![ + ComputeBudgetInstruction::request_heap_frame(40 * 1024), + ComputeBudgetInstruction::set_compute_unit_limit(u32::MAX), + ComputeBudgetInstruction::set_compute_unit_price(u64::MAX), + ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(u32::MAX), + system_instruction::transfer(&payer_keypair.pubkey(), &Pubkey::new_unique(), 1), + ]); + let tx = build_sanitized_transaction(&payer_keypair, &ixs); - bencher.iter(|| { - (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { - assert!(process_compute_budget_instructions(black_box( - tx.message().program_instructions_iter() - )) - .is_ok()) - }) + bencher.iter(|| { + (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { + assert!(process_compute_budget_instructions(black_box( + tx.message().program_instructions_iter() + )) + .is_ok()) + }) + }); }); - }); } criterion_group!( From 2be74828512d5f2f3c1b108b4367f6f0381f9a93 Mon Sep 17 00:00:00 2001 From: Tao Zhu Date: Tue, 13 Aug 2024 16:26:34 -0500 Subject: [PATCH 7/8] rename bench group --- .../process_compute_budget_instructions.rs | 112 ++++++++++-------- 1 file changed, 64 insertions(+), 48 deletions(-) diff --git a/runtime-transaction/benches/process_compute_budget_instructions.rs b/runtime-transaction/benches/process_compute_budget_instructions.rs index a1a8661fc7f21c..55982d93aea7c8 100644 --- a/runtime-transaction/benches/process_compute_budget_instructions.rs +++ b/runtime-transaction/benches/process_compute_budget_instructions.rs @@ -27,9 +27,9 @@ fn build_sanitized_transaction( } fn bench_process_compute_budget_instructions_empty(c: &mut Criterion) { - c.benchmark_group(format!("bench_size_{NUM_TRANSACTIONS_PER_ITER}")) + c.benchmark_group("bench_process_compute_budget_instructions_empty") .throughput(Throughput::Elements(NUM_TRANSACTIONS_PER_ITER as u64)) - .bench_function("No instructions", |bencher| { + .bench_function("0 instructions", |bencher| { let tx = build_sanitized_transaction(&Keypair::new(), &[]); bencher.iter(|| { (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { @@ -43,30 +43,38 @@ fn bench_process_compute_budget_instructions_empty(c: &mut Criterion) { } fn bench_process_compute_budget_instructions_no_builtins(c: &mut Criterion) { - c.benchmark_group(format!("bench_size_{NUM_TRANSACTIONS_PER_ITER}")) + let num_instructions = 4; + c.benchmark_group("bench_process_compute_budget_instructions_no_builtins") .throughput(Throughput::Elements(NUM_TRANSACTIONS_PER_ITER as u64)) - .bench_function("No builtins", |bencher| { - let ixs: Vec<_> = (0..4) - .map(|_| { - Instruction::new_with_bincode(DUMMY_PROGRAM_ID.parse().unwrap(), &(), vec![]) - }) - .collect(); - let tx = build_sanitized_transaction(&Keypair::new(), &ixs); - bencher.iter(|| { - (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { - assert!(process_compute_budget_instructions(black_box( - tx.message().program_instructions_iter() - )) - .is_ok()) - }) - }); - }); + .bench_function( + format!("{num_instructions} dummy Instructions"), + |bencher| { + let ixs: Vec<_> = (0..num_instructions) + .map(|_| { + Instruction::new_with_bincode( + DUMMY_PROGRAM_ID.parse().unwrap(), + &(), + vec![], + ) + }) + .collect(); + let tx = build_sanitized_transaction(&Keypair::new(), &ixs); + bencher.iter(|| { + (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { + assert!(process_compute_budget_instructions(black_box( + tx.message().program_instructions_iter() + )) + .is_ok()) + }) + }); + }, + ); } fn bench_process_compute_budget_instructions_compute_budgets(c: &mut Criterion) { - c.benchmark_group(format!("bench_size_{NUM_TRANSACTIONS_PER_ITER}")) + c.benchmark_group("bench_process_compute_budget_instructions_compute_budgets") .throughput(Throughput::Elements(NUM_TRANSACTIONS_PER_ITER as u64)) - .bench_function("Only compute-budget instructions", |bencher| { + .bench_function("4 compute-budget instructions", |bencher| { let ixs = vec![ ComputeBudgetInstruction::request_heap_frame(40 * 1024), ComputeBudgetInstruction::set_compute_unit_limit(u32::MAX), @@ -86,9 +94,9 @@ fn bench_process_compute_budget_instructions_compute_budgets(c: &mut Criterion) } fn bench_process_compute_budget_instructions_builtins(c: &mut Criterion) { - c.benchmark_group(format!("bench_size_{NUM_TRANSACTIONS_PER_ITER}")) + c.benchmark_group("bench_process_compute_budget_instructions_builtins") .throughput(Throughput::Elements(NUM_TRANSACTIONS_PER_ITER as u64)) - .bench_function("Only builtins", |bencher| { + .bench_function("4 dummy builtins", |bencher| { let ixs = vec![ Instruction::new_with_bincode(solana_sdk::bpf_loader::id(), &(), vec![]), Instruction::new_with_bincode(solana_sdk::secp256k1_program::id(), &(), vec![]), @@ -112,33 +120,41 @@ fn bench_process_compute_budget_instructions_builtins(c: &mut Criterion) { } fn bench_process_compute_budget_instructions_mixed(c: &mut Criterion) { - c.benchmark_group(format!("bench_size_{NUM_TRANSACTIONS_PER_ITER}")) + let num_instructions = 1024; + c.benchmark_group("bench_process_compute_budget_instructions_mixed") .throughput(Throughput::Elements(NUM_TRANSACTIONS_PER_ITER as u64)) - .bench_function("Mixed instructions", |bencher| { - let payer_keypair = Keypair::new(); - let mut ixs: Vec<_> = (0..128) - .map(|_| { - Instruction::new_with_bincode(DUMMY_PROGRAM_ID.parse().unwrap(), &(), vec![]) - }) - .collect(); - ixs.extend(vec![ - ComputeBudgetInstruction::request_heap_frame(40 * 1024), - ComputeBudgetInstruction::set_compute_unit_limit(u32::MAX), - ComputeBudgetInstruction::set_compute_unit_price(u64::MAX), - ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(u32::MAX), - system_instruction::transfer(&payer_keypair.pubkey(), &Pubkey::new_unique(), 1), - ]); - let tx = build_sanitized_transaction(&payer_keypair, &ixs); + .bench_function( + format!("{num_instructions} mixed instructions"), + |bencher| { + let payer_keypair = Keypair::new(); + let mut ixs: Vec<_> = (0..num_instructions) + .map(|_| { + Instruction::new_with_bincode( + DUMMY_PROGRAM_ID.parse().unwrap(), + &(), + vec![], + ) + }) + .collect(); + ixs.extend(vec![ + ComputeBudgetInstruction::request_heap_frame(40 * 1024), + ComputeBudgetInstruction::set_compute_unit_limit(u32::MAX), + ComputeBudgetInstruction::set_compute_unit_price(u64::MAX), + ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(u32::MAX), + system_instruction::transfer(&payer_keypair.pubkey(), &Pubkey::new_unique(), 1), + ]); + let tx = build_sanitized_transaction(&payer_keypair, &ixs); - bencher.iter(|| { - (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { - assert!(process_compute_budget_instructions(black_box( - tx.message().program_instructions_iter() - )) - .is_ok()) - }) - }); - }); + bencher.iter(|| { + (0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| { + assert!(process_compute_budget_instructions(black_box( + tx.message().program_instructions_iter() + )) + .is_ok()) + }) + }); + }, + ); } criterion_group!( From c5ca9784903177641efb06586a9669d5e147924d Mon Sep 17 00:00:00 2001 From: Tao Zhu Date: Wed, 14 Aug 2024 09:45:38 -0500 Subject: [PATCH 8/8] set reaonsable worst-case --- .../benches/process_compute_budget_instructions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime-transaction/benches/process_compute_budget_instructions.rs b/runtime-transaction/benches/process_compute_budget_instructions.rs index 55982d93aea7c8..463a4cda596c3a 100644 --- a/runtime-transaction/benches/process_compute_budget_instructions.rs +++ b/runtime-transaction/benches/process_compute_budget_instructions.rs @@ -120,7 +120,7 @@ fn bench_process_compute_budget_instructions_builtins(c: &mut Criterion) { } fn bench_process_compute_budget_instructions_mixed(c: &mut Criterion) { - let num_instructions = 1024; + let num_instructions = 355; c.benchmark_group("bench_process_compute_budget_instructions_mixed") .throughput(Throughput::Elements(NUM_TRANSACTIONS_PER_ITER as u64)) .bench_function(