Skip to content

Commit bfdba7b

Browse files
committed
Posibility to specify body of the request
1 parent 90da2e4 commit bfdba7b

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ struct Args {
2525
#[arg(short, long, value_name = "URL")]
2626
url: String,
2727

28+
#[arg(short, long, value_name = "BODY")]
29+
body: Option<String>,
30+
2831
#[arg(short, long, value_name = "CONCURRENCY", default_value_t = 1)]
2932
concurrency: u64,
3033

@@ -75,6 +78,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
7578
&metrics,
7679
args.method,
7780
args.url,
81+
args.body,
7882
args.concurrency,
7983
args.duration,
8084
header_map,

src/scheduler/scheduler.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub struct Scheduler<'a> {
88
metrics: &'a Arc<Metrics>,
99
method: Method,
1010
url: String,
11+
body: Option<String>,
1112
concurrency: u64,
1213
duration: u64,
1314
headers: HeaderMap,
@@ -18,6 +19,7 @@ impl<'a> Scheduler<'a> {
1819
metrics: &'a Arc<Metrics>,
1920
method: Method,
2021
url: String,
22+
body: Option<String>,
2123
concurrency: u64,
2224
duration: u64,
2325
headers: HeaderMap,
@@ -26,6 +28,7 @@ impl<'a> Scheduler<'a> {
2628
metrics,
2729
method,
2830
url,
31+
body,
2932
concurrency,
3033
duration,
3134
headers,
@@ -42,12 +45,14 @@ impl<'a> Scheduler<'a> {
4245
for _ in 0..self.concurrency {
4346
let method = self.method.clone();
4447
let url = url.clone();
48+
let body = self.body.clone();
4549
let headers = headers.clone();
4650
let duration = self.duration;
4751
let metrics = Arc::clone(self.metrics);
4852

4953
tasks.push(tokio::spawn(async move {
50-
Scheduler::run_client(&metrics, start_bench, method, url, duration, headers).await;
54+
Scheduler::run_client(&metrics, start_bench, method, url, body, duration, headers)
55+
.await;
5156
}));
5257
}
5358

@@ -87,13 +92,14 @@ impl<'a> Scheduler<'a> {
8792
start_bench: std::time::Instant,
8893
method: Method,
8994
url: String,
95+
body: Option<String>,
9096
duration: u64,
9197
headers: HeaderMap,
9298
) {
9399
let client = Client::builder().default_headers(headers).build().unwrap();
94100

95101
loop {
96-
let result = Self::make_request(metrics, &client, &method, &url).await;
102+
let result = Self::make_request(metrics, &client, &method, &url, &body).await;
97103
Self::handle_request_result(metrics, result).await;
98104

99105
if std::time::Instant::now() >= start_bench + std::time::Duration::from_secs(duration) {
@@ -107,10 +113,17 @@ impl<'a> Scheduler<'a> {
107113
client: &Client,
108114
method: &Method,
109115
url: &str,
116+
body: &Option<String>,
110117
) -> Result<String, reqwest::Error> {
111118
let start = std::time::Instant::now();
112119

113-
let resp = client.request(method.clone(), url).send().await?;
120+
let req_builder = client.request(method.clone(), url);
121+
let req_builder = match body {
122+
Some(b) => req_builder.body(b.clone()),
123+
None => req_builder,
124+
};
125+
126+
let resp = req_builder.send().await?;
114127
let _status = resp.status();
115128
let body = resp.text().await?;
116129

0 commit comments

Comments
 (0)