Skip to content

Commit 9d8aaa0

Browse files
committed
add -i flag for multiple concurrent requests
1 parent 4b1889b commit 9d8aaa0

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

src/lib.rs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use reqwest::header::CONTENT_TYPE;
1515
use serde::{Serialize, Deserialize};
1616
use std::fmt;
1717
use clap::{Parser};
18+
use std::thread::{spawn, JoinHandle};
1819

1920
/// Defines the Ambi Mock Client command line interface as a struct
2021
#[derive(Parser, Debug)]
@@ -27,6 +28,10 @@ pub struct Cli {
2728
/// Turns verbose console debug output on
2829
#[clap(short, long)]
2930
pub debug: bool,
31+
32+
// Make int number of concurrent requests
33+
#[clap(short, long, default_value_t = 1)]
34+
pub int: u16
3035
}
3136

3237
#[derive(Serialize, Deserialize)]
@@ -110,39 +115,34 @@ fn random_gen_dust_concentration() -> String {
110115
rng.gen_range(0..=1000).to_string()
111116
}
112117

113-
pub fn run(cli: &Cli) {
114-
println!("\r\ncli: {:?}\r\n", cli);
115-
118+
fn send_request(url: &str, client: Client, debug: bool) {
116119
let dust_concentration = random_gen_dust_concentration();
117120
let air_purity = AirPurity::from_value(dust_concentration.parse::<u16>().unwrap()).to_string();
118121
let reading = Reading::new(
119122
random_gen_temperature(),
120123
random_gen_humidity(),
121124
random_gen_pressure(),
122125
dust_concentration,
123-
air_purity
126+
air_purity,
124127
);
125128

126129
let json = serde_json::to_string(&reading).unwrap();
127-
const URL: &str = "http://localhost:4000/api/readings/add";
128-
129-
println!("Sending POST request to {} as JSON: {}", URL, json);
130-
131-
let client = Client::new();
130+
println!("Sending POST request to {} as JSON: {}", url, json);
132131
let res = client
133-
.post(URL)
132+
.post(url)
134133
.header(CONTENT_TYPE, "application/json")
135134
.body(json)
136135
.send();
137136
match res {
138-
Ok(response) => {
139-
match cli.debug {
140-
true => println!("Response from Ambi backend: {:#?}", response),
141-
false => println!("Response from Ambi backend: {:?}", response.status().as_str())
142-
}
143-
}
137+
Ok(response) => match debug {
138+
true => println!("Response from Ambi backend: {:#?}", response),
139+
false => println!(
140+
"Response from Ambi backend: {:?}",
141+
response.status().as_str()
142+
),
143+
},
144144
Err(e) => {
145-
match cli.debug {
145+
match debug {
146146
// Print out the entire reqwest::Error for verbose debugging
147147
true => eprintln!("Response error from Ambi backend: {:?}", e),
148148
// Keep the error reports more succinct
@@ -160,6 +160,23 @@ pub fn run(cli: &Cli) {
160160
}
161161
}
162162

163+
pub fn run(cli: &Cli) {
164+
println!("\r\ncli: {:?}\r\n", cli);
165+
166+
const URL: &str = "http://localhost:4000/api/readings/add";
167+
let mut handlers: Vec<JoinHandle<()>> = vec![];
168+
let debug: bool = cli.debug;
169+
for _ in 0..cli.int {
170+
let handler = spawn(move || send_request(URL, Client::new(), debug));
171+
172+
handlers.push(handler);
173+
}
174+
175+
for handler in handlers {
176+
handler.join().unwrap();
177+
}
178+
}
179+
163180
#[cfg(test)]
164181
mod tests {
165182
use super::*;

0 commit comments

Comments
 (0)