Skip to content

Commit

Permalink
Benchmark compute-budget instructions (solana-labs#3614)
Browse files Browse the repository at this point in the history
* Benchmark compute-budget instructions

* Move bench to separate and unpublished crate to break circular dependency
  • Loading branch information
tao-stones authored Nov 19, 2024
1 parent aeb5518 commit be47703
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ members = [
"programs/bpf_loader",
"programs/bpf_loader/gen-syscall-list",
"programs/compute-budget",
"programs/compute-budget-bench",
"programs/config",
"programs/ed25519-tests",
"programs/loader-v4",
Expand Down
24 changes: 24 additions & 0 deletions programs/compute-budget-bench/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "solana-compute-budget-program-bench"
publish = false
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
criterion = { workspace = true }
solana-compute-budget = { workspace = true }
solana-compute-budget-program = { workspace = true }
solana-runtime-transaction = { workspace = true }
solana-sdk = { workspace = true }
solana-svm-transaction = { workspace = true }

[[bench]]
name = "compute_budget"
harness = false

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
136 changes: 136 additions & 0 deletions programs/compute-budget-bench/benches/compute_budget.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
use {
criterion::{black_box, criterion_group, criterion_main, Criterion},
solana_compute_budget::compute_budget_limits::ComputeBudgetLimits,
solana_runtime_transaction::instructions_processor::process_compute_budget_instructions,
solana_sdk::{compute_budget::ComputeBudgetInstruction, instruction::CompiledInstruction},
solana_svm_transaction::instruction::SVMInstruction,
std::num::NonZero,
};

const ONE_PAGE: u32 = 32 * 1024;
const SIXTY_FOUR_MB: u32 = 64 * 1024 * 1024;

fn bench_request_heap_frame(c: &mut Criterion) {
let instruction = [(
solana_sdk::compute_budget::id(),
CompiledInstruction::new_from_raw_parts(
0,
ComputeBudgetInstruction::request_heap_frame(ONE_PAGE).data,
vec![],
),
)];

c.bench_function("request_heap_limit", |bencher| {
bencher.iter(|| {
assert_eq!(
process_compute_budget_instructions(black_box(
instruction
.iter()
.map(|(id, ix)| (id, SVMInstruction::from(ix)))
)),
Ok(ComputeBudgetLimits {
updated_heap_bytes: ONE_PAGE,
compute_unit_limit: 0,
compute_unit_price: 0,
loaded_accounts_bytes: NonZero::new(SIXTY_FOUR_MB).unwrap()
})
)
})
});
}

fn bench_set_compute_unit_limit(c: &mut Criterion) {
let instruction = [(
solana_sdk::compute_budget::id(),
CompiledInstruction::new_from_raw_parts(
0,
ComputeBudgetInstruction::set_compute_unit_limit(1024).data,
vec![],
),
)];

c.bench_function("set_compute_unit_limit", |bencher| {
bencher.iter(|| {
assert_eq!(
process_compute_budget_instructions(black_box(
instruction
.iter()
.map(|(id, ix)| (id, SVMInstruction::from(ix)))
)),
Ok(ComputeBudgetLimits {
updated_heap_bytes: ONE_PAGE,
compute_unit_limit: 1024,
compute_unit_price: 0,
loaded_accounts_bytes: NonZero::new(SIXTY_FOUR_MB).unwrap()
})
)
})
});
}

fn bench_set_compute_unit_price(c: &mut Criterion) {
let instruction = [(
solana_sdk::compute_budget::id(),
CompiledInstruction::new_from_raw_parts(
0,
ComputeBudgetInstruction::set_compute_unit_price(1).data,
vec![],
),
)];

c.bench_function("set_compute_unit_price", |bencher| {
bencher.iter(|| {
assert_eq!(
process_compute_budget_instructions(black_box(
instruction
.iter()
.map(|(id, ix)| (id, SVMInstruction::from(ix)))
)),
Ok(ComputeBudgetLimits {
updated_heap_bytes: ONE_PAGE,
compute_unit_limit: 0,
compute_unit_price: 1,
loaded_accounts_bytes: NonZero::new(SIXTY_FOUR_MB).unwrap()
})
)
})
});
}

fn bench_set_loaded_accounts_data_size_limit(c: &mut Criterion) {
let instruction = [(
solana_sdk::compute_budget::id(),
CompiledInstruction::new_from_raw_parts(
0,
ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(1).data,
vec![],
),
)];

c.bench_function("set_loaded_accounts_data_size_limit", |bencher| {
bencher.iter(|| {
assert_eq!(
process_compute_budget_instructions(black_box(
instruction
.iter()
.map(|(id, ix)| (id, SVMInstruction::from(ix)))
)),
Ok(ComputeBudgetLimits {
updated_heap_bytes: ONE_PAGE,
compute_unit_limit: 0,
compute_unit_price: 0,
loaded_accounts_bytes: NonZero::new(1).unwrap()
})
)
})
});
}

criterion_group!(
benches,
bench_request_heap_frame,
bench_set_compute_unit_limit,
bench_set_compute_unit_price,
bench_set_loaded_accounts_data_size_limit,
);
criterion_main!(benches);

0 comments on commit be47703

Please sign in to comment.