From 2a6d12a3ac9130cc50ee7ac597a1800e24260f97 Mon Sep 17 00:00:00 2001 From: Tao Zhu Date: Fri, 9 Aug 2024 16:36:36 -0500 Subject: [PATCH] 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 38fe0617bdc8cd..ba271523413574 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7343,6 +7343,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);