From 570d1a919729741f062ae0ad0b1f9bf52fbb83f2 Mon Sep 17 00:00:00 2001 From: Brooks Date: Mon, 4 Mar 2024 13:59:35 -0500 Subject: [PATCH] Adds a bench for hash_account() (#47) --- Cargo.lock | 1 + accounts-db/Cargo.toml | 5 ++++ accounts-db/benches/bench_hashing.rs | 43 ++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 accounts-db/benches/bench_hashing.rs diff --git a/Cargo.lock b/Cargo.lock index c72b90930d7cf9..a67b7aba0d6b64 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5390,6 +5390,7 @@ dependencies = [ "bytemuck", "byteorder", "bzip2", + "criterion", "crossbeam-channel", "dashmap", "ed25519-dalek", diff --git a/accounts-db/Cargo.toml b/accounts-db/Cargo.toml index b986c17de0636b..702f14f9f3b07d 100644 --- a/accounts-db/Cargo.toml +++ b/accounts-db/Cargo.toml @@ -70,6 +70,7 @@ name = "solana_accounts_db" [dev-dependencies] assert_matches = { workspace = true } +criterion = { workspace = true } ed25519-dalek = { workspace = true } libsecp256k1 = { workspace = true } memoffset = { workspace = true } @@ -89,3 +90,7 @@ rustc_version = { workspace = true } [features] dev-context-only-utils = [] + +[[bench]] +name = "bench_hashing" +harness = false diff --git a/accounts-db/benches/bench_hashing.rs b/accounts-db/benches/bench_hashing.rs new file mode 100644 index 00000000000000..3158f78c7a938f --- /dev/null +++ b/accounts-db/benches/bench_hashing.rs @@ -0,0 +1,43 @@ +use { + criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}, + solana_accounts_db::accounts_db::AccountsDb, + solana_sdk::{account::AccountSharedData, pubkey::Pubkey}, +}; + +const KB: usize = 1024; +const MB: usize = KB * KB; + +const DATA_SIZES: [usize; 6] = [ + 0, // the smallest account + 165, // the size of an spl token account + 200, // the size of a stake account + KB, // a medium sized account + MB, // a large sized account + 10 * MB, // the largest account +]; + +/// The number of bytes of *non account data* that are also hashed as +/// part of computing an account's hash. +/// +/// Ensure this constant stays in sync with the value of `META_SIZE` in +/// AccountsDb::hash_account_data(). +const META_SIZE: usize = 81; + +fn bench_hash_account(c: &mut Criterion) { + let lamports = 123_456_789; + let owner = Pubkey::default(); + let address = Pubkey::default(); + + let mut group = c.benchmark_group("hash_account"); + for data_size in DATA_SIZES { + let num_bytes = META_SIZE.checked_add(data_size).unwrap(); + group.throughput(Throughput::Bytes(num_bytes as u64)); + let account = AccountSharedData::new(lamports, data_size, &owner); + group.bench_function(BenchmarkId::new("data_size", data_size), |b| { + b.iter(|| AccountsDb::hash_account(&account, &address)); + }); + } +} + +criterion_group!(benches, bench_hash_account,); +criterion_main!(benches);