use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use std::time::Duration;
use crate::core::constants::{
DEFAULT_DURATION_SECS, DEFAULT_MAX_CONCURRENCY, DEFAULT_METHOD, DEFAULT_NUM_USERS,
DEFAULT_TARGET_RPS, DEFAULT_TIMEOUT_SECS, MAX_ALLOWED_CONCURRENCY, MAX_ALLOWED_RPS,
MAX_ALLOWED_USERS, MIN_ALLOWED_CONCURRENCY,
};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Mode {
#[default]
Rps,
Users,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
pub url: String,
#[serde(default = "default_method")]
pub method: String,
pub body: Option<String>,
#[serde(default)]
pub headers: IndexMap<String, String>,
#[serde(default = "default_target_rps")]
pub target_rps: u32,
#[serde(default = "default_duration")]
pub steady_dur_secs: u64,
#[serde(default)]
pub ramp_up_secs: u64,
#[serde(default)]
pub ramp_down_secs: u64,
#[serde(default = "default_timeout")]
pub timeout_secs: u64,
#[serde(default)]
pub mode: Mode,
#[serde(default = "default_num_users")]
pub num_users: u32,
#[serde(default)]
pub think_time_ms: u64,
pub command: Option<String>,
pub out_prefix: Option<String>,
#[serde(default = "default_max_concurrency")]
pub max_concurrency: u32,
}
impl Default for Config {
fn default() -> Self {
Self {
url: String::new(),
method: default_method(),
body: None,
headers: IndexMap::new(),
target_rps: default_target_rps(),
steady_dur_secs: default_duration(),
ramp_up_secs: 0,
ramp_down_secs: 0,
timeout_secs: default_timeout(),
mode: Mode::default(),
num_users: default_num_users(),
think_time_ms: 0,
command: None,
out_prefix: None,
max_concurrency: default_max_concurrency(),
}
}
}
impl Config {
pub fn total_duration(&self) -> Duration {
Duration::from_secs(self.ramp_up_secs + self.steady_dur_secs + self.ramp_down_secs)
}
pub fn validate(&self) -> Result<(), Vec<String>> {
let mut errors = Vec::new();
if self.mode == Mode::Rps && self.target_rps == 0 {
errors.push("target_rps must be greater than 0 in RPS mode".into());
}
if self.target_rps > MAX_ALLOWED_RPS {
errors.push(format!(
"target_rps must be at most {MAX_ALLOWED_RPS} (got {})",
self.target_rps
));
}
if self.mode == Mode::Users && self.num_users == 0 {
errors.push("num_users must be greater than 0 in Users mode".into());
}
if self.num_users > MAX_ALLOWED_USERS {
errors.push(format!(
"num_users must be at most {MAX_ALLOWED_USERS} (got {})",
self.num_users
));
}
if self.steady_dur_secs == 0 {
errors.push("steady_dur_secs must be greater than 0".into());
}
if self.max_concurrency < MIN_ALLOWED_CONCURRENCY {
errors.push(format!(
"max_concurrency must be at least {MIN_ALLOWED_CONCURRENCY} (got {})",
self.max_concurrency
));
}
if self.max_concurrency > MAX_ALLOWED_CONCURRENCY {
errors.push(format!(
"max_concurrency must be at most {MAX_ALLOWED_CONCURRENCY} (got {})",
self.max_concurrency
));
}
if self.timeout_secs == 0 {
errors.push("timeout_secs must be greater than 0".into());
}
if self.url.is_empty() && self.command.is_none() {
errors.push("either url or command must be specified".into());
} else if !self.url.is_empty() && !has_http_scheme(&self.url) {
errors.push(format!(
"url must be absolute and start with http:// or https:// (got {})",
self.url
));
}
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}
}
fn has_http_scheme(url: &str) -> bool {
let lowered = url.to_ascii_lowercase();
lowered.starts_with("http://") || lowered.starts_with("https://")
}
fn default_method() -> String {
DEFAULT_METHOD.to_string()
}
fn default_target_rps() -> u32 {
DEFAULT_TARGET_RPS
}
fn default_duration() -> u64 {
DEFAULT_DURATION_SECS
}
fn default_timeout() -> u64 {
DEFAULT_TIMEOUT_SECS
}
fn default_num_users() -> u32 {
DEFAULT_NUM_USERS
}
fn default_max_concurrency() -> u32 {
DEFAULT_MAX_CONCURRENCY
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let cfg = Config::default();
assert_eq!(cfg.method, "GET");
assert_eq!(cfg.target_rps, 100);
assert_eq!(cfg.steady_dur_secs, 30);
assert_eq!(cfg.mode, Mode::Rps);
}
#[test]
fn test_total_duration() {
let cfg = Config {
ramp_up_secs: 5,
steady_dur_secs: 30,
ramp_down_secs: 5,
..Default::default()
};
assert_eq!(cfg.total_duration(), Duration::from_secs(40));
}
#[test]
fn test_validate_empty_url_and_command() {
let cfg = Config::default();
assert!(cfg.validate().is_err());
}
#[test]
fn test_validate_rps_zero() {
let cfg = Config {
url: "http://localhost".into(),
target_rps: 0,
mode: Mode::Rps,
..Default::default()
};
assert!(cfg.validate().is_err());
}
#[test]
fn test_validate_users_zero() {
let cfg = Config {
command: Some("echo test".into()),
num_users: 0,
mode: Mode::Users,
..Default::default()
};
assert!(cfg.validate().is_err());
}
#[test]
fn test_validate_ok() {
let cfg = Config {
url: "http://localhost:8080".into(),
target_rps: 50,
steady_dur_secs: 10,
mode: Mode::Rps,
..Default::default()
};
assert!(cfg.validate().is_ok());
}
}