use crate::core::config::Config;
pub fn current_rps(cfg: &Config, elapsed_secs: f64) -> f64 {
let total_ramp_secs = cfg.ramp_up_secs as f64;
if elapsed_secs < total_ramp_secs {
if cfg.ramp_up_secs == 0 {
return cfg.target_rps as f64;
}
return cfg.target_rps as f64 * (elapsed_secs / total_ramp_secs);
}
let steady_end = (cfg.ramp_up_secs + cfg.steady_dur_secs) as f64;
if steady_end == 0.0 {
} else if elapsed_secs < steady_end {
return cfg.target_rps as f64;
}
let total_dur = (cfg.ramp_up_secs + cfg.steady_dur_secs + cfg.ramp_down_secs) as f64;
if elapsed_secs < total_dur {
if cfg.ramp_down_secs == 0 {
return 0.0;
}
let remaining = total_dur - elapsed_secs;
return cfg.target_rps as f64 * (remaining / cfg.ramp_down_secs as f64);
}
0.0
}
#[cfg(test)]
mod tests {
use super::*;
fn cfg(ramp_up: u64, steady: u64, ramp_down: u64, target: u32) -> Config {
Config {
url: "http://localhost".into(),
target_rps: target,
ramp_up_secs: ramp_up,
steady_dur_secs: steady,
ramp_down_secs: ramp_down,
..Default::default()
}
}
#[test]
fn test_no_ramp_immediate_full_rps() {
let c = cfg(0, 10, 0, 100);
assert_eq!(current_rps(&c, 0.0), 100.0);
assert_eq!(current_rps(&c, 5.0), 100.0);
assert_eq!(current_rps(&c, 9.9), 100.0);
}
#[test]
fn test_ramp_up_linear() {
let c = cfg(10, 10, 0, 100);
let eps = 0.1;
assert!((current_rps(&c, 5.0) - 50.0).abs() < eps);
assert!((current_rps(&c, 10.0) - 100.0).abs() < eps);
}
#[test]
fn test_steady_state() {
let c = cfg(5, 20, 5, 200);
assert_eq!(current_rps(&c, 5.0), 200.0);
assert_eq!(current_rps(&c, 15.0), 200.0);
assert_eq!(current_rps(&c, 24.9), 200.0);
}
#[test]
fn test_ramp_down_linear() {
let c = cfg(0, 10, 10, 100);
let eps = 0.1;
assert!((current_rps(&c, 15.0) - 50.0).abs() < eps);
assert!(current_rps(&c, 19.9) < 10.0);
assert_eq!(current_rps(&c, 20.0), 0.0);
}
#[test]
fn test_full_ramp_profile() {
let c = cfg(10, 20, 10, 100);
let eps = 0.5;
assert!(current_rps(&c, 0.0) < 1.0);
assert!((current_rps(&c, 5.0) - 50.0).abs() < eps);
assert!((current_rps(&c, 15.0) - 100.0).abs() < eps);
assert!((current_rps(&c, 35.0) - 50.0).abs() < eps);
assert_eq!(current_rps(&c, 40.0), 0.0);
assert_eq!(current_rps(&c, 50.0), 0.0);
}
#[test]
fn ramp_up_ends_exactly_at_the_full_rate() {
let c = cfg(10, 5, 0, 100);
assert!(
(current_rps(&c, 9.999) - 99.99).abs() < 0.01,
"the last instant of ramp-up must still be ramping"
);
assert_eq!(
current_rps(&c, 10.0),
100.0,
"the instant ramp-up ends must already be the full rate"
);
}
#[test]
fn steady_ends_exactly_at_the_start_of_ramp_down() {
let c = cfg(0, 10, 10, 100);
assert_eq!(current_rps(&c, 9.999), 100.0);
assert!(
(current_rps(&c, 10.0) - 100.0).abs() < 0.01,
"ramp-down must start from the full rate, not from zero"
);
assert!(
(current_rps(&c, 10.001) - 99.999).abs() < 0.01,
"the first instant past steady must have begun to decline"
);
}
#[test]
fn no_ramp_down_means_the_rate_stops_at_the_end_of_steady() {
let c = cfg(5, 5, 0, 100);
assert_eq!(
current_rps(&c, 10.0),
0.0,
"with no ramp-down configured the generator must stop, not hold the \
full rate until the caller notices"
);
assert_eq!(current_rps(&c, 11.0), 0.0);
}
#[test]
fn a_run_with_no_steady_phase_ramps_straight_down_from_the_full_rate() {
let c = cfg(0, 0, 10, 100);
assert_eq!(current_rps(&c, 0.0), 100.0);
assert!((current_rps(&c, 5.0) - 50.0).abs() < 0.1);
assert_eq!(current_rps(&c, 10.0), 0.0);
}
#[test]
fn ramp_up_with_no_steady_phase_still_reaches_the_full_rate() {
let c = cfg(10, 0, 10, 100);
assert!((current_rps(&c, 10.0) - 100.0).abs() < 0.1);
assert!((current_rps(&c, 15.0) - 50.0).abs() < 0.1);
assert_eq!(current_rps(&c, 20.0), 0.0);
}
}