-
Notifications
You must be signed in to change notification settings - Fork 0
Add concurrent requests #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: use_debug_flag
Are you sure you want to change the base?
Changes from 1 commit
9d8aaa0
584b6f2
61680eb
5f46a75
59aa0e0
09c7479
abe575e
44dc890
214ee76
1857c0c
b667689
2392f43
6c896df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ use reqwest::header::CONTENT_TYPE; | |
| use serde::{Serialize, Deserialize}; | ||
| use std::fmt; | ||
| use clap::{Parser}; | ||
| use std::thread::{spawn, JoinHandle}; | ||
|
|
||
| /// Defines the Ambi Mock Client command line interface as a struct | ||
| #[derive(Parser, Debug)] | ||
|
|
@@ -27,6 +28,10 @@ pub struct Cli { | |
| /// Turns verbose console debug output on | ||
| #[clap(short, long)] | ||
| pub debug: bool, | ||
|
|
||
| // Make int number of concurrent requests | ||
| #[clap(short, long, default_value_t = 1)] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there an upper limit to how many concurrent requests we want to allow for? Perhaps adding CLI interface data check on that would be in order. For example, do we really want to allow for 65k simultaneous threads, or at least without having tested it well? :) |
||
| pub int: u16 | ||
| } | ||
|
|
||
| #[derive(Serialize, Deserialize)] | ||
|
|
@@ -110,39 +115,34 @@ fn random_gen_dust_concentration() -> String { | |
| rng.gen_range(0..=1000).to_string() | ||
| } | ||
|
|
||
| pub fn run(cli: &Cli) { | ||
| println!("\r\ncli: {:?}\r\n", cli); | ||
|
|
||
| fn send_request(url: &str, client: Client, debug: bool) { | ||
| let dust_concentration = random_gen_dust_concentration(); | ||
| let air_purity = AirPurity::from_value(dust_concentration.parse::<u16>().unwrap()).to_string(); | ||
| let reading = Reading::new( | ||
| random_gen_temperature(), | ||
| random_gen_humidity(), | ||
| random_gen_pressure(), | ||
| dust_concentration, | ||
| air_purity | ||
| air_purity, | ||
| ); | ||
|
|
||
| let json = serde_json::to_string(&reading).unwrap(); | ||
| const URL: &str = "http://localhost:4000/api/readings/add"; | ||
|
|
||
| println!("Sending POST request to {} as JSON: {}", URL, json); | ||
|
|
||
| let client = Client::new(); | ||
| println!("Sending POST request to {} as JSON: {}", url, json); | ||
| let res = client | ||
| .post(URL) | ||
| .post(url) | ||
| .header(CONTENT_TYPE, "application/json") | ||
| .body(json) | ||
| .send(); | ||
| match res { | ||
| Ok(response) => { | ||
| match cli.debug { | ||
| true => println!("Response from Ambi backend: {:#?}", response), | ||
| false => println!("Response from Ambi backend: {:?}", response.status().as_str()) | ||
| } | ||
| } | ||
| Ok(response) => match debug { | ||
calebbourg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| true => println!("Response from Ambi backend: {:#?}", response), | ||
| false => println!( | ||
| "Response from Ambi backend: {:?}", | ||
| response.status().as_str() | ||
| ), | ||
| }, | ||
| Err(e) => { | ||
| match cli.debug { | ||
| match debug { | ||
|
||
| // Print out the entire reqwest::Error for verbose debugging | ||
| true => eprintln!("Response error from Ambi backend: {:?}", e), | ||
| // Keep the error reports more succinct | ||
|
|
@@ -160,6 +160,23 @@ pub fn run(cli: &Cli) { | |
| } | ||
| } | ||
|
|
||
| pub fn run(cli: &Cli) { | ||
| println!("\r\ncli: {:?}\r\n", cli); | ||
|
|
||
| const URL: &str = "http://localhost:4000/api/readings/add"; | ||
| let mut handlers: Vec<JoinHandle<()>> = vec![]; | ||
| let debug: bool = cli.debug; | ||
| for _ in 0..cli.int { | ||
| let handler = spawn(move || send_request(URL, Client::new(), debug)); | ||
|
|
||
| handlers.push(handler); | ||
| } | ||
|
|
||
| for handler in handlers { | ||
| handler.join().unwrap(); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.