use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio_util::sync::CancellationToken;
use uuid::Uuid;
use crate::core::config::{Config, Mode};
use crate::core::constants::STATS_UPDATE_INTERVAL_MS;
use crate::runner::client::build_client;
use crate::runner::executor::{execute_http, execute_script};
use crate::runner::ramp::current_rps;
use crate::runner::request::PreparedRequest;
use crate::runner::stats::{InflightGuard, RunStats};
use crate::templates::{TemplateContext, TemplateEngine};
const DRAIN_POLL_INTERVAL_MS: u64 = 20;
const MAX_SCHEDULE_SLIP: Duration = Duration::from_secs(1);
pub struct LoadEngine {
cfg: Arc<Config>,
client: reqwest::Client,
prepared: PreparedRequest,
template_engine: Arc<TemplateEngine>,
stats: Arc<RunStats>,
permits: Arc<tokio::sync::Semaphore>,
}
impl LoadEngine {
pub fn new(
cfg: Config,
updates: tokio::sync::mpsc::Sender<crate::core::snapshot::StatsSnapshot>,
) -> anyhow::Result<Self> {
if let Err(errors) = cfg.validate() {
anyhow::bail!("invalid configuration:\n - {}", errors.join("\n - "));
}
let client = build_client(&cfg)?;
let permits = Arc::new(tokio::sync::Semaphore::new(cfg.max_concurrency as usize));
Ok(Self {
prepared: PreparedRequest::new(&cfg, &TemplateEngine::new())?,
client,
template_engine: Arc::new(TemplateEngine::new()),
stats: Arc::new(RunStats::new(updates)),
permits,
cfg: Arc::new(cfg),
})
}
pub fn stats(&self) -> &Arc<RunStats> {
&self.stats
}
pub async fn run(&self, cancel: CancellationToken) {
let ticker = self.spawn_stats_ticker(cancel.clone());
match self.cfg.mode {
Mode::Users => self.run_users(cancel.clone()).await,
Mode::Rps => self.run_rps(cancel.clone()).await,
}
ticker.abort();
}
fn spawn_stats_ticker(&self, cancel: CancellationToken) -> tokio::task::JoinHandle<()> {
let stats = Arc::clone(&self.stats);
tokio::spawn(async move {
let mut interval =
tokio::time::interval(Duration::from_millis(STATS_UPDATE_INTERVAL_MS));
loop {
tokio::select! {
_ = interval.tick() => stats.publish_snapshot(),
_ = cancel.cancelled() => {
stats.publish_snapshot();
break;
}
}
}
})
}
async fn run_users(&self, cancel: CancellationToken) {
let total_duration = self.cfg.total_duration();
let start = Instant::now();
let spawn_interval = self.user_spawn_interval();
let mut handles = Vec::with_capacity(self.cfg.num_users.min(1_024) as usize);
for index in 0..self.cfg.num_users {
if cancel.is_cancelled() {
break;
}
if index > 0 && !spawn_interval.is_zero() {
tokio::select! {
_ = tokio::time::sleep(spawn_interval) => {}
_ = cancel.cancelled() => break,
}
}
handles.push(self.spawn_virtual_user(cancel.clone(), start, total_duration));
}
for handle in handles {
let _ = handle.await;
}
}
fn user_spawn_interval(&self) -> Duration {
if self.cfg.ramp_up_secs > 0 && self.cfg.num_users > 1 {
Duration::from_secs_f64(self.cfg.ramp_up_secs as f64 / self.cfg.num_users as f64)
} else {
Duration::ZERO
}
}
fn spawn_virtual_user(
&self,
cancel: CancellationToken,
start: Instant,
total_duration: Duration,
) -> tokio::task::JoinHandle<()> {
let cfg = Arc::clone(&self.cfg);
let client = self.client.clone();
let engine = Arc::clone(&self.template_engine);
let stats = Arc::clone(&self.stats);
let prepared = self.prepared.clone();
let user_id = Uuid::new_v4().to_string();
tokio::spawn(async move {
loop {
if cancel.is_cancelled() || start.elapsed() > total_duration {
break;
}
let scheduled = Instant::now();
let ctx = TemplateContext::new(user_id.clone());
let _admitted = InflightGuard::admit(&stats);
if cfg.command.is_some() {
execute_script(&cfg, &engine, &ctx, scheduled, &stats).await;
} else {
execute_http(&client, &prepared, &engine, &ctx, scheduled, &stats).await;
}
if cfg.think_time_ms > 0 {
tokio::select! {
_ = tokio::time::sleep(Duration::from_millis(cfg.think_time_ms)) => {}
_ = cancel.cancelled() => break,
}
}
}
})
}
async fn run_rps(&self, cancel: CancellationToken) {
let total_duration = self.cfg.total_duration();
let start = Instant::now();
let mut next_request_time = start;
loop {
if cancel.is_cancelled() || start.elapsed() >= total_duration {
break;
}
let target_rps = current_rps(&self.cfg, start.elapsed().as_secs_f64());
if target_rps <= 0.001 {
tokio::time::sleep(Duration::from_millis(100)).await;
next_request_time = Instant::now();
continue;
}
let period = Duration::from_secs_f64(1.0 / target_rps);
self.absorb_schedule_slip(&mut next_request_time, period);
self.dispatch_due_requests(&mut next_request_time, period, &cancel);
sleep_until(next_request_time).await;
}
self.await_drain().await;
self.stats.publish_snapshot();
}
fn absorb_schedule_slip(&self, next_request_time: &mut Instant, period: Duration) {
let now = Instant::now();
let behind = now.saturating_duration_since(*next_request_time);
if behind <= MAX_SCHEDULE_SLIP {
return;
}
let period_ns = period.max(Duration::from_nanos(1)).as_nanos();
let skipped = (behind.as_nanos() / period_ns).min(u64::MAX as u128) as u64;
self.stats.record_scheduled_drops(skipped);
*next_request_time = now;
}
fn dispatch_due_requests(
&self,
next_request_time: &mut Instant,
period: Duration,
cancel: &CancellationToken,
) {
let now = Instant::now();
while *next_request_time <= now {
if cancel.is_cancelled() {
return;
}
match self.permits.clone().try_acquire_owned() {
Ok(permit) => {
self.spawn_scheduled(*next_request_time, permit);
}
Err(_) => self.stats.record_scheduled_drop(),
}
*next_request_time += period;
}
}
fn spawn_scheduled(&self, scheduled: Instant, permit: tokio::sync::OwnedSemaphorePermit) {
let client = self.client.clone();
let engine = Arc::clone(&self.template_engine);
let stats = Arc::clone(&self.stats);
let prepared = self.prepared.clone();
let cfg = Arc::clone(&self.cfg);
let ctx = TemplateContext::new(Uuid::new_v4().to_string());
let admitted = InflightGuard::admit(&stats);
tokio::spawn(async move {
let _permit = permit;
let _admitted = admitted;
if cfg.command.is_some() {
execute_script(&cfg, &engine, &ctx, scheduled, &stats).await;
} else {
execute_http(&client, &prepared, &engine, &ctx, scheduled, &stats).await;
}
});
}
async fn await_drain(&self) {
while self.stats.inflight_count() > 0 {
tokio::time::sleep(Duration::from_millis(DRAIN_POLL_INTERVAL_MS)).await;
}
}
}
async fn sleep_until(deadline: Instant) {
let wait = deadline.saturating_duration_since(Instant::now());
if !wait.is_zero() {
tokio::time::sleep(wait).await;
}
}