solana/src/bin/client-demo.rs

384 lines
11 KiB
Rust
Raw Normal View History

2018-06-13 18:42:30 -07:00
extern crate atty;
2018-05-25 10:53:57 -07:00
extern crate env_logger;
extern crate getopts;
extern crate rayon;
extern crate serde_json;
2018-03-27 15:24:05 -07:00
extern crate solana;
2018-02-28 09:07:54 -08:00
2018-06-13 15:07:36 -07:00
use atty::{is, Stream};
2018-06-13 18:42:30 -07:00
use getopts::Options;
2018-04-02 20:15:21 -07:00
use rayon::prelude::*;
use solana::crdt::{Crdt, ReplicatedData};
use solana::hash::Hash;
use solana::mint::Mint;
use solana::nat::udp_public_bind;
2018-06-07 15:06:32 -07:00
use solana::ncp::Ncp;
use solana::signature::{GenKeys, KeyPair, KeyPairUtil};
use solana::streamer::default_window;
2018-05-08 17:59:01 -07:00
use solana::thin_client::ThinClient;
use solana::timing::{duration_as_ms, duration_as_s};
2018-03-27 15:24:05 -07:00
use solana::transaction::Transaction;
2018-04-11 19:24:14 -07:00
use std::env;
use std::fs::File;
2018-04-21 06:12:57 -07:00
use std::io::{stdin, Read};
2018-05-31 15:48:03 -07:00
use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket};
2018-04-19 07:06:19 -07:00
use std::process::exit;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, RwLock};
use std::thread::sleep;
use std::thread::Builder;
use std::thread::JoinHandle;
use std::time::Duration;
use std::time::Instant;
fn print_usage(program: &str, opts: Options) {
let mut brief = format!("Usage: cat <mint.json> | {} [options]\n\n", program);
brief += " Solana client demo creates a number of transactions and\n";
brief += " sends them to a target node.";
brief += " Takes json formatted mint file to stdin.";
print!("{}", opts.usage(&brief));
}
fn sample_tx_count(
exit: Arc<AtomicBool>,
maxes: Arc<RwLock<Vec<(f64, u64)>>>,
first_count: u64,
v: ReplicatedData,
sample_period: u64,
) {
let mut client = mk_client(&v);
let mut now = Instant::now();
let mut initial_tx_count = client.transaction_count();
let mut max_tps = 0.0;
let mut total;
loop {
let tx_count = client.transaction_count();
let duration = now.elapsed();
now = Instant::now();
let sample = tx_count - initial_tx_count;
initial_tx_count = tx_count;
println!("{}: Transactions processed {}", v.transactions_addr, sample);
let ns = duration.as_secs() * 1_000_000_000 + u64::from(duration.subsec_nanos());
let tps = (sample * 1_000_000_000) as f64 / ns as f64;
if tps > max_tps {
max_tps = tps;
}
println!("{}: {:.2} tps", v.transactions_addr, tps);
total = tx_count - first_count;
println!(
"{}: Total Transactions processed {}",
v.transactions_addr, total
);
sleep(Duration::new(sample_period, 0));
if exit.load(Ordering::Relaxed) {
println!("exiting validator thread");
maxes.write().unwrap().push((max_tps, total));
break;
}
}
}
fn generate_and_send_txs(
client: &mut ThinClient,
tx_clients: &Vec<ThinClient>,
mint: &Mint,
keypairs: &Vec<KeyPair>,
leader: &ReplicatedData,
txs: i64,
last_id: &mut Hash,
threads: usize,
) {
println!("Signing transactions... {}", keypairs.len(),);
let signing_start = Instant::now();
let transactions: Vec<_> = keypairs
.par_iter()
.map(|keypair| Transaction::new(&mint.keypair(), keypair.pubkey(), 1, *last_id))
.collect();
let duration = signing_start.elapsed();
let ns = duration.as_secs() * 1_000_000_000 + u64::from(duration.subsec_nanos());
let bsps = txs as f64 / ns as f64;
let nsps = ns as f64 / txs as f64;
println!(
"Done. {:.2} thousand signatures per second, {:.2} us per signature, {} ms total time",
bsps * 1_000_000_f64,
nsps / 1_000_f64,
duration_as_ms(&duration),
);
println!("Transfering {} transactions in {} batches", txs, threads);
let transfer_start = Instant::now();
let sz = transactions.len() / threads;
let chunks: Vec<_> = transactions.chunks(sz).collect();
chunks
.into_par_iter()
.zip(tx_clients)
.for_each(|(txs, client)| {
println!(
"Transferring 1 unit {} times... to {:?}",
txs.len(),
leader.transactions_addr
);
for tx in txs {
client.transfer_signed(tx.clone()).unwrap();
}
});
println!(
"Transfer done. {:?} ms {} tps",
duration_as_ms(&transfer_start.elapsed()),
txs as f32 / (duration_as_s(&transfer_start.elapsed()))
);
loop {
let new_id = client.get_last_id();
if *last_id != new_id {
*last_id = new_id;
break;
}
sleep(Duration::from_millis(100));
}
}
2018-02-28 09:07:54 -08:00
fn main() {
env_logger::init();
let mut threads = 4usize;
2018-05-31 16:48:05 -07:00
let mut num_nodes = 1usize;
let mut time_sec = 60;
let mut opts = Options::new();
opts.optopt("l", "", "leader", "leader.json");
opts.optopt("t", "", "number of threads", &format!("{}", threads));
opts.optopt(
"s",
"",
"send transactions for this many seconds",
&format!("{}", time_sec),
);
opts.optopt(
"n",
"",
"number of nodes to converge to",
&format!("{}", num_nodes),
);
opts.optflag("h", "help", "print help");
let args: Vec<String> = env::args().collect();
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
2018-04-19 07:06:19 -07:00
Err(e) => {
eprintln!("{}", e);
exit(1);
}
};
if matches.opt_present("h") {
let program = args[0].clone();
print_usage(&program, opts);
return;
}
if matches.opt_present("t") {
threads = matches.opt_str("t").unwrap().parse().expect("integer");
}
if matches.opt_present("n") {
num_nodes = matches.opt_str("n").unwrap().parse().expect("integer");
}
if matches.opt_present("s") {
time_sec = matches.opt_str("s").unwrap().parse().expect("integer");
}
2018-04-21 06:12:57 -07:00
2018-05-31 15:48:03 -07:00
let leader = if matches.opt_present("l") {
read_leader(matches.opt_str("l").unwrap())
} else {
let server_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8000);
ReplicatedData::new_leader(&server_addr)
};
let signal = Arc::new(AtomicBool::new(false));
let mut c_threads = vec![];
let validators = converge(&leader, signal.clone(), num_nodes, &mut c_threads);
assert_eq!(validators.len(), num_nodes);
2018-06-13 15:07:36 -07:00
if is(Stream::Stdin) {
2018-04-21 06:12:57 -07:00
eprintln!("nothing found on stdin, expected a json file");
exit(1);
}
let mut buffer = String::new();
let num_bytes = stdin().read_to_string(&mut buffer).unwrap();
if num_bytes == 0 {
eprintln!("empty file on stdin, expected a json file");
exit(1);
}
println!("Parsing stdin...");
let mint: Mint = serde_json::from_str(&buffer).unwrap_or_else(|e| {
2018-04-19 07:55:47 -07:00
eprintln!("failed to parse json: {}", e);
exit(1);
});
let mut client = mk_client(&leader);
println!("Get last ID...");
let mut last_id = client.get_last_id();
println!("Got last ID {:?}", last_id);
2018-06-11 13:04:51 -07:00
let mut seed = [0u8; 32];
seed.copy_from_slice(&mint.keypair().public_key_bytes()[..32]);
2018-06-11 13:04:51 -07:00
let rnd = GenKeys::new(seed);
println!("Creating keypairs...");
2018-06-27 13:32:18 -07:00
let txs = 500_000;
let keypairs = rnd.gen_n_keypairs(txs);
let first_count = client.transaction_count();
println!("initial count {}", first_count);
2018-04-17 15:41:58 -07:00
println!("Sampling tps every second...",);
// Setup a thread per validator to sample every period
// collect the max transaction rate and total tx count seen
let maxes = Arc::new(RwLock::new(Vec::new()));
let sample_period = 1; // in seconds
let v_threads: Vec<_> = validators
.into_iter()
.map(|v| {
let exit = signal.clone();
let maxes = maxes.clone();
Builder::new()
.name("solana-client-sample".to_string())
.spawn(move || {
sample_tx_count(exit, maxes, first_count, v, sample_period);
})
.unwrap()
})
.collect();
let clients = (0..threads).map(|_| mk_client(&leader)).collect();
// generate and send transactions for the specified duration
let time = Duration::new(time_sec, 0);
let now = Instant::now();
while now.elapsed() < time {
generate_and_send_txs(
&mut client,
&clients,
&mint,
&keypairs,
&leader,
txs,
&mut last_id,
threads,
);
}
// Stop the sampling threads so it will collect the stats
signal.store(true, Ordering::Relaxed);
for t in v_threads {
t.join().unwrap();
}
// Compute/report stats
let mut max_of_maxes = 0.0;
let mut total_txs = 0;
for (max, txs) in maxes.read().unwrap().iter() {
if *max > max_of_maxes {
max_of_maxes = *max;
}
total_txs += *txs;
}
println!(
"\nHighest TPS: {:.2} sampling period {}s total transactions: {} clients: {}",
max_of_maxes,
sample_period,
total_txs,
maxes.read().unwrap().len()
);
// join the crdt client threads
for t in c_threads {
t.join().unwrap();
}
}
fn mk_client(r: &ReplicatedData) -> ThinClient {
2018-07-02 13:43:33 -07:00
let requests_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
let transactions_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
2018-07-02 13:43:33 -07:00
requests_socket
.set_read_timeout(Some(Duration::new(1, 0)))
.unwrap();
ThinClient::new(
r.requests_addr,
2018-07-02 13:43:33 -07:00
requests_socket,
r.transactions_addr,
2018-07-02 13:43:33 -07:00
transactions_socket,
)
}
fn spy_node() -> (ReplicatedData, UdpSocket) {
let gossip_socket_pair = udp_public_bind("gossip");
let pubkey = KeyPair::new().pubkey();
let daddr = "0.0.0.0:0".parse().unwrap();
let node = ReplicatedData::new(
pubkey,
//gossip.local_addr().unwrap(),
gossip_socket_pair.addr,
daddr,
daddr,
daddr,
daddr,
);
(node, gossip_socket_pair.receiver)
}
fn converge(
leader: &ReplicatedData,
exit: Arc<AtomicBool>,
num_nodes: usize,
threads: &mut Vec<JoinHandle<()>>,
) -> Vec<ReplicatedData> {
//lets spy on the network
let daddr = "0.0.0.0:0".parse().unwrap();
let (spy, spy_gossip) = spy_node();
let mut spy_crdt = Crdt::new(spy);
spy_crdt.insert(&leader);
spy_crdt.set_leader(leader.id);
let spy_ref = Arc::new(RwLock::new(spy_crdt));
let window = default_window();
let gossip_send_socket = UdpSocket::bind("0.0.0.0:0").expect("bind 0");
2018-06-07 15:06:32 -07:00
let ncp = Ncp::new(
spy_ref.clone(),
window.clone(),
spy_gossip,
gossip_send_socket,
exit.clone(),
).expect("DataReplicator::new");
let mut rv = vec![];
//wait for the network to converge, 30 seconds should be plenty
for _ in 0..30 {
let v: Vec<ReplicatedData> = spy_ref
.read()
.unwrap()
.table
.values()
.into_iter()
.filter(|x| x.requests_addr != daddr)
.cloned()
.collect();
if v.len() >= num_nodes {
println!("CONVERGED!");
rv.extend(v.into_iter());
break;
}
sleep(Duration::new(1, 0));
2018-04-17 15:41:58 -07:00
}
2018-06-07 15:06:32 -07:00
threads.extend(ncp.thread_hdls.into_iter());
rv
}
fn read_leader(path: String) -> ReplicatedData {
2018-06-15 15:29:43 -07:00
let file = File::open(path.clone()).expect(&format!("file not found: {}", path));
serde_json::from_reader(file).expect(&format!("failed to parse {}", path))
2018-02-28 09:07:54 -08:00
}