@@ -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 ) ;
1818const DEFAULT_NUM_THREADS : u32 = 1 ;
1919pub const MAX_NUM_THREADS : u32 = 10 ;
2020
2121#[ derive( Clone , Copy ) ]
2222pub 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
2929impl 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 ) ]
8686pub 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