|
| 1 | +use { |
| 2 | + mollusk_svm_bencher::Bench, |
| 3 | + serde::Serialize, |
| 4 | + solana_config_program::{ |
| 5 | + instruction::store, |
| 6 | + state::{ConfigKeys, ConfigState}, |
| 7 | + }, |
| 8 | + solana_sdk::{ |
| 9 | + account::AccountSharedData, |
| 10 | + hash::Hash, |
| 11 | + instruction::{AccountMeta, Instruction}, |
| 12 | + pubkey::Pubkey, |
| 13 | + rent::Rent, |
| 14 | + }, |
| 15 | +}; |
| 16 | + |
| 17 | +/// Helper struct to convert to a `Bench`. |
| 18 | +pub struct BenchContext { |
| 19 | + label: String, |
| 20 | + instruction: Instruction, |
| 21 | + accounts: Vec<(Pubkey, AccountSharedData)>, |
| 22 | +} |
| 23 | + |
| 24 | +impl BenchContext { |
| 25 | + /// Convert to a `Bench`. |
| 26 | + pub fn bench(&self) -> Bench { |
| 27 | + (self.label.as_str(), &self.instruction, &self.accounts) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/// Trait to avoid re-defining the same instruction and account constructors |
| 32 | +/// for each `ConfigState`. |
| 33 | +pub trait BenchSetup: ConfigState + Default { |
| 34 | + const BENCH_ID: &'static str; |
| 35 | + |
| 36 | + fn default_account_state(keys: Vec<(Pubkey, bool)>) -> (ConfigKeys, Self) { |
| 37 | + (ConfigKeys { keys }, Self::default()) |
| 38 | + } |
| 39 | + |
| 40 | + fn default_space(keys: Vec<(Pubkey, bool)>) -> usize { |
| 41 | + (Self::max_space() |
| 42 | + .checked_add(ConfigKeys::serialized_size(keys)) |
| 43 | + .unwrap()) as usize |
| 44 | + } |
| 45 | + |
| 46 | + fn keys(keys_len: usize) -> Vec<(Pubkey, bool)> { |
| 47 | + (0..keys_len) |
| 48 | + .map(|_| (Pubkey::new_unique(), false)) |
| 49 | + .collect() |
| 50 | + } |
| 51 | + |
| 52 | + fn init(keys_len: usize) -> BenchContext { |
| 53 | + let config_pubkey = Pubkey::new_unique(); |
| 54 | + let keys = Self::keys(keys_len); |
| 55 | + let space = Self::default_space(keys.clone()); |
| 56 | + let lamports = Rent::default().minimum_balance(space); |
| 57 | + |
| 58 | + let instruction = { |
| 59 | + let account_metas = vec![AccountMeta::new(config_pubkey, true)]; |
| 60 | + let account_data = Self::default_account_state(keys); |
| 61 | + Instruction::new_with_bincode(solana_config_program::id(), &account_data, account_metas) |
| 62 | + }; |
| 63 | + |
| 64 | + let accounts = vec![( |
| 65 | + config_pubkey, |
| 66 | + AccountSharedData::new(lamports, space, &solana_config_program::id()), |
| 67 | + )]; |
| 68 | + |
| 69 | + BenchContext { |
| 70 | + label: format!("{}_init_{}_keys", Self::BENCH_ID, keys_len), |
| 71 | + instruction, |
| 72 | + accounts, |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + fn store(keys_len: usize) -> BenchContext { |
| 77 | + let config_pubkey = Pubkey::new_unique(); |
| 78 | + let keys = Self::keys(keys_len); |
| 79 | + let space = Self::default_space(keys.clone()); |
| 80 | + let lamports = Rent::default().minimum_balance(space); |
| 81 | + |
| 82 | + let instruction = store(&config_pubkey, true, keys.clone(), &Self::default()); |
| 83 | + |
| 84 | + let accounts = vec![( |
| 85 | + config_pubkey, |
| 86 | + AccountSharedData::new_data( |
| 87 | + lamports, |
| 88 | + &Self::default_account_state(keys), |
| 89 | + &solana_config_program::id(), |
| 90 | + ) |
| 91 | + .unwrap(), |
| 92 | + )]; |
| 93 | + |
| 94 | + BenchContext { |
| 95 | + label: format!("{}_store_{}_keys", Self::BENCH_ID, keys_len), |
| 96 | + instruction, |
| 97 | + accounts, |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/// A small config, which just stores 8 bytes. |
| 103 | +#[derive(Debug, Default, PartialEq, Serialize)] |
| 104 | +pub struct ConfigSmall { |
| 105 | + pub item: u64, |
| 106 | +} |
| 107 | + |
| 108 | +impl ConfigState for ConfigSmall { |
| 109 | + fn max_space() -> u64 { |
| 110 | + bincode::serialized_size(&Self::default()).unwrap() |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +impl BenchSetup for ConfigSmall { |
| 115 | + const BENCH_ID: &'static str = "config_small"; |
| 116 | +} |
| 117 | + |
| 118 | +/// A medium config, which stores 1024 bytes. |
| 119 | +#[derive(Debug, Default, PartialEq, Serialize)] |
| 120 | +pub struct ConfigMedium { |
| 121 | + pub hashes: [Hash; 32], // 32 x 32 = 1024 bytes |
| 122 | +} |
| 123 | + |
| 124 | +impl ConfigState for ConfigMedium { |
| 125 | + fn max_space() -> u64 { |
| 126 | + bincode::serialized_size(&Self::default()).unwrap() |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +impl BenchSetup for ConfigMedium { |
| 131 | + const BENCH_ID: &'static str = "config_medium"; |
| 132 | +} |
| 133 | + |
| 134 | +/// A large config, which stores 32_768 bytes. |
| 135 | +#[derive(Debug, Default, PartialEq, Serialize)] |
| 136 | +pub struct ConfigLarge { |
| 137 | + pub hashes: [[Hash; 32]; 32], // 32 x 32 x 32 = 32_768 bytes |
| 138 | +} |
| 139 | + |
| 140 | +impl ConfigState for ConfigLarge { |
| 141 | + fn max_space() -> u64 { |
| 142 | + bincode::serialized_size(&Self::default()).unwrap() |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +impl BenchSetup for ConfigLarge { |
| 147 | + const BENCH_ID: &'static str = "config_large"; |
| 148 | +} |
0 commit comments