Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hatoo committed Jan 18, 2025
1 parent 5c4b2c2 commit f18a0f3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 52 deletions.
10 changes: 5 additions & 5 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use hyper_util::rt::{TokioExecutor, TokioIo};
use rand::prelude::*;
use std::{
borrow::Cow,
io::Write,
sync::{
atomic::{AtomicBool, Ordering::Relaxed},
Arc,
Expand Down Expand Up @@ -965,15 +966,14 @@ fn set_start_latency_correction<E>(
}
}

/// Run n tasks by m workers
pub async fn work_debug(client: Arc<Client>) -> Result<(), ClientError> {
pub async fn work_debug<W: Write>(w: &mut W, client: Arc<Client>) -> Result<(), ClientError> {
let mut rng = StdRng::from_entropy();
let url = client.url_generator.generate(&mut rng)?;
println!("URL: {}", url);
writeln!(w, "URL: {}", url)?;

let request = client.request(&url)?;

println!("{:#?}", request);
writeln!(w, "{:#?}", request)?;

let response = if client.is_work_http2() {
let (_, mut client_state) = client.connect_http2(&url, &mut rng).await?;
Expand All @@ -989,7 +989,7 @@ pub async fn work_debug(client: Arc<Client>) -> Result<(), ClientError> {

let response = http::Response::from_parts(parts, body);

println!("{:#?}", response);
writeln!(w, "{:#?}", response)?;

Ok(())
}
Expand Down
59 changes: 33 additions & 26 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,26 +511,6 @@ async fn run() -> anyhow::Result<()> {
_ => None,
};

let print_config = {
let mode = if opts.json {
PrintMode::Json
} else {
PrintMode::Text
};
let output: Box<dyn std::io::Write + Send + 'static> = if let Some(output) = opts.output {
Box::new(File::create(output)?)
} else {
Box::new(std::io::stdout())
};

PrintConfig {
mode,
output,
disable_color: opts.disable_color,
stats_success_breakdown: opts.stats_success_breakdown,
}
};

let ip_strategy = match (opts.ipv4, opts.ipv6) {
(false, false) => Default::default(),
(true, false) => hickory_resolver::config::LookupIpStrategy::Ipv4Only,
Expand Down Expand Up @@ -581,6 +561,30 @@ async fn run() -> anyhow::Result<()> {

let no_tui = opts.no_tui || !std::io::stdout().is_tty() || opts.debug;

let print_config = {
let mode = if opts.json {
PrintMode::Json
} else {
PrintMode::Text
};

let disable_style =
opts.disable_color || !std::io::stdout().is_tty() || opts.output.is_some();

let output: Box<dyn std::io::Write + Send + 'static> = if let Some(output) = opts.output {
Box::new(File::create(output)?)
} else {
Box::new(std::io::stdout())
};

PrintConfig {
mode,
output,
disable_style,
stats_success_breakdown: opts.stats_success_breakdown,
}
};

// When panics, reset terminal mode and exit immediately.
std::panic::set_hook(Box::new(move |info| {
if !no_tui {
Expand All @@ -597,6 +601,13 @@ async fn run() -> anyhow::Result<()> {

let data_collect_future: Pin<Box<dyn std::future::Future<Output = (ResultData, PrintConfig)>>> =
match work_mode {
WorkMode::Debug => {
let mut print_config = print_config;
if let Err(e) = client::work_debug(&mut print_config.output, client).await {
eprintln!("{e}");
}
std::process::exit(libc::EXIT_SUCCESS)
}
WorkMode::FixedNumber {
n_requests,
n_connections,
Expand Down Expand Up @@ -700,6 +711,7 @@ async fn run() -> anyhow::Result<()> {
report_receiver: result_rx,
start,
fps: opts.fps,
disable_color: opts.disable_color,
}
.monitor(),
);
Expand All @@ -709,12 +721,7 @@ async fn run() -> anyhow::Result<()> {
};

match mode {
WorkMode::Debug => {
if let Err(e) = client::work_debug(client).await {
eprintln!("{e}");
}
std::process::exit(libc::EXIT_SUCCESS)
}
WorkMode::Debug => unreachable!("Must be already handled"),
WorkMode::FixedNumber {
n_requests,
n_connections,
Expand Down
3 changes: 2 additions & 1 deletion src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub struct Monitor {
pub start: std::time::Instant,
// Frame per second of TUI
pub fps: usize,
pub disable_color: bool,
}

struct IntoRawMode;
Expand Down Expand Up @@ -105,7 +106,7 @@ impl Monitor {
let mut timescale_auto = None;

let mut colors = ColorScheme::new();
if !self.print_config.disable_color {
if !self.disable_color {
colors.set_colors();
}

Expand Down
44 changes: 24 additions & 20 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ use std::{

#[derive(Clone, Copy)]
struct StyleScheme {
color_enabled: bool,
style_enabled: bool,
}
impl StyleScheme {
fn no_color(self, text: &str) -> StyledContent<&str> {
text.reset()
fn no_style(self, text: &str) -> StyledContent<&str> {
StyledContent::new(crossterm::style::ContentStyle::new(), text)
}
fn heading(self, text: &str) -> StyledContent<&str> {
text.bold().underlined()
if self.style_enabled {
text.bold().underlined()
} else {
self.no_style(text)
}
}
fn success_rate(self, text: &str, success_rate: f64) -> StyledContent<&str> {
if self.color_enabled {
if self.style_enabled {
if success_rate >= 100.0 {
text.green().bold()
} else if success_rate >= 99.0 {
Expand All @@ -31,28 +35,28 @@ impl StyleScheme {
text.red().bold()
}
} else {
self.no_color(text).bold()
self.no_style(text)
}
}
fn fastest(self, text: &str) -> StyledContent<&str> {
if self.color_enabled {
if self.style_enabled {
text.green()
} else {
self.no_color(text)
self.no_style(text)
}
}
fn slowest(self, text: &str) -> StyledContent<&str> {
if self.color_enabled {
if self.style_enabled {
text.yellow()
} else {
self.no_color(text)
self.no_style(text)
}
}
fn average(self, text: &str) -> StyledContent<&str> {
if self.color_enabled {
if self.style_enabled {
text.cyan()
} else {
self.no_color(text)
self.no_style(text)
}
}

Expand All @@ -61,7 +65,7 @@ impl StyleScheme {
const LATENCY_YELLOW_THRESHOLD: f64 = 0.1;
const LATENCY_RED_THRESHOLD: f64 = 0.4;

if self.color_enabled {
if self.style_enabled {
if label <= LATENCY_YELLOW_THRESHOLD {
text.green()
} else if label <= LATENCY_RED_THRESHOLD {
Expand All @@ -70,12 +74,12 @@ impl StyleScheme {
text.red()
}
} else {
self.no_color(text)
self.no_style(text)
}
}

fn status_distribution(self, text: &str, status: StatusCode) -> StyledContent<&str> {
if self.color_enabled {
if self.style_enabled {
if status.is_success() {
text.green()
} else if status.is_client_error() {
Expand All @@ -86,7 +90,7 @@ impl StyleScheme {
text.white()
}
} else {
self.no_color(text)
self.no_style(text)
}
}
}
Expand All @@ -100,7 +104,7 @@ pub enum PrintMode {
pub struct PrintConfig {
pub output: Box<dyn Write + Send + 'static>,
pub mode: PrintMode,
pub disable_color: bool,
pub disable_style: bool,
pub stats_success_breakdown: bool,
}

Expand All @@ -115,7 +119,7 @@ pub fn print_result(
&mut config.output,
res,
total_duration,
config.disable_color,
config.disable_style,
config.stats_success_breakdown,
)?,
PrintMode::Json => print_json(
Expand Down Expand Up @@ -374,11 +378,11 @@ fn print_summary<W: Write>(
w: &mut W,
res: &ResultData,
total_duration: Duration,
disable_color: bool,
disable_style: bool,
stats_success_breakdown: bool,
) -> std::io::Result<()> {
let style = StyleScheme {
color_enabled: !disable_color,
style_enabled: !disable_style,
};
writeln!(w, "{}", style.heading("Summary:"))?;
let success_rate = 100.0 * res.success_rate();
Expand Down

0 comments on commit f18a0f3

Please sign in to comment.