-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcustom_requests.rs
More file actions
53 lines (46 loc) · 1.63 KB
/
custom_requests.rs
File metadata and controls
53 lines (46 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use httpress::{Benchmark, HttpMethod, RequestConfig, RequestContext};
use std::collections::HashMap;
#[tokio::main]
async fn main() -> httpress::Result<()> {
println!("Running benchmark with custom request generator...\n");
// Example: Rotating URLs and dynamic headers
let results = Benchmark::builder()
.request_fn(|ctx: RequestContext| {
// Rotate through different user IDs
let user_id = ctx.request_number % 100;
// Add custom headers based on worker and request number
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(),
);
// Vary request method based on request number
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(())
}