Skip to content

Commit 0ffe61d

Browse files
2.1.1
1 parent c2febc2 commit 0ffe61d

File tree

5 files changed

+81
-13
lines changed

5 files changed

+81
-13
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "layer7benchmark"
3-
version = "2.0.1"
3+
version = "2.1.1"
44
edition = "2021"
55
description = "A simple benchmark tool for Layer 7 (HTTP) load testing"
66
license = "MIT"

src/args.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,7 @@ pub struct Args {
6262
/// Four log levels are available: error, warn, info, and debug.
6363
#[arg(long = "log-level", default_value = "info")]
6464
pub log_level: String,
65+
66+
#[arg(long = "normal-output", default_value_t = false)]
67+
pub normal_output: bool,
6568
}

src/http_benchmark.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,14 @@ pub async fn run(args: Args) -> Result<()> {
5555
handles.spawn(handle);
5656
}
5757

58-
tokio::spawn(terminal::terminal_output(
59-
method.clone(),
60-
shutdown_rx.clone(),
61-
));
58+
if args.normal_output {
59+
tokio::spawn(terminal::normal_output(method.clone(), shutdown_rx.clone()));
60+
} else {
61+
tokio::spawn(terminal::terminal_output(
62+
method.clone(),
63+
shutdown_rx.clone(),
64+
));
65+
}
6266

6367
tokio::spawn(shutdown::handle_shutdown_signals(shutdown_tx.clone()));
6468
wait_for_completion(time, shutdown_tx.clone(), shutdown_rx.clone()).await?;

src/terminal.rs

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,91 @@ use std::{
1212

1313
use 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+
1528
pub 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
"\nrequest 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
"\nnetwork 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

Comments
 (0)