mod common;
use axum::Router;
use axum::body::{Body, Bytes};
use axum::http::StatusCode;
use axum::response::Response;
use axum::routing::get;
use rustress::core::snapshot::StatsSnapshot;
use rustress::dummy::server::BIG_BODY_BYTES;
use common::{Harness, TestServer, cfg_for, dummy_server, serve};
const CHUNK_BYTES: usize = 8 * 1024;
const CHUNK_COUNT: usize = 1024;
const CHUNKED_TOTAL_BYTES: u64 = (CHUNK_BYTES * CHUNK_COUNT) as u64;
static CHUNK: [u8; CHUNK_BYTES] = [b'x'; CHUNK_BYTES];
async fn handler_chunked() -> Response {
let stream = futures_util::stream::unfold(0usize, |sent| async move {
if sent >= CHUNK_COUNT {
return None;
}
Some((
Ok::<Bytes, std::io::Error>(Bytes::from_static(&CHUNK)),
sent + 1,
))
});
Response::builder()
.status(StatusCode::OK)
.body(Body::from_stream(stream))
.expect("a statically-built response cannot fail to build")
}
async fn chunked_server() -> TestServer {
serve(Router::new().route("/chunked", get(handler_chunked))).await
}
fn big_body_config(url: &str) -> rustress::core::config::Config {
let mut cfg = cfg_for(url);
cfg.target_rps = 20;
cfg.steady_dur_secs = 1;
cfg.max_concurrency = 8;
cfg.timeout_secs = 10;
cfg
}
fn assert_bytes_measured(
results: &[rustress::core::result::ExperimentResult],
snap: &StatsSnapshot,
expected: u64,
label: &str,
) {
assert!(
!results.is_empty(),
"{label}: the engine executed no requests, so nothing was proven about byte counting"
);
for (i, r) in results.iter().enumerate() {
assert_eq!(
r.bytes as u64, expected,
"{label}: request {i} counted {} bytes, expected {expected}",
r.bytes
);
}
assert_eq!(
snap.bytes,
results.len() as u64 * expected,
"{label}: the aggregate must be the exact sum of the per-request counts, \
not a re-derived or clamped figure"
);
}
#[tokio::test]
async fn chunked_bytes_are_counted_from_the_stream_not_from_content_length() {
let server = chunked_server().await;
let probe = reqwest::Client::new()
.get(server.url("/chunked"))
.send()
.await
.expect("the chunked fixture must answer a direct GET");
assert_eq!(probe.status(), StatusCode::OK);
assert_eq!(
probe.content_length(),
None,
"the chunked fixture must not advertise a content-length, or it is not \
testing chunked byte counting"
);
let mut probed = 0u64;
let mut probe_body = probe;
while let Some(chunk) = probe_body
.chunk()
.await
.expect("draining the fixture must succeed")
{
probed += chunk.len() as u64;
}
assert_eq!(
probed, CHUNKED_TOTAL_BYTES,
"the fixture must actually put {CHUNKED_TOTAL_BYTES} bytes on the wire"
);
let harness = Harness::new(big_body_config(&server.url("/chunked")))
.expect("config should construct an engine");
harness.run().await;
let results = harness.results();
let snapshot = harness.snapshot();
assert_bytes_measured(&results, &snapshot, CHUNKED_TOTAL_BYTES, "chunked");
}
#[tokio::test]
async fn content_length_bytes_are_counted_exactly_once() {
let server = dummy_server().await;
let probe = reqwest::Client::new()
.get(server.url("/big-ok"))
.send()
.await
.expect("the sized fixture must answer a direct GET");
assert_eq!(
probe.content_length(),
Some(BIG_BODY_BYTES as u64),
"this fixture must advertise a content-length, or it is not the control"
);
let harness = Harness::new(big_body_config(&server.url("/big-ok")))
.expect("config should construct an engine");
harness.run().await;
let results = harness.results();
let snapshot = harness.snapshot();
assert_bytes_measured(&results, &snapshot, BIG_BODY_BYTES as u64, "content-length");
}
#[tokio::test]
async fn error_response_bytes_are_counted_too() {
let server = dummy_server().await;
let harness = Harness::new(big_body_config(&server.url("/big")))
.expect("config should construct an engine");
harness.run().await;
let results = harness.results();
let snapshot = harness.snapshot();
assert!(snapshot.fail > 0, "the /big fixture always returns 500");
assert_bytes_measured(&results, &snapshot, BIG_BODY_BYTES as u64, "500");
}