use std::collections::HashMap;
use thundra::{Benchmark, HttpMethod, RequestConfig, RequestContext};
#[tokio::main]
async fn main() -> thundra::Result<()> {
println!("Running benchmark with custom request generator...\n");
let results = Benchmark::builder()
.request_fn(|ctx: RequestContext| {
let user_id = ctx.request_number % 100;
let mut headers = HashMap::new();
headers.insert("X-Worker-Id".to_string(), ctx.worker_id.to_string());
headers.insert(
"X-Request-Number".to_string(),
ctx.request_number.to_string(),
);
let method = if ctx.request_number.is_multiple_of(10) {
HttpMethod::Post
} else {
HttpMethod::Get
};
RequestConfig {
url: format!("http://localhost:3000/user/{}", user_id),
method,
headers,
body: if method == HttpMethod::Post {
Some(
format!(r#"{{"user_id": {}, "worker": {}}}"#, user_id, ctx.worker_id)
.into(),
)
} else {
None
},
}
})
.concurrency(10)
.requests(100)
.show_progress(true)
.build()?
.run()
.await?;
results.print();
Ok(())
}