Skip to content

Commit 90da2e4

Browse files
committed
Posibility to specify method of the request
1 parent 5a7bba0 commit 90da2e4

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::sync::Arc;
22

33
use clap::{Parser, ValueEnum};
44
use hammerload::{metrics::metrics::Metrics, scheduler::scheduler::Scheduler};
5+
use reqwest::Method;
56

67
#[derive(ValueEnum, Debug, Clone)]
78
enum Protocol {
@@ -18,6 +19,9 @@ struct Args {
1819
#[arg(value_name = "PROTOCOL")]
1920
protocol: Protocol,
2021

22+
#[arg(short, long, value_name = "METHOD", default_value = "GET")]
23+
method: Method,
24+
2125
#[arg(short, long, value_name = "URL")]
2226
url: String,
2327

@@ -69,6 +73,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
6973

7074
let scheduler = Scheduler::new(
7175
&metrics,
76+
args.method,
7277
args.url,
7378
args.concurrency,
7479
args.duration,

src/scheduler/scheduler.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use std::sync::Arc;
22

3-
use reqwest::{header::HeaderMap, Client};
3+
use reqwest::{header::HeaderMap, Client, Method};
44

55
use crate::metrics::metrics::Metrics;
66

77
pub struct Scheduler<'a> {
88
metrics: &'a Arc<Metrics>,
9+
method: Method,
910
url: String,
1011
concurrency: u64,
1112
duration: u64,
@@ -15,13 +16,15 @@ pub struct Scheduler<'a> {
1516
impl<'a> Scheduler<'a> {
1617
pub fn new(
1718
metrics: &'a Arc<Metrics>,
19+
method: Method,
1820
url: String,
1921
concurrency: u64,
2022
duration: u64,
2123
headers: HeaderMap,
2224
) -> Self {
2325
Scheduler {
2426
metrics,
27+
method,
2528
url,
2629
concurrency,
2730
duration,
@@ -37,13 +40,14 @@ impl<'a> Scheduler<'a> {
3740
let headers = self.headers.clone();
3841

3942
for _ in 0..self.concurrency {
43+
let method = self.method.clone();
4044
let url = url.clone();
4145
let headers = headers.clone();
4246
let duration = self.duration;
4347
let metrics = Arc::clone(self.metrics);
4448

4549
tasks.push(tokio::spawn(async move {
46-
Scheduler::run_client(&metrics, start_bench, url, duration, headers).await;
50+
Scheduler::run_client(&metrics, start_bench, method, url, duration, headers).await;
4751
}));
4852
}
4953

@@ -81,14 +85,15 @@ impl<'a> Scheduler<'a> {
8185
async fn run_client(
8286
metrics: &'a Arc<Metrics>,
8387
start_bench: std::time::Instant,
88+
method: Method,
8489
url: String,
8590
duration: u64,
8691
headers: HeaderMap,
8792
) {
8893
let client = Client::builder().default_headers(headers).build().unwrap();
8994

9095
loop {
91-
let result = Self::make_request(metrics, &client, &url).await;
96+
let result = Self::make_request(metrics, &client, &method, &url).await;
9297
Self::handle_request_result(metrics, result).await;
9398

9499
if std::time::Instant::now() >= start_bench + std::time::Duration::from_secs(duration) {
@@ -100,11 +105,12 @@ impl<'a> Scheduler<'a> {
100105
async fn make_request(
101106
metrics: &Arc<Metrics>,
102107
client: &Client,
108+
method: &Method,
103109
url: &str,
104110
) -> Result<String, reqwest::Error> {
105111
let start = std::time::Instant::now();
106112

107-
let resp = client.get(url).send().await?;
113+
let resp = client.request(method.clone(), url).send().await?;
108114
let _status = resp.status();
109115
let body = resp.text().await?;
110116

0 commit comments

Comments
 (0)