use axum::{Router, http::StatusCode, response::IntoResponse, routing::get};
use rand::Rng;
use std::time::Duration;
use tracing::info;
pub struct DummyServer {
port: u16,
}
impl DummyServer {
pub fn new(port: u16) -> Self {
Self { port }
}
pub fn router() -> Router {
Router::new()
.route("/fast", get(handler_fast).post(handler_fast))
.route("/medium", get(handler_medium).post(handler_medium))
.route("/slow", get(handler_slow).post(handler_slow))
.route("/spike", get(handler_spike).post(handler_spike))
.route("/error", get(handler_error).post(handler_error))
.route("/big", get(handler_big).post(handler_big))
.route("/big-ok", get(handler_big_ok).post(handler_big_ok))
.route("/sized", get(handler_sized).post(handler_sized))
.with_state(())
}
pub async fn run(&self) -> anyhow::Result<()> {
let app = Self::router();
let addr = format!("0.0.0.0:{}", self.port);
info!("Dummy server listening on {}", addr);
info!(" /fast — 10-50ms jitter");
info!(" /medium — 100-300ms jitter");
info!(" /slow — 1000-2000ms jitter");
info!(" /spike — 95% 20ms, 5% 2000ms");
info!(" /error — 20% 500, 20% 429, 60% 200");
info!(" /big — 8 MB 500 body (memory-bound regression fixture)");
info!(" /big-ok — 8 MB 200 body (throughput byte-counting fixture)");
info!(" /sized?bytes=N — 500 with a body of exactly N bytes (capture-cap fixture)");
let listener = tokio::net::TcpListener::bind(&addr).await?;
axum::serve(listener, app).await?;
Ok(())
}
}
async fn handler_fast() -> impl IntoResponse {
let ms = rand::thread_rng().gen_range(10..50);
tokio::time::sleep(Duration::from_millis(ms)).await;
(StatusCode::OK, format!("fast: {}ms", ms))
}
async fn handler_medium() -> impl IntoResponse {
let ms = rand::thread_rng().gen_range(100..300);
tokio::time::sleep(Duration::from_millis(ms)).await;
(StatusCode::OK, format!("medium: {}ms", ms))
}
async fn handler_slow() -> impl IntoResponse {
let ms = rand::thread_rng().gen_range(1000..2000);
tokio::time::sleep(Duration::from_millis(ms)).await;
(StatusCode::OK, format!("slow: {}ms", ms))
}
async fn handler_spike() -> impl IntoResponse {
let r: u32 = rand::thread_rng().gen_range(0..100);
if r < 95 {
tokio::time::sleep(Duration::from_millis(20)).await;
(StatusCode::OK, "spike: 20ms")
} else {
tokio::time::sleep(Duration::from_millis(2000)).await;
(StatusCode::OK, "spike: 2000ms")
}
}
async fn handler_error() -> impl IntoResponse {
let r: u32 = rand::thread_rng().gen_range(0..100);
if r < 20 {
(StatusCode::INTERNAL_SERVER_ERROR, "error: 500")
} else if r < 40 {
(StatusCode::TOO_MANY_REQUESTS, "error: 429")
} else {
(StatusCode::OK, "ok: 200")
}
}
async fn handler_big() -> impl IntoResponse {
(
StatusCode::INTERNAL_SERVER_ERROR,
vec![b'x'; BIG_BODY_BYTES],
)
}
async fn handler_big_ok() -> impl IntoResponse {
(StatusCode::OK, vec![b'x'; BIG_BODY_BYTES])
}
#[derive(serde::Deserialize)]
struct SizedQuery {
bytes: usize,
}
async fn handler_sized(
axum::extract::Query(query): axum::extract::Query<SizedQuery>,
) -> axum::response::Response {
use crate::core::constants::MAX_DRAINED_BODY_BYTES;
if query.bytes > MAX_DRAINED_BODY_BYTES as usize {
return (
StatusCode::BAD_REQUEST,
format!("bytes must not exceed {MAX_DRAINED_BODY_BYTES}"),
)
.into_response();
}
(StatusCode::INTERNAL_SERVER_ERROR, vec![b'x'; query.bytes]).into_response()
}
pub const BIG_BODY_BYTES: usize = 8 * 1024 * 1024;