Skip to content

Commit

Permalink
Merge branch 'master' into opt-timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
hatoo committed Feb 4, 2024
2 parents f237ee1 + 2b42ed2 commit 1f87b19
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 115 deletions.
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use hyper::http::{
use printer::PrintMode;
use rand::prelude::*;
use rand_regex::Regex;
use result_data::ResultData;
use std::{io::Read, str::FromStr};
use url::Url;
use url_generator::UrlGenerator;
Expand All @@ -18,15 +19,14 @@ mod client;
mod histogram;
mod monitor;
mod printer;
mod result_data;
mod timescale;
mod url_generator;

#[cfg(unix)]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

use client::{ClientError, RequestResult};

#[derive(Parser)]
#[clap(author, about, version, override_usage = "oha [FLAGS] [OPTIONS] <url>")]
#[command(arg_required_else_help(true))]
Expand Down Expand Up @@ -427,7 +427,7 @@ async fn main() -> anyhow::Result<()> {
}
});

let mut all: Vec<Result<RequestResult, ClientError>> = Vec::new();
let mut all: ResultData = Default::default();
loop {
tokio::select! {
report = result_rx.recv_async() => {
Expand Down Expand Up @@ -610,7 +610,7 @@ async fn main() -> anyhow::Result<()> {

let duration = start.elapsed();

let res: Vec<Result<RequestResult, ClientError>> = data_collector.await??;
let res: ResultData = data_collector.await??;

printer::print_result(
&mut std::io::stdout(),
Expand Down
42 changes: 19 additions & 23 deletions src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::{collections::BTreeMap, io};
use crate::{
client::{ClientError, RequestResult},
printer::PrintMode,
result_data::ResultData,
timescale::{TimeLabel, TimeScale},
};

Expand Down Expand Up @@ -65,7 +66,7 @@ pub struct Monitor {
}

impl Monitor {
pub async fn monitor(self) -> Result<Vec<Result<RequestResult, ClientError>>, std::io::Error> {
pub async fn monitor(self) -> Result<ResultData, std::io::Error> {
crossterm::terminal::enable_raw_mode()?;
io::stdout().execute(crossterm::terminal::EnterAlternateScreen)?;
io::stdout().execute(crossterm::cursor::Hide)?;
Expand All @@ -77,11 +78,9 @@ impl Monitor {

// Return this when ends to application print summary
// We must not read all data from this due to computational cost.
let mut all: Vec<Result<RequestResult, ClientError>> = Vec::new();
let mut all: ResultData = Default::default();
// stats for HTTP status
let mut status_dist: BTreeMap<http::StatusCode, usize> = Default::default();
// stats for Error
let mut error_dist: BTreeMap<String, usize> = Default::default();

#[cfg(unix)]
// Limit for number open files. eg. ulimit -n
Expand All @@ -100,9 +99,8 @@ impl Monitor {
loop {
match self.report_receiver.try_recv() {
Ok(report) => {
match report.as_ref() {
Ok(report) => *status_dist.entry(report.status).or_default() += 1,
Err(e) => *error_dist.entry(e.to_string()).or_default() += 1,
if let Ok(report) = report.as_ref() {
*status_dist.entry(report.status).or_default() += 1;
}
all.push(report);
}
Expand Down Expand Up @@ -136,19 +134,17 @@ impl Monitor {

let mut bar_num_req = vec![0u64; count];
let short_bin = (now - self.start).as_secs_f64() % bin;
for r in all.iter().rev() {
if let Ok(r) = r.as_ref() {
let past = (now - r.end).as_secs_f64();
let i = if past <= short_bin {
0
} else {
1 + ((past - short_bin) / bin) as usize
};
if i >= bar_num_req.len() {
break;
}
bar_num_req[i] += 1;
for r in all.success.iter().rev() {
let past = (now - r.end).as_secs_f64();
let i = if past <= short_bin {
0
} else {
1 + ((past - short_bin) / bin) as usize
};
if i >= bar_num_req.len() {
break;
}
bar_num_req[i] += 1;
}

let cols = bar_num_req
Expand Down Expand Up @@ -189,7 +185,7 @@ impl Monitor {
[
Constraint::Length(3),
Constraint::Length(8),
Constraint::Length(error_dist.len() as u16 + 2),
Constraint::Length(all.error.len() as u16 + 2),
Constraint::Fill(1),
]
.as_ref(),
Expand Down Expand Up @@ -224,9 +220,9 @@ impl Monitor {
f.render_widget(gauge, row4[0]);

let last_1_timescale = all
.success
.iter()
.rev()
.filter_map(|r| r.as_ref().ok())
.take_while(|r| (now - r.end).as_secs_f64() <= timescale.as_secs_f64())
.collect::<Vec<_>>();

Expand Down Expand Up @@ -316,7 +312,7 @@ impl Monitor {
);
f.render_widget(stats2, mid[1]);

let mut error_v: Vec<(String, usize)> = error_dist.clone().into_iter().collect();
let mut error_v: Vec<(String, usize)> = all.error.clone().into_iter().collect();
error_v.sort_by_key(|t| std::cmp::Reverse(t.1));
let errors_text = error_v
.into_iter()
Expand Down Expand Up @@ -370,9 +366,9 @@ impl Monitor {
}
.max(2);
let values = all
.success
.iter()
.rev()
.filter_map(|r| r.as_ref().ok())
.take_while(|r| (now - r.end).as_secs_f64() < timescale.as_secs_f64())
.map(|r| r.duration().as_secs_f64())
.collect::<Vec<_>>();
Expand Down
Loading

0 comments on commit 1f87b19

Please sign in to comment.