Skip to content

Commit

Permalink
use criterion
Browse files Browse the repository at this point in the history
  • Loading branch information
tao-stones committed Aug 9, 2024
1 parent 767c6f6 commit 2a6d12a
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 83 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions runtime-transaction/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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
184 changes: 101 additions & 83 deletions runtime-transaction/benches/process_compute_budget_instructions.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -12,7 +11,6 @@ use {
system_instruction::{self},
transaction::{SanitizedTransaction, Transaction},
},
test::{black_box, Bencher},
};

const NUM_TRANSACTIONS_PER_ITER: usize = 1024;
Expand All @@ -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);

0 comments on commit 2a6d12a

Please sign in to comment.