Skip to content

Commit 5782646

Browse files
Renamed request to post
1 parent 9807ab5 commit 5782646

File tree

2 files changed

+44
-38
lines changed

2 files changed

+44
-38
lines changed

src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
1212
mod data;
1313
pub mod error;
14-
mod requests;
14+
mod sensor_posts;
1515

16-
use crate::requests::{send_data, PostSchedulerBuilder};
16+
use crate::sensor_posts::{send_data, PostSchedulerBuilder};
1717
use clap::Parser;
1818
use error::PostSchedulerError;
1919
use log::debug;
20-
use requests::MAX_NUM_THREADS;
20+
use sensor_posts::MAX_NUM_THREADS;
2121
use std::time::Duration;
2222

2323
const URL: &str = "http://localhost:8000/api/readings/add";
@@ -38,11 +38,11 @@ pub struct Cli {
3838
/// The number of sensor readings to post.
3939
/// [DEFAULT: 1]
4040
#[arg(short = 'n', long)]
41-
pub request_amount: Option<u32>,
41+
pub post_amount: Option<u32>,
4242
/// The time between each sensor reading post (in seconds).
4343
/// [DEFAULT: 10]
44-
#[arg(short = 't', long = "time-per-request")]
45-
pub time_per_request_s: Option<u64>,
44+
#[arg(short = 't', long = "time-per-post")]
45+
pub time_per_post_s: Option<u64>,
4646
/// The total time over which all the sensor reading posts must be sent (in seconds, alternative to -t).
4747
#[arg(short = 'T', long = "total-time")]
4848
pub total_time_s: Option<u64>,
@@ -70,8 +70,8 @@ pub fn run(cli: &Cli) -> Result<(), PostSchedulerError> {
7070
debug!("cli: {cli:?}");
7171

7272
let req_scheduler = PostSchedulerBuilder::default()
73-
.with_some_request_amount(&cli.request_amount)
74-
.with_some_time_per_request(&cli.time_per_request_s.map(Duration::from_secs))
73+
.with_some_post_amount(&cli.post_amount)
74+
.with_some_time_per_post(&cli.time_per_post_s.map(Duration::from_secs))
7575
.with_some_total_time(&cli.total_time_s.map(Duration::from_secs))
7676
.with_some_num_threads(&cli.num_threads)
7777
.build()?;

src/requests.rs renamed to src/sensor_posts.rs

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,36 @@ use crate::{
1313
URL,
1414
};
1515

16-
const DEFAULT_REQUEST_AMOUNT: u32 = 1;
17-
const DEFAULT_TIME_PER_REQUEST: Duration = Duration::from_secs(10);
16+
const DEFAULT_POST_AMOUNT: u32 = 1;
17+
const DEFAULT_TIME_PER_POST: Duration = Duration::from_secs(10);
1818
const DEFAULT_NUM_THREADS: u32 = 1;
1919
pub const MAX_NUM_THREADS: u32 = 10;
2020

2121
#[derive(Clone, Copy)]
2222
pub struct PostSchedulerBuilder {
23-
request_amount: Option<u32>,
24-
time_per_request: Option<Duration>,
23+
post_amount: Option<u32>,
24+
time_per_post: Option<Duration>,
2525
total_time: Option<Duration>,
2626
num_threads: Option<u32>,
2727
}
2828

2929
impl PostSchedulerBuilder {
3030
pub fn default() -> Self {
3131
PostSchedulerBuilder {
32-
request_amount: None,
33-
time_per_request: None,
32+
post_amount: None,
33+
time_per_post: None,
3434
total_time: None,
3535
num_threads: None,
3636
}
3737
}
3838

39-
pub fn with_some_request_amount(mut self, request_amount: &Option<u32>) -> Self {
40-
self.request_amount = *request_amount;
39+
pub fn with_some_post_amount(mut self, post_amount: &Option<u32>) -> Self {
40+
self.post_amount = *post_amount;
4141
self
4242
}
4343

44-
pub fn with_some_time_per_request(mut self, time_per_request: &Option<Duration>) -> Self {
45-
self.time_per_request = *time_per_request;
44+
pub fn with_some_time_per_post(mut self, time_per_post: &Option<Duration>) -> Self {
45+
self.time_per_post = *time_per_post;
4646
self
4747
}
4848

@@ -58,24 +58,24 @@ impl PostSchedulerBuilder {
5858

5959
pub fn build(self) -> Result<PostScheduler, PostSchedulerError> {
6060
// Loop indefinitely if no req amt is set. If time per req is also not set then don't loop.
61-
let loop_indefinitely = self.request_amount.is_none() && self.time_per_request.is_some();
61+
let loop_indefinitely = self.post_amount.is_none() && self.time_per_post.is_some();
6262

63-
let request_amount = self.request_amount.unwrap_or(DEFAULT_REQUEST_AMOUNT);
63+
let post_amount = self.post_amount.unwrap_or(DEFAULT_POST_AMOUNT);
6464

65-
let time_per_request = match (&self.time_per_request, &self.total_time) {
66-
(None, None) => DEFAULT_TIME_PER_REQUEST,
67-
(None, Some(total_time)) => *total_time / request_amount,
68-
(Some(time_per_request), None) => *time_per_request,
69-
(Some(time_per_request), Some(_)) => *time_per_request,
65+
let time_per_post = match (&self.time_per_post, &self.total_time) {
66+
(None, None) => DEFAULT_TIME_PER_POST,
67+
(None, Some(total_time)) => *total_time / post_amount,
68+
(Some(time_per_post), None) => *time_per_post,
69+
(Some(time_per_post), Some(_)) => *time_per_post,
7070
};
7171

7272
let num_threads = self.num_threads.unwrap_or(DEFAULT_NUM_THREADS);
7373
// At this point we know that the number of threads is in [1, `MAX_NUM_THREADS`].
7474
// Validation is done in `lib::is_valid_num_of_threads`.
7575

7676
Ok(PostScheduler {
77-
request_amount,
78-
time_per_request,
77+
post_amount,
78+
time_per_post,
7979
num_threads,
8080
loop_indefinitely,
8181
})
@@ -84,8 +84,8 @@ impl PostSchedulerBuilder {
8484

8585
#[derive(Clone, Copy)]
8686
pub struct PostScheduler {
87-
request_amount: u32,
88-
time_per_request: Duration,
87+
post_amount: u32,
88+
time_per_post: Duration,
8989
num_threads: u32,
9090
loop_indefinitely: bool,
9191
}
@@ -133,26 +133,26 @@ fn send_data_internal(
133133
) {
134134
if req_scheduler.loop_indefinitely {
135135
loop {
136-
make_request(thread_id, &client, rng);
137-
thread::sleep(req_scheduler.time_per_request)
136+
make_post(thread_id, &client, rng);
137+
thread::sleep(req_scheduler.time_per_post)
138138
}
139139
}
140140

141-
for i in 0..req_scheduler.request_amount {
142-
make_request(thread_id, &client, rng);
141+
for i in 0..req_scheduler.post_amount {
142+
make_post(thread_id, &client, rng);
143143

144144
// Only use thread.sleep if we are not on the last request
145-
if i != req_scheduler.request_amount - 1 {
146-
thread::sleep(req_scheduler.time_per_request)
145+
if i != req_scheduler.post_amount - 1 {
146+
thread::sleep(req_scheduler.time_per_post)
147147
}
148148
}
149149
}
150150

151151
/// This also can run in parallel, refer to [`send_data_internal`].
152-
fn make_request(thread_id: u32, client: &Client, rng: &mut ThreadRng) {
152+
fn make_post(thread_id: u32, client: &Client, rng: &mut ThreadRng) {
153153
let json = generate_random_reading(rng);
154154
info!("[Thread {thread_id}]: Sending POST request to {}", URL);
155-
debug!("[Thread {thread_id}]: Request JSON: {}", json);
155+
debug!("[Thread {thread_id}]: Post JSON: {}", json);
156156

157157
let res = client
158158
.post(URL)
@@ -162,10 +162,16 @@ fn make_request(thread_id: u32, client: &Client, rng: &mut ThreadRng) {
162162

163163
match res {
164164
Ok(response) => {
165-
info!(
165+
let info_msg = format!(
166166
"[Thread {thread_id}]: Response from Ambi backend: {}",
167167
response.status().as_str()
168168
);
169+
170+
match response.status().is_success() {
171+
true => info!("{}", info_msg),
172+
false => error!("{}", info_msg),
173+
}
174+
169175
debug!(
170176
"[Thread {thread_id}]: Response from Ambi backend: {:#?}",
171177
response

0 commit comments

Comments
 (0)