forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CLI command for measuring TpuClient slot estimation
- Loading branch information
Showing
10 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "solana-tpu-client-test" | ||
description = "Solana TPU Client Test" | ||
publish = false | ||
version = { workspace = true } | ||
authors = { workspace = true } | ||
repository = { workspace = true } | ||
homepage = { workspace = true } | ||
license = { workspace = true } | ||
edition = { workspace = true } | ||
|
||
[dependencies] | ||
solana-client = { workspace = true } | ||
solana-sdk = { workspace = true } | ||
solana-logger = { workspace = true } | ||
solana-test-validator = { workspace = true } | ||
clap = { version = "3.1.5", features = ["cargo", "derive"] } | ||
tokio = { workspace = true, features = ["full"] } | ||
log = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use clap::{crate_description, crate_name, crate_version, Parser, Subcommand}; | ||
|
||
#[derive(Parser, Debug)] | ||
#[clap(name = crate_name!(), | ||
version = crate_version!(), | ||
about = crate_description!(), | ||
rename_all = "kebab-case" | ||
)] | ||
struct TpuClientTestArgs { | ||
#[clap(subcommand)] | ||
pub mode: Mode, | ||
} | ||
|
||
#[derive(Subcommand, Debug)] | ||
enum Mode { | ||
/// Test the TpuClient's slot estimation accuracy. | ||
SlotEstimationAccuracy { | ||
#[clap( | ||
short, | ||
long, | ||
default_value_t = 0.15, | ||
help = "Accuracy threshold for slot estimation" | ||
)] | ||
accuracy_threshold: f64, | ||
#[clap(short, long, default_value_t = 100, help = "Number of samples to take")] | ||
num_samples: usize, | ||
}, | ||
} | ||
|
||
mod modes; | ||
use modes::*; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
solana_logger::setup_with("solana_tpu_client_test=info"); | ||
|
||
let args = TpuClientTestArgs::parse(); | ||
|
||
match args.mode { | ||
Mode::SlotEstimationAccuracy { | ||
accuracy_threshold, | ||
num_samples, | ||
} => slot_estimation_accuracy::run(accuracy_threshold, num_samples).await, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod slot_estimation_accuracy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use { | ||
log::info, | ||
solana_client::{connection_cache::Protocol, nonblocking::tpu_client::LeaderTpuService}, | ||
solana_sdk::clock::DEFAULT_MS_PER_SLOT, | ||
solana_test_validator::{TestValidator, TestValidatorGenesis}, | ||
std::{ | ||
sync::{ | ||
atomic::{AtomicBool, Ordering}, | ||
Arc, | ||
}, | ||
time::Duration, | ||
}, | ||
tokio::time::sleep, | ||
}; | ||
|
||
struct SlotEstimationAccuracy { | ||
leader_tpu_service: LeaderTpuService, | ||
validator: TestValidator, | ||
exit: Arc<AtomicBool>, | ||
} | ||
|
||
impl SlotEstimationAccuracy { | ||
async fn new() -> Self { | ||
let validator = TestValidatorGenesis::default().start_async().await.0; | ||
let rpc_client = Arc::new(validator.get_async_rpc_client()); | ||
let exit = Arc::new(AtomicBool::new(false)); | ||
let leader_tpu_service = LeaderTpuService::new( | ||
rpc_client, | ||
&validator.rpc_pubsub_url(), | ||
Protocol::QUIC, | ||
exit.clone(), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
Self { | ||
leader_tpu_service, | ||
validator, | ||
exit, | ||
} | ||
} | ||
} | ||
|
||
pub(crate) async fn run(accuracy_threshold: f64, num_samples: usize) { | ||
info!("bootstrapping test validator"); | ||
|
||
let SlotEstimationAccuracy { | ||
mut leader_tpu_service, | ||
validator, | ||
exit, | ||
} = SlotEstimationAccuracy::new().await; | ||
|
||
let sleep_time = Duration::from_millis(DEFAULT_MS_PER_SLOT); | ||
let mut result_pairs = vec![]; | ||
|
||
let mut actual = validator.current_slot(); | ||
while (actual as usize) < num_samples { | ||
actual = validator.current_slot(); | ||
let estimated = leader_tpu_service.estimated_current_slot(); | ||
result_pairs.push((estimated, actual)); | ||
info!( | ||
"estimated: {}, actual: {} {}", | ||
estimated, | ||
actual, | ||
if estimated == actual { "✅" } else { "❌" } | ||
); | ||
|
||
sleep(sleep_time).await; | ||
} | ||
|
||
let failure_rate = | ||
result_pairs.iter().filter(|(a, b)| a != b).count() as f64 / result_pairs.len() as f64; | ||
|
||
info!( | ||
"failure rate: {failure_rate} <= {accuracy_threshold} {}", | ||
if failure_rate <= accuracy_threshold { | ||
"✅" | ||
} else { | ||
"❌" | ||
} | ||
); | ||
|
||
info!("cleaning up and exiting..."); | ||
|
||
exit.store(true, Ordering::Relaxed); | ||
leader_tpu_service.join().await; | ||
} |