use crate::core::config::{Config, Mode};
use crate::core::constants::VERSION;
use clap::{Parser, Subcommand};
const HELP_BANNER: &str = concat!(
"\x1b[38;2;196;248;245m██████╗ ██╗ ██╗███████╗████████╗██████╗ ███████╗███████╗\x1b[0m\n",
"\x1b[38;2;173;226;224m██╔══██╗██║ ██║██╔════╝╚══██╔══╝██╔══██╗██╔════╝██╔════╝\x1b[0m\n",
"\x1b[38;2;150;204;203m██████╔╝██║ ██║███████╗ ██║ ██████╔╝█████╗ ███████╗\x1b[0m\n",
"\x1b[38;2;127;182;182m██╔══██╗██║ ██║╚════██║ ██║ ██╔══██╗██╔══╝ ╚════██║\x1b[0m\n",
"\x1b[38;2;104;160;161m██║ ██║╚██████╔╝███████║ ██║ ██║ ██║███████╗███████║\x1b[0m\n",
"\x1b[38;2;79;137;139m╚═╝ ╚═╝ ╚═════╝ ╚══════╝ ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝\x1b[0m\n",
"\x1b[38;2;196;248;245m v",
env!("CARGO_PKG_VERSION"),
" | High-Performance Load Testing Engine\x1b[0m\n",
"\n",
"🚀 Quick Start:\n",
" rustress --url http://localhost:8080 --rate 100 --duration 30 # RPS mode\n",
" rustress --url http://localhost:8080 --users 50 --duration 30 # Users mode\n",
"\n",
"💡 Examples:\n",
" rustress # Launch interactive TUI\n",
" rustress -u http://localhost:8080 # TUI with pre-filled URL\n",
" rustress -u http://localhost:8080 -r 1000 -d 60 # Headless: 1000 RPS for 60s\n",
" rustress dummy --port 9090 # Start test server on port 9090\n",
" rustress report -i results.csv # Show summary from CSV\n",
);
#[derive(Parser, Debug)]
#[command(name = "rustress")]
#[command(version = VERSION)]
#[command(about = None)]
#[command(before_help = HELP_BANNER)]
pub struct Cli {
#[arg(short, long)]
pub url: Option<String>,
#[arg(short, long)]
pub method: Option<String>,
#[arg(short, long)]
pub body: Option<String>,
#[arg(short, long)]
pub rate: Option<u32>,
#[arg(short = 'n', long)]
pub users: Option<u32>,
#[arg(short, long)]
pub duration: Option<u64>,
#[arg(long)]
pub ramp_up: Option<u64>,
#[arg(long)]
pub ramp_down: Option<u64>,
#[arg(long)]
pub timeout: Option<u64>,
#[arg(short = 'H', long, action = clap::ArgAction::Append)]
pub header: Vec<String>,
#[arg(long)]
pub out: Option<String>,
#[arg(long)]
pub max_concurrency: Option<u32>,
#[arg(long)]
pub pool_max_idle_per_host: Option<u32>,
#[arg(long)]
pub pool_idle_timeout: Option<u64>,
#[arg(long)]
pub config: Option<String>,
#[command(subcommand)]
pub command: Option<Commands>,
}
#[derive(Subcommand, Debug)]
pub enum Commands {
Dummy {
#[arg(short, long, default_value_t = 8080)]
port: u16,
},
Report {
#[arg(short, long)]
input: String,
},
}
impl Cli {
pub fn into_config(&self) -> Config {
let mut cfg = match self.config {
Some(ref path) => match load_config_file(path) {
Ok(cfg) => cfg,
Err(e) => {
eprintln!("{e}");
std::process::exit(2);
}
},
None => Config::default(),
};
if let Some(ref url) = self.url {
cfg.url = url.clone();
}
if let Some(ref method) = self.method {
cfg.method = method.clone();
}
if let Some(ref body) = self.body {
cfg.body = Some(body.clone());
}
if let Some(ref out) = self.out {
cfg.out_prefix = Some(out.clone());
}
if let Some(rate) = self.rate {
cfg.target_rps = rate;
}
if let Some(users) = self.users {
cfg.num_users = users;
cfg.mode = Mode::Users;
}
if let Some(dur) = self.duration {
cfg.steady_dur_secs = dur;
}
if let Some(ramp) = self.ramp_up {
cfg.ramp_up_secs = ramp;
}
if let Some(ramp) = self.ramp_down {
cfg.ramp_down_secs = ramp;
}
if let Some(timeout) = self.timeout {
cfg.timeout_secs = timeout;
}
if let Some(mc) = self.max_concurrency {
cfg.max_concurrency = mc;
}
if let Some(p) = self.pool_max_idle_per_host {
cfg.pool_max_idle_per_host = p;
}
if let Some(t) = self.pool_idle_timeout {
cfg.pool_idle_timeout_secs = t;
}
for h in &self.header {
if let Some((key, value)) = h.split_once(':') {
cfg.headers
.insert(key.trim().to_string(), value.trim().to_string());
}
}
cfg
}
}
fn load_config_file(path: &str) -> anyhow::Result<Config> {
let contents = std::fs::read_to_string(path)
.map_err(|e| anyhow::anyhow!("failed to read config file '{path}': {e}"))?;
toml::from_str::<Config>(&contents)
.map_err(|e| anyhow::anyhow!("failed to parse config file '{path}': {e}"))
}