extern crate env_logger;
extern crate time;
extern crate url;
extern crate solana_ws as ws;
use ws::{Builder, CloseCode, Handler, Handshake, Message, Result, Sender, Settings};
const CONNECTIONS: usize = 10_000; const MESSAGES: u32 = 10;
static MESSAGE: &'static str = "TEST TEST TEST TEST TEST TEST TEST TEST";
fn main() {
env_logger::init();
let url = url::Url::parse("ws://127.0.0.1:3012").unwrap();
struct Connection {
out: Sender,
count: u32,
time: u64,
total: u64,
}
impl Handler for Connection {
fn on_open(&mut self, _: Handshake) -> Result<()> {
self.out.send(MESSAGE)?;
self.count += 1;
self.time = time::precise_time_ns();
Ok(())
}
fn on_message(&mut self, msg: Message) -> Result<()> {
assert_eq!(msg.as_text().unwrap(), MESSAGE);
if self.count > MESSAGES {
self.out.close(CloseCode::Normal)?;
} else {
self.out.send(MESSAGE)?;
let time = time::precise_time_ns();
self.total += time - self.time;
self.count += 1;
self.time = time;
}
Ok(())
}
}
let mut ws = Builder::new()
.with_settings(Settings {
max_connections: CONNECTIONS,
..Settings::default()
})
.build(|out| Connection {
out,
count: 0,
time: 0,
total: 0,
})
.unwrap();
for _ in 0..CONNECTIONS {
ws.connect(url.clone()).unwrap();
}
let start = time::precise_time_ns();
ws.run().unwrap();
println!(
"Total time. {}",
(time::precise_time_ns() - start) / 1_000_000
)
}