@@ -12,35 +12,91 @@ use std::{
1212
1313use crate :: statistic:: STATISTIC ;
1414
15+ pub enum OutputMode {
16+ Terminal { refresh_rate_ms : u64 } ,
17+ Normal { refresh_rate_ms : u64 } ,
18+ }
19+
20+ impl Default for OutputMode {
21+ fn default ( ) -> Self {
22+ OutputMode :: Terminal {
23+ refresh_rate_ms : 200 ,
24+ }
25+ }
26+ }
27+
1528pub async fn terminal_output (
1629 method : reqwest:: Method ,
1730 shutdown_signal : tokio:: sync:: watch:: Receiver < bool > ,
31+ ) -> anyhow:: Result < ( ) > {
32+ output_statistics (
33+ method,
34+ shutdown_signal,
35+ OutputMode :: Terminal {
36+ refresh_rate_ms : 200 ,
37+ } ,
38+ )
39+ . await
40+ }
41+
42+ pub async fn normal_output (
43+ method : reqwest:: Method ,
44+ shutdown_signal : tokio:: sync:: watch:: Receiver < bool > ,
45+ ) -> anyhow:: Result < ( ) > {
46+ output_statistics (
47+ method,
48+ shutdown_signal,
49+ OutputMode :: Normal {
50+ refresh_rate_ms : 2000 ,
51+ } ,
52+ )
53+ . await
54+ }
55+
56+ async fn output_statistics (
57+ method : reqwest:: Method ,
58+ shutdown_signal : tokio:: sync:: watch:: Receiver < bool > ,
59+ mode : OutputMode ,
1860) -> anyhow:: Result < ( ) > {
1961 let s = STATISTIC . get ( ) . unwrap ( ) ;
2062 let counter = & s. request_counter ;
2163 let sc = & s. status_counter ;
2264 let network_traffics = & s. network_traffics ;
2365
66+ // Initial delay before starting output
2467 tokio:: time:: sleep ( Duration :: from_secs ( 4 ) ) . await ;
68+
2569 let mut stdout = stdout ( ) ;
2670 write ! ( stdout, "\n " ) ?;
2771 stdout. flush ( ) ?;
28- let ( _, y) = cursor:: position ( ) ?;
72+
73+ // Only get cursor position when using terminal mode
74+ let cursor_y = match mode {
75+ OutputMode :: Terminal { .. } => Some ( cursor:: position ( ) ?. 1 ) ,
76+ OutputMode :: Normal { .. } => None ,
77+ } ;
78+
2979 loop {
3080 if * shutdown_signal. borrow ( ) {
3181 break Ok ( ( ) ) ;
3282 }
3383
34- stdout
35- . execute ( MoveTo ( 0 , y) ) ?
36- . execute ( Clear ( ClearType :: FromCursorDown ) ) ?;
84+ // Clear screen if in terminal mode
85+ if let Some ( y) = cursor_y {
86+ stdout
87+ . execute ( MoveTo ( 0 , y) ) ?
88+ . execute ( Clear ( ClearType :: FromCursorDown ) ) ?;
89+ }
90+
91+ // Write request count
3792 write ! (
3893 stdout,
3994 "The {} request has sent {} times" ,
4095 method,
4196 counter. load( Ordering :: Relaxed )
4297 ) ?;
4398
99+ // Write status counts
44100 write ! (
45101 stdout,
46102 "\n request status counter's results: 2xx: {} 3xx: {} 4xx: {} 5xx: {} timeout: {}" ,
@@ -51,8 +107,8 @@ pub async fn terminal_output(
51107 sc. status_other. load( Ordering :: Relaxed )
52108 ) ?;
53109
110+ // Write network traffic
54111 let byte = Byte :: from_u64 ( network_traffics. load ( Ordering :: Relaxed ) ) ;
55-
56112 write ! (
57113 stdout,
58114 "\n network traffics: {} bytes, Human readable: {}" ,
@@ -61,9 +117,14 @@ pub async fn terminal_output(
61117 ) ?;
62118
63119 writeln ! ( stdout, "" ) ?;
64-
65120 stdout. flush ( ) ?;
66121
67- tokio:: time:: sleep ( Duration :: from_millis ( 200 ) ) . await ;
122+ // Sleep according to the specified mode
123+ let sleep_duration = match mode {
124+ OutputMode :: Terminal { refresh_rate_ms } => refresh_rate_ms,
125+ OutputMode :: Normal { refresh_rate_ms } => refresh_rate_ms,
126+ } ;
127+
128+ tokio:: time:: sleep ( Duration :: from_millis ( sleep_duration) ) . await ;
68129 }
69130}
0 commit comments