|
| 1 | +#![allow(dead_code)] |
| 2 | + |
| 3 | +use std::{env, path::PathBuf}; |
| 4 | + |
| 5 | +use hpsvm::HPSVM; |
| 6 | +#[cfg(feature = "register-tracing")] |
| 7 | +use hpsvm::register_tracing::TraceMetricsCollector; |
| 8 | +use solana_account::Account; |
| 9 | +use solana_address::Address; |
| 10 | +use solana_instruction::{Instruction, account_meta::AccountMeta}; |
| 11 | +use solana_keypair::Keypair; |
| 12 | +use solana_message::Message; |
| 13 | +use solana_transaction::Transaction; |
| 14 | + |
| 15 | +const COUNTER_PROGRAM_RELATIVE_PATH: &str = "test_programs/target/deploy/counter.so"; |
| 16 | +const HOTPATH_ENV_VAR: &str = "HPSVM_HOTPATH"; |
| 17 | +const HOTPATH_LIMIT_ENV_VAR: &str = "HPSVM_HOTPATH_LIMIT"; |
| 18 | +const TRACE_METRICS_ENV_VAR: &str = "HPSVM_TRACE_METRICS"; |
| 19 | + |
| 20 | +pub struct HotpathGuard { |
| 21 | + #[cfg(feature = "hotpath")] |
| 22 | + _inner: Option<hotpath::HotpathGuard>, |
| 23 | +} |
| 24 | + |
| 25 | +impl HotpathGuard { |
| 26 | + pub fn new(name: &'static str) -> Self { |
| 27 | + #[cfg(feature = "hotpath")] |
| 28 | + { |
| 29 | + if env::var_os(HOTPATH_ENV_VAR).is_none() { |
| 30 | + return Self { _inner: None }; |
| 31 | + } |
| 32 | + |
| 33 | + let limit = env::var(HOTPATH_LIMIT_ENV_VAR) |
| 34 | + .ok() |
| 35 | + .and_then(|value| value.parse::<usize>().ok()) |
| 36 | + .unwrap_or(20); |
| 37 | + let output_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 38 | + .join("../../target/hotpath") |
| 39 | + .join(format!("{name}.json")); |
| 40 | + if let Some(parent) = output_path.parent() { |
| 41 | + std::fs::create_dir_all(parent) |
| 42 | + .expect("hotpath benchmark output directory should be creatable"); |
| 43 | + } |
| 44 | + let guard = hotpath::HotpathGuardBuilder::new(name) |
| 45 | + .percentiles(&[50.0, 95.0, 99.0]) |
| 46 | + .format(hotpath::Format::JsonPretty) |
| 47 | + .output_path(&output_path) |
| 48 | + .limit(limit) |
| 49 | + .build(); |
| 50 | + |
| 51 | + return Self { _inner: Some(guard) }; |
| 52 | + } |
| 53 | + |
| 54 | + #[cfg(not(feature = "hotpath"))] |
| 55 | + { |
| 56 | + let _ = name; |
| 57 | + Self {} |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +pub struct TraceMetricsGuard { |
| 63 | + #[cfg(feature = "register-tracing")] |
| 64 | + collector: Option<TraceMetricsCollector>, |
| 65 | + #[cfg(feature = "register-tracing")] |
| 66 | + output_path: Option<PathBuf>, |
| 67 | +} |
| 68 | + |
| 69 | +impl TraceMetricsGuard { |
| 70 | + pub fn new(name: &'static str) -> Self { |
| 71 | + #[cfg(feature = "register-tracing")] |
| 72 | + { |
| 73 | + if env::var_os(TRACE_METRICS_ENV_VAR).is_none() { |
| 74 | + return Self { collector: None, output_path: None }; |
| 75 | + } |
| 76 | + |
| 77 | + let output_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 78 | + .join("../../target/hotpath") |
| 79 | + .join(format!("{name}.trace.json")); |
| 80 | + if let Some(parent) = output_path.parent() { |
| 81 | + std::fs::create_dir_all(parent) |
| 82 | + .expect("trace metrics output directory should be creatable"); |
| 83 | + } |
| 84 | + |
| 85 | + return Self { |
| 86 | + collector: Some(TraceMetricsCollector::default()), |
| 87 | + output_path: Some(output_path), |
| 88 | + }; |
| 89 | + } |
| 90 | + |
| 91 | + #[cfg(not(feature = "register-tracing"))] |
| 92 | + { |
| 93 | + let _ = name; |
| 94 | + Self {} |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + pub fn install(&self, svm: &mut HPSVM) { |
| 99 | + #[cfg(feature = "register-tracing")] |
| 100 | + if let Some(collector) = &self.collector { |
| 101 | + svm.set_invocation_inspect_callback(collector.clone()); |
| 102 | + } |
| 103 | + |
| 104 | + #[cfg(not(feature = "register-tracing"))] |
| 105 | + let _ = svm; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl Drop for TraceMetricsGuard { |
| 110 | + fn drop(&mut self) { |
| 111 | + #[cfg(feature = "register-tracing")] |
| 112 | + if let (Some(collector), Some(output_path)) = (&self.collector, &self.output_path) { |
| 113 | + collector |
| 114 | + .write_json_path(output_path) |
| 115 | + .expect("trace metrics output should be writable"); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +pub fn new_benchmark_vm() -> HPSVM { |
| 121 | + #[cfg(feature = "register-tracing")] |
| 122 | + let mut svm = if env::var_os(TRACE_METRICS_ENV_VAR).is_some() { |
| 123 | + HPSVM::new_debuggable(true) |
| 124 | + } else { |
| 125 | + HPSVM::new() |
| 126 | + }; |
| 127 | + |
| 128 | + #[cfg(not(feature = "register-tracing"))] |
| 129 | + let mut svm = HPSVM::new(); |
| 130 | + |
| 131 | + svm.set_blockhash_check(false); |
| 132 | + svm.set_sigverify(false); |
| 133 | + svm.set_transaction_history(0); |
| 134 | + svm |
| 135 | +} |
| 136 | + |
| 137 | +pub fn counter_program_path() -> PathBuf { |
| 138 | + let mut so_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); |
| 139 | + so_path.push(COUNTER_PROGRAM_RELATIVE_PATH); |
| 140 | + so_path |
| 141 | +} |
| 142 | + |
| 143 | +pub fn read_counter_program() -> Vec<u8> { |
| 144 | + std::fs::read(counter_program_path()).expect("counter program bytes should be available") |
| 145 | +} |
| 146 | + |
| 147 | +pub fn make_counter_tx( |
| 148 | + program_id: Address, |
| 149 | + counter_address: Address, |
| 150 | + payer_pk: &Address, |
| 151 | + blockhash: solana_hash::Hash, |
| 152 | + payer_kp: &Keypair, |
| 153 | + deduper: u8, |
| 154 | +) -> Transaction { |
| 155 | + let msg = Message::new_with_blockhash( |
| 156 | + &[Instruction { |
| 157 | + program_id, |
| 158 | + accounts: vec![AccountMeta::new(counter_address, false)], |
| 159 | + data: vec![0, deduper], |
| 160 | + }], |
| 161 | + Some(payer_pk), |
| 162 | + &blockhash, |
| 163 | + ); |
| 164 | + Transaction::new(&[payer_kp], msg, blockhash) |
| 165 | +} |
| 166 | + |
| 167 | +pub fn counter_account(program_id: Address) -> Account { |
| 168 | + Account { |
| 169 | + lamports: 5, |
| 170 | + data: vec![0_u8; std::mem::size_of::<u32>()], |
| 171 | + owner: program_id, |
| 172 | + ..Default::default() |
| 173 | + } |
| 174 | +} |
0 commit comments