use clap::{Parser, Subcommand};
use rperf3::{server::Server, Config, Protocol};
use std::time::Duration;
#[derive(Parser)]
#[command(name = "rperf3")]
#[command(about = "A Rust implementation of iperf3 - network performance testing tool", long_about = None)]
#[command(version)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Server {
#[arg(short, long, default_value = "5201")]
port: u16,
#[arg(short, long)]
bind: Option<String>,
#[arg(short, long)]
udp: bool,
},
Client {
server: String,
#[arg(short, long, default_value = "5201")]
port: u16,
#[arg(short, long)]
udp: bool,
#[arg(short = 't', long, default_value = "10")]
time: u64,
#[arg(short, long)]
bandwidth: Option<u64>,
#[arg(short = 'l', long, default_value = "131072")]
length: usize,
#[arg(short = 'P', long, default_value = "1")]
parallel: usize,
#[arg(short = 'R', long)]
reverse: bool,
#[arg(short = 'J', long)]
json: bool,
#[arg(short, long, default_value = "1")]
interval: u64,
},
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
let cli = Cli::parse();
match cli.command {
Commands::Server { port, bind, udp } => {
let protocol = if udp { Protocol::Udp } else { Protocol::Tcp };
let mut config = Config::server(port).with_protocol(protocol);
if let Some(bind_addr) = bind {
config.bind_addr = Some(bind_addr.parse()?);
}
let server = Server::new(config);
server.run().await?;
}
Commands::Client {
server,
port,
udp,
time,
bandwidth,
length,
parallel,
reverse,
json,
interval,
} => {
let protocol = if udp { Protocol::Udp } else { Protocol::Tcp };
let mut config = Config::client(server, port)
.with_protocol(protocol)
.with_duration(Duration::from_secs(time))
.with_buffer_size(length)
.with_parallel(parallel)
.with_reverse(reverse)
.with_json(json)
.with_interval(Duration::from_secs(interval));
if let Some(bw) = bandwidth {
config = config.with_bandwidth(bw * 1_000_000); }
use rperf3::client::Client;
let client = Client::new(config)?;
client.run().await?;
}
}
Ok(())
}