Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
Refactor - Simplify loader utils (#29699)
Browse files Browse the repository at this point in the history
* Moves all the common code of tests and benches into loader utils.

* Removes write_sbf_program().

* Removes ignored test: test_program_sbf_test_use_latest_executor2().
  • Loading branch information
Lichtso authored Jan 13, 2023
1 parent 055c6a5 commit ab97d37
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 491 deletions.
52 changes: 8 additions & 44 deletions programs/sbf/benches/bpf_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use {
bank::Bank,
bank_client::BankClient,
genesis_utils::{create_genesis_config, GenesisConfigInfo},
loader_utils::load_program,
loader_utils::{create_deprecated_program, load_program_from_file},
},
solana_sdk::{
bpf_loader,
Expand All @@ -35,54 +35,18 @@ use {
feature_set::FeatureSet,
instruction::{AccountMeta, Instruction},
message::Message,
pubkey::Pubkey,
signature::{Keypair, Signer},
signature::Signer,
},
std::{env, fs::File, io::Read, mem, path::PathBuf, sync::Arc},
std::{mem, sync::Arc},
test::Bencher,
};

/// SBF program file extension
const PLATFORM_FILE_EXTENSION_SBF: &str = "so";
/// Create a SBF program file name
fn create_sbf_path(name: &str) -> PathBuf {
let mut pathbuf = {
let current_exe = env::current_exe().unwrap();
PathBuf::from(current_exe.parent().unwrap().parent().unwrap())
};
pathbuf.push("sbf/");
pathbuf.push(name);
pathbuf.set_extension(PLATFORM_FILE_EXTENSION_SBF);
pathbuf
}

fn load_elf(name: &str) -> Result<Vec<u8>, std::io::Error> {
let path = create_sbf_path(name);
let mut file = File::open(&path).expect(&format!("Unable to open {:?}", path));
let mut elf = Vec::new();
file.read_to_end(&mut elf).unwrap();
Ok(elf)
}

fn load_sbf_program(
bank_client: &BankClient,
loader_id: &Pubkey,
payer_keypair: &Keypair,
name: &str,
) -> Pubkey {
let path = create_sbf_path(name);
let mut file = File::open(path).unwrap();
let mut elf = Vec::new();
file.read_to_end(&mut elf).unwrap();
load_program(bank_client, payer_keypair, loader_id, elf)
}

const ARMSTRONG_LIMIT: u64 = 500;
const ARMSTRONG_EXPECTED: u64 = 5;

#[bench]
fn bench_program_create_executable(bencher: &mut Bencher) {
let elf = load_elf("bench_alu").unwrap();
let elf = load_program_from_file("bench_alu");

let loader = create_loader(
&FeatureSet::default(),
Expand All @@ -106,7 +70,7 @@ fn bench_program_alu(bencher: &mut Bencher) {
.write_u64::<LittleEndian>(ARMSTRONG_LIMIT)
.unwrap();
inner_iter.write_u64::<LittleEndian>(0).unwrap();
let elf = load_elf("bench_alu").unwrap();
let elf = load_program_from_file("bench_alu");
let loader_id = bpf_loader::id();
with_mock_invoke_context(loader_id, 10000001, false, |invoke_context| {
let loader = create_loader(
Expand Down Expand Up @@ -196,7 +160,7 @@ fn bench_program_execute_noop(bencher: &mut Bencher) {
let bank_client = BankClient::new_shared(&bank);

let invoke_program_id =
load_sbf_program(&bank_client, &bpf_loader::id(), &mint_keypair, "noop");
create_deprecated_program(&bank_client, &bpf_loader::id(), &mint_keypair, "noop");

let mint_pubkey = mint_keypair.pubkey();
let account_metas = vec![AccountMeta::new(mint_pubkey, true)];
Expand All @@ -219,7 +183,7 @@ fn bench_program_execute_noop(bencher: &mut Bencher) {

#[bench]
fn bench_create_vm(bencher: &mut Bencher) {
let elf = load_elf("noop").unwrap();
let elf = load_program_from_file("noop");
let loader_id = bpf_loader::id();
with_mock_invoke_context(loader_id, 10000001, false, |invoke_context| {
const BUDGET: u64 = 200_000;
Expand Down Expand Up @@ -264,7 +228,7 @@ fn bench_create_vm(bencher: &mut Bencher) {

#[bench]
fn bench_instruction_count_tuner(_bencher: &mut Bencher) {
let elf = load_elf("tuner").unwrap();
let elf = load_program_from_file("tuner");
let loader_id = bpf_loader::id();
with_mock_invoke_context(loader_id, 10000001, true, |invoke_context| {
const BUDGET: u64 = 200_000;
Expand Down
Loading

0 comments on commit ab97d37

Please sign in to comment.