From d8dc218479a1e52a03a6607dd8f3b92fba8bd8e9 Mon Sep 17 00:00:00 2001 From: waitfreemaxi Date: Sat, 22 Jun 2024 10:17:04 -0700 Subject: [PATCH] add transfer benchmarks --- harness/Cargo.toml | 8 ++++++ harness/benches/ips.rs | 65 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 harness/benches/ips.rs diff --git a/harness/Cargo.toml b/harness/Cargo.toml index fc988e2..9b5edb8 100644 --- a/harness/Cargo.toml +++ b/harness/Cargo.toml @@ -10,3 +10,11 @@ solana-program-runtime = { workspace = true } solana-system-program = { workspace = true } solana-sdk = { workspace = true } solana-logger = "2.0.0" + + +[[bench]] +name = "ips" +harness = false + +[dev-dependencies] +criterion = "0.5.1" diff --git a/harness/benches/ips.rs b/harness/benches/ips.rs new file mode 100644 index 0000000..30aac2b --- /dev/null +++ b/harness/benches/ips.rs @@ -0,0 +1,65 @@ +//! Benches Mollusk invocation (instructions per second) +use criterion::{criterion_group, criterion_main, Criterion, Throughput}; +use mollusk::{result::Check, Mollusk}; +use solana_sdk::{ + account::AccountSharedData, native_token::LAMPORTS_PER_SOL, pubkey::Pubkey, system_instruction, + system_program, +}; +use solana_system_program::system_processor::DEFAULT_COMPUTE_UNITS; + +fn transfer_checked_unchecked(c: &mut Criterion) { + let sender = Pubkey::new_unique(); + let recipient = Pubkey::new_unique(); + + let base_lamports = 100 * LAMPORTS_PER_SOL; + let transfer_amount = 1; + + let instruction = system_instruction::transfer(&sender, &recipient, transfer_amount); + let accounts = vec![ + ( + sender, + AccountSharedData::new(base_lamports, 0, &system_program::id()), + ), + ( + recipient, + AccountSharedData::new(base_lamports, 0, &system_program::id()), + ), + ]; + let checks = vec![ + Check::success(), + Check::compute_units(DEFAULT_COMPUTE_UNITS), + Check::account(&sender) + .lamports(base_lamports - transfer_amount) + .build(), + Check::account(&recipient) + .lamports(base_lamports + transfer_amount) + .build(), + ]; + + // No logs for bench + let mollusk = Mollusk::default(); + solana_logger::setup_with(""); + + // Create transfers group with elements/second + let mut g = c.benchmark_group("transfers"); + g.throughput(Throughput::Elements(1)); + + // Bench transfer with post-execution checks + g.bench_function("transfer_checked", |b| { + b.iter(|| { + mollusk.process_and_validate_instruction(&instruction, accounts.clone(), &checks); + }) + }); + + // Bench transfer without post-execution checks + g.bench_function("transfer_unchecked", |b| { + b.iter(|| { + mollusk.process_instruction(&instruction, accounts.clone()); + }) + }); + + g.finish(); +} + +criterion_group!(transfers, transfer_checked_unchecked); +criterion_main!(transfers);