Send and receive are working. Now to make them faster.

This commit is contained in:
Ben Shiller 2025-06-15 13:39:32 -05:00
parent 666bae83ec
commit 2306735f42
Signed by: shillerben
GPG Key ID: 7B4602B1FBF82986
4 changed files with 120 additions and 0 deletions

7
Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "rperf"
version = "0.1.0"
edition = "2024"
[dependencies]
clap = { version = "4.5.40", features = ["derive"] }

58
src/main.rs Normal file
View File

@ -0,0 +1,58 @@
mod send;
mod receive;
use std::io;
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(name = "rperf")]
#[command(about = "A simple CLI for sending and receiving UDP messages", long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Send a message to a specified host and port
Send {
/// The target IP address and port: host:port
#[arg(long)]
host: String,
/// The message to send
#[arg(short, long)]
message: String,
/// Number of times to send
#[arg(short, long)]
iters: u32,
},
/// Receive a message from a specified host and port
Receive {
/// The src IP address and port: host:port
#[arg(long)]
host: String,
/// Number of times to receive
#[arg(short, long)]
iters: u32,
},
}
fn main() -> io::Result<()> {
let cli = Cli::parse();
match cli.command {
Commands::Send { host, message, iters } => {
println!("Sending message '{}' to {}", message, host);
send::send(&host, &message, iters)?
}
Commands::Receive { host, iters } => {
println!("Receiving messages from {}", host);
receive::receive(&host, iters)?
}
}
Ok(())
}

34
src/receive.rs Normal file
View File

@ -0,0 +1,34 @@
use std::net::UdpSocket;
use std::io;
use std::time::Instant;
pub fn receive(host: &str, iters: u32) -> io::Result<()> {
let socket = UdpSocket::bind(host).expect("Failed to bind socket");
let packet_size = 1080;
let mut buf = vec![0u8; packet_size];
let mut total_size: usize = 0;
let mut start: Option<Instant> = None;
for i in 0..iters {
match socket.recv_from(&mut buf) {
Ok((size, _)) => {
if start.is_none() {
start = Some(Instant::now());
}
total_size += size;
if i % 10000 == 0 {
let elapsed = start.unwrap().elapsed().as_secs_f64();
let mbps = (total_size * 8) as f64 / 1e6 / elapsed;
let pps = 10000.0 / elapsed;
println!("{}: Received {} bytes in {} seconds ({} mbps, {} pps)", i, total_size, elapsed, mbps, pps);
total_size = 0;
start = None;
}
},
Err(e) => eprintln!("Failed to receive message: {}", e),
}
}
Ok(())
}

21
src/send.rs Normal file
View File

@ -0,0 +1,21 @@
use std::net::UdpSocket;
use std::io;
pub fn send(host: &str, message: &str, iters: u32) -> io::Result<()> {
let socket = UdpSocket::bind("0.0.0.0:0").expect("Failed to bind socket");
let packet_size = 1080;
let message = String::from(message);
let padding = "\0".repeat(packet_size - message.len());
let buf = message + &padding;
for _ in 0..iters {
match socket.send_to(buf.as_bytes(), host) {
Ok(_) => (),
Err(e) => eprintln!("Failed to send message: {}", e),
}
}
Ok(())
}