solana/src/bin/client-demo.rs

147 lines
4.7 KiB
Rust
Raw Normal View History

extern crate futures;
extern crate getopts;
2018-04-21 06:12:57 -07:00
extern crate isatty;
extern crate rayon;
extern crate serde_json;
2018-03-27 15:24:05 -07:00
extern crate solana;
extern crate untrusted;
2018-02-28 09:07:54 -08:00
use futures::Future;
2018-04-11 19:24:14 -07:00
use getopts::Options;
2018-04-21 06:12:57 -07:00
use isatty::stdin_isatty;
2018-04-02 20:15:21 -07:00
use rayon::prelude::*;
2018-03-27 15:24:05 -07:00
use solana::accountant_stub::AccountantStub;
use solana::mint::MintDemo;
2018-03-27 15:24:05 -07:00
use solana::signature::{KeyPair, KeyPairUtil};
use solana::transaction::Transaction;
2018-04-11 19:24:14 -07:00
use std::env;
2018-04-21 06:12:57 -07:00
use std::io::{stdin, Read};
use std::net::UdpSocket;
2018-04-19 07:06:19 -07:00
use std::process::exit;
2018-04-17 15:41:58 -07:00
use std::thread::sleep;
use std::time::{Duration, Instant};
use untrusted::Input;
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));
}
2018-02-28 09:07:54 -08:00
fn main() {
let mut threads = 4usize;
let mut addr: String = "127.0.0.1:8000".to_string();
let mut send_addr: String = "127.0.0.1:8001".to_string();
let mut opts = Options::new();
opts.optopt("s", "", "server address", "host:port");
opts.optopt("c", "", "client address", "host:port");
opts.optopt("t", "", "number of threads", "4");
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("s") {
addr = matches.opt_str("s").unwrap();
}
if matches.opt_present("c") {
send_addr = matches.opt_str("c").unwrap();
}
if matches.opt_present("t") {
threads = matches.opt_str("t").unwrap().parse().expect("integer");
}
2018-04-21 06:12:57 -07:00
if stdin_isatty() {
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);
}
let demo: MintDemo = serde_json::from_reader(stdin()).unwrap_or_else(|e| {
2018-04-19 07:55:47 -07:00
eprintln!("failed to parse json: {}", e);
exit(1);
});
2018-02-28 09:07:54 -08:00
let socket = UdpSocket::bind(&send_addr).unwrap();
let mut acc = AccountantStub::new(&addr, socket);
println!("Get last id");
let last_id = acc.get_last_id().wait().unwrap();
let txs = demo.users.len() / 2;
let keypairs: Vec<_> = demo.users
.into_par_iter()
.map(|(pkcs8, _)| KeyPair::from_pkcs8(Input::from(&pkcs8)).unwrap())
.collect();
let keypair_pairs: Vec<_> = keypairs.chunks(2).collect();
println!("Signing transactions...");
let now = Instant::now();
let transactions: Vec<_> = keypair_pairs
.into_par_iter()
.map(|chunk| Transaction::new(&chunk[0], chunk[1].pubkey(), 1, last_id))
.collect();
let duration = now.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!(
2018-03-03 10:52:46 -08:00
"Done. {} thousand signatures per second, {}us per signature",
bsps * 1_000_000_f64,
nsps / 1_000_f64
);
2018-04-17 15:41:58 -07:00
let initial_tx_count = acc.transaction_count();
println!("Transfering {} transactions in {} batches", txs, threads);
let now = Instant::now();
let sz = transactions.len() / threads;
let chunks: Vec<_> = transactions.chunks(sz).collect();
chunks.into_par_iter().for_each(|trs| {
println!("Transferring 1 unit {} times...", trs.len());
let send_addr = "0.0.0.0:0";
let socket = UdpSocket::bind(send_addr).unwrap();
let acc = AccountantStub::new(&addr, socket);
for tr in trs {
acc.transfer_signed(tr.clone()).unwrap();
}
});
2018-04-17 15:41:58 -07:00
let mut tx_count = acc.transaction_count();
println!("tx count {}", tx_count);
let mut prev_tx_count = tx_count + 1;
println!("Waiting for the server to go idle...",);
while tx_count != prev_tx_count {
sleep(Duration::from_millis(20));
prev_tx_count = tx_count;
tx_count = acc.transaction_count();
}
let txs = tx_count - initial_tx_count;
println!("Sent transactions {}", txs);
2018-02-28 09:07:54 -08:00
let duration = now.elapsed();
2018-03-22 13:15:29 -07:00
let ns = duration.as_secs() * 1_000_000_000 + u64::from(duration.subsec_nanos());
let tps = (txs * 1_000_000_000) as f64 / ns as f64;
println!("Done. If no packets dropped, {} tps", tps);
2018-02-28 09:07:54 -08:00
}