2020-05-27 15:58:57 +00:00
|
|
|
///run with
|
|
|
|
/// ```bash
|
|
|
|
/// (cd network/examples/network-speed && RUST_BACKTRACE=1 cargo run --profile=debuginfo -Z unstable-options -- --trace=error --protocol=tcp --mode=server)
|
|
|
|
/// (cd network/examples/network-speed && RUST_BACKTRACE=1 cargo run --profile=debuginfo -Z unstable-options -- --trace=error --protocol=tcp --mode=client)
|
|
|
|
/// ```
|
2022-04-02 21:29:22 +00:00
|
|
|
use clap::{Arg, Command};
|
2021-02-10 10:37:42 +00:00
|
|
|
use prometheus::Registry;
|
|
|
|
use prometheus_hyper::Server;
|
2020-04-08 14:26:42 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::{
|
2021-02-10 10:37:42 +00:00
|
|
|
net::SocketAddr,
|
2021-01-15 13:04:32 +00:00
|
|
|
sync::Arc,
|
2020-04-08 14:26:42 +00:00
|
|
|
thread,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
2021-01-15 13:04:32 +00:00
|
|
|
use tokio::runtime::Runtime;
|
2020-04-08 14:26:42 +00:00
|
|
|
use tracing::*;
|
|
|
|
use tracing_subscriber::EnvFilter;
|
2021-04-15 08:16:42 +00:00
|
|
|
use veloren_network::{ConnectAddr, ListenAddr, Message, Network, Pid, Promises};
|
2020-04-08 14:26:42 +00:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
enum Msg {
|
|
|
|
Ping { id: u64, data: Vec<u8> },
|
|
|
|
Pong { id: u64, data: Vec<u8> },
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This utility tests the speed of veloren network by creating a client that
|
|
|
|
/// opens a stream and pipes as many messages through it as possible.
|
|
|
|
fn main() {
|
2022-04-02 13:44:07 +00:00
|
|
|
let matches = Command::new("Veloren Speed Test Utility")
|
2020-04-08 14:26:42 +00:00
|
|
|
.version("0.1.0")
|
|
|
|
.author("Marcel Märtens <marcel.cochem@googlemail.com>")
|
|
|
|
.about("Runs speedtests regarding different parameter to benchmark veloren-network")
|
|
|
|
.arg(
|
2022-04-02 13:44:07 +00:00
|
|
|
Arg::new("mode")
|
|
|
|
.short('m')
|
2020-04-08 14:26:42 +00:00
|
|
|
.long("mode")
|
|
|
|
.takes_value(true)
|
2022-09-08 19:51:02 +00:00
|
|
|
.possible_values(["server", "client", "both"])
|
2020-04-08 14:26:42 +00:00
|
|
|
.default_value("both")
|
|
|
|
.help(
|
|
|
|
"choose whether you want to start the server or client or both needed for \
|
|
|
|
this program",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.arg(
|
2022-04-02 13:44:07 +00:00
|
|
|
Arg::new("port")
|
|
|
|
.short('p')
|
2020-04-08 14:26:42 +00:00
|
|
|
.long("port")
|
|
|
|
.takes_value(true)
|
|
|
|
.default_value("52000")
|
|
|
|
.help("port to listen on"),
|
|
|
|
)
|
|
|
|
.arg(
|
2022-04-02 13:44:07 +00:00
|
|
|
Arg::new("ip")
|
2020-04-08 14:26:42 +00:00
|
|
|
.long("ip")
|
|
|
|
.takes_value(true)
|
|
|
|
.default_value("127.0.0.1")
|
|
|
|
.help("ip to listen and connect to"),
|
|
|
|
)
|
|
|
|
.arg(
|
2022-04-02 13:44:07 +00:00
|
|
|
Arg::new("protocol")
|
2020-04-08 14:26:42 +00:00
|
|
|
.long("protocol")
|
|
|
|
.takes_value(true)
|
|
|
|
.default_value("tcp")
|
2022-09-08 19:51:02 +00:00
|
|
|
.possible_values(["tcp", "udp", "mpsc"])
|
2020-04-08 14:26:42 +00:00
|
|
|
.help(
|
|
|
|
"underlying protocol used for this test, mpsc can only combined with mode=both",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.arg(
|
2022-04-02 13:44:07 +00:00
|
|
|
Arg::new("trace")
|
|
|
|
.short('t')
|
2020-04-08 14:26:42 +00:00
|
|
|
.long("trace")
|
|
|
|
.takes_value(true)
|
|
|
|
.default_value("warn")
|
2022-09-08 19:51:02 +00:00
|
|
|
.possible_values(["trace", "debug", "info", "warn", "error"])
|
2020-04-08 14:26:42 +00:00
|
|
|
.help("set trace level, not this has a performance impact!"),
|
|
|
|
)
|
|
|
|
.get_matches();
|
|
|
|
|
|
|
|
let trace = matches.value_of("trace").unwrap();
|
2020-05-15 12:29:17 +00:00
|
|
|
let filter = EnvFilter::from_default_env()
|
|
|
|
.add_directive(trace.parse().unwrap())
|
|
|
|
.add_directive("network_speed=debug".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::participant=trace".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::protocol=trace".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::scheduler=trace".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::api=trace".parse().unwrap())
|
|
|
|
/*
|
2020-04-24 10:56:04 +00:00
|
|
|
.add_directive("veloren_network::participant=debug".parse().unwrap()).add_directive("veloren_network::api=debug".parse().unwrap())*/;
|
2020-04-08 14:26:42 +00:00
|
|
|
tracing_subscriber::FmtSubscriber::builder()
|
2020-04-24 10:56:04 +00:00
|
|
|
.with_max_level(Level::ERROR)
|
2020-04-08 14:26:42 +00:00
|
|
|
.with_env_filter(filter)
|
|
|
|
.init();
|
|
|
|
|
|
|
|
let port: u16 = matches.value_of("port").unwrap().parse().unwrap();
|
|
|
|
let ip: &str = matches.value_of("ip").unwrap();
|
2021-04-15 08:16:42 +00:00
|
|
|
let addresses = match matches.value_of("protocol") {
|
|
|
|
Some("tcp") => (
|
|
|
|
ListenAddr::Tcp(format!("{}:{}", ip, port).parse().unwrap()),
|
|
|
|
ConnectAddr::Tcp(format!("{}:{}", ip, port).parse().unwrap()),
|
|
|
|
),
|
|
|
|
Some("udp") => (
|
|
|
|
ListenAddr::Udp(format!("{}:{}", ip, port).parse().unwrap()),
|
|
|
|
ConnectAddr::Udp(format!("{}:{}", ip, port).parse().unwrap()),
|
|
|
|
),
|
|
|
|
_ => panic!("invalid mode, run --help!"),
|
2020-04-08 14:26:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut background = None;
|
2021-01-15 13:04:32 +00:00
|
|
|
let runtime = Arc::new(Runtime::new().unwrap());
|
2020-04-08 14:26:42 +00:00
|
|
|
match matches.value_of("mode") {
|
2021-04-15 08:16:42 +00:00
|
|
|
Some("server") => server(addresses.0, Arc::clone(&runtime)),
|
|
|
|
Some("client") => client(addresses.1, Arc::clone(&runtime)),
|
2020-04-08 14:26:42 +00:00
|
|
|
Some("both") => {
|
2021-04-15 08:16:42 +00:00
|
|
|
let s = addresses.0;
|
2021-01-15 13:04:32 +00:00
|
|
|
let runtime2 = Arc::clone(&runtime);
|
2021-04-15 08:16:42 +00:00
|
|
|
background = Some(thread::spawn(|| server(s, runtime2)));
|
2020-04-08 14:26:42 +00:00
|
|
|
thread::sleep(Duration::from_millis(200)); //start client after server
|
2021-04-15 08:16:42 +00:00
|
|
|
client(addresses.1, Arc::clone(&runtime));
|
2020-04-08 14:26:42 +00:00
|
|
|
},
|
2020-07-05 22:13:53 +00:00
|
|
|
_ => panic!("Invalid mode, run --help!"),
|
2020-04-08 14:26:42 +00:00
|
|
|
};
|
|
|
|
if let Some(background) = background {
|
|
|
|
background.join().unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 08:16:42 +00:00
|
|
|
fn server(address: ListenAddr, runtime: Arc<Runtime>) {
|
2021-02-10 10:37:42 +00:00
|
|
|
let registry = Arc::new(Registry::new());
|
2022-09-09 15:29:43 +00:00
|
|
|
let mut server = Network::new_with_registry(Pid::new(), &runtime, ®istry);
|
2021-02-10 10:37:42 +00:00
|
|
|
runtime.spawn(Server::run(
|
|
|
|
Arc::clone(®istry),
|
|
|
|
SocketAddr::from(([0; 4], 59112)),
|
|
|
|
futures_util::future::pending(),
|
|
|
|
));
|
2021-01-15 13:04:32 +00:00
|
|
|
runtime.block_on(server.listen(address)).unwrap();
|
2020-04-08 14:26:42 +00:00
|
|
|
|
|
|
|
loop {
|
2021-02-14 17:45:12 +00:00
|
|
|
info!("----");
|
2020-07-05 22:13:53 +00:00
|
|
|
info!("Waiting for participant to connect");
|
2022-09-09 15:29:43 +00:00
|
|
|
let mut p1 = runtime.block_on(server.connected()).unwrap(); //remote representation of p1
|
2021-01-15 13:04:32 +00:00
|
|
|
let mut s1 = runtime.block_on(p1.opened()).unwrap(); //remote representation of s1
|
|
|
|
runtime.block_on(async {
|
2020-04-08 14:26:42 +00:00
|
|
|
let mut last = Instant::now();
|
|
|
|
let mut id = 0u64;
|
2020-04-24 10:56:04 +00:00
|
|
|
while let Ok(_msg) = s1.recv_raw().await {
|
2020-04-08 14:26:42 +00:00
|
|
|
id += 1;
|
|
|
|
if id.rem_euclid(1000000) == 0 {
|
|
|
|
let new = Instant::now();
|
|
|
|
let diff = new.duration_since(last);
|
|
|
|
last = new;
|
2020-07-05 22:13:53 +00:00
|
|
|
println!("Recv 1.000.000 took {}", diff.as_millis());
|
2020-04-08 14:26:42 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-05 22:13:53 +00:00
|
|
|
info!("Other stream was closed");
|
2020-04-08 14:26:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 08:16:42 +00:00
|
|
|
fn client(address: ConnectAddr, runtime: Arc<Runtime>) {
|
2021-02-10 10:37:42 +00:00
|
|
|
let registry = Arc::new(Registry::new());
|
2021-03-03 09:39:21 +00:00
|
|
|
let client = Network::new_with_registry(Pid::new(), &runtime, ®istry);
|
2021-02-10 10:37:42 +00:00
|
|
|
runtime.spawn(Server::run(
|
|
|
|
Arc::clone(®istry),
|
|
|
|
SocketAddr::from(([0; 4], 59111)),
|
|
|
|
futures_util::future::pending(),
|
|
|
|
));
|
2020-04-08 14:26:42 +00:00
|
|
|
|
2021-01-15 13:04:32 +00:00
|
|
|
let p1 = runtime.block_on(client.connect(address)).unwrap(); //remote representation of p1
|
2022-09-09 15:29:43 +00:00
|
|
|
let s1 = runtime
|
2021-02-18 00:01:57 +00:00
|
|
|
.block_on(p1.open(4, Promises::ORDERED | Promises::CONSISTENCY, 0))
|
2021-01-15 13:04:32 +00:00
|
|
|
.unwrap(); //remote representation of s1
|
2020-04-08 14:26:42 +00:00
|
|
|
let mut last = Instant::now();
|
|
|
|
let mut id = 0u64;
|
2020-10-16 09:21:18 +00:00
|
|
|
let raw_msg = Message::serialize(
|
|
|
|
&Msg::Ping {
|
2020-04-08 14:26:42 +00:00
|
|
|
id,
|
|
|
|
data: vec![0; 1000],
|
2020-10-16 09:21:18 +00:00
|
|
|
},
|
2021-03-25 11:22:31 +00:00
|
|
|
s1.params(),
|
2020-10-16 09:21:18 +00:00
|
|
|
);
|
2020-04-24 10:56:04 +00:00
|
|
|
loop {
|
2020-10-16 09:21:18 +00:00
|
|
|
s1.send_raw(&raw_msg).unwrap();
|
2020-04-08 14:26:42 +00:00
|
|
|
id += 1;
|
|
|
|
if id.rem_euclid(1000000) == 0 {
|
|
|
|
let new = Instant::now();
|
|
|
|
let diff = new.duration_since(last);
|
|
|
|
last = new;
|
2020-07-05 22:13:53 +00:00
|
|
|
println!("Send 1.000.000 took {}", diff.as_millis());
|
2020-04-08 14:26:42 +00:00
|
|
|
}
|
|
|
|
if id > 2000000 {
|
2020-07-05 22:13:53 +00:00
|
|
|
println!("Stop");
|
2022-07-15 12:08:04 +00:00
|
|
|
thread::sleep(Duration::from_millis(2000));
|
2020-04-08 14:26:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-05-27 15:58:57 +00:00
|
|
|
}
|
2020-05-15 12:29:17 +00:00
|
|
|
drop(s1);
|
2022-07-15 12:08:04 +00:00
|
|
|
thread::sleep(Duration::from_millis(2000));
|
2020-07-05 22:13:53 +00:00
|
|
|
info!("Closing participant");
|
2021-01-15 13:04:32 +00:00
|
|
|
runtime.block_on(p1.disconnect()).unwrap();
|
2022-07-15 12:08:04 +00:00
|
|
|
thread::sleep(Duration::from_millis(2000));
|
2020-05-15 12:29:17 +00:00
|
|
|
info!("DROPPING! client");
|
|
|
|
drop(client);
|
2022-07-15 12:08:04 +00:00
|
|
|
thread::sleep(Duration::from_millis(2000));
|
2020-04-08 14:26:42 +00:00
|
|
|
}
|