Skip to content

Commit

Permalink
Integrate High Dynamic Range Histogram
Browse files Browse the repository at this point in the history
Instead of computing the statistical summary manually this commit makes
use of the HDR Histogram.

Additionally, the mean(s) and standard deviations are insufficient when
determining the respones behavior. Therefore, this commit also
introduces the output of the 99.x'th percentiles.
  • Loading branch information
schrieveslaach authored and fcsonline committed Dec 2, 2021
1 parent 6ac9365 commit 2ae5b48
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 28 deletions.
88 changes: 88 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ futures = "0.3.5"
lazy_static = "1.4.0"
num_cpus = "1.13.0"
rand = "0.7.3"
hdrhistogram = "7.4.0"

# Add openssl-sys as a direct dependency so it can be cross compiled to
# x86_64-unknown-linux-musl using the "vendored" feature below
Expand Down
62 changes: 34 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::actions::Report;
use clap::crate_version;
use clap::{App, Arg};
use colored::*;
use hdrhistogram::Histogram;
use linked_hash_map::LinkedHashMap;
use std::collections::HashMap;
use std::process;
Expand Down Expand Up @@ -63,34 +64,35 @@ struct DrillStats {
total_requests: usize,
successful_requests: usize,
failed_requests: usize,
mean_duration: f64,
median_duration: f64,
stdev_duration: f64,
hist: Histogram<u64>,
}

impl DrillStats {
fn mean_duration(&self) -> f64 {
self.hist.mean() / 1_000.0
}
fn median_duration(&self) -> f64 {
self.hist.value_at_quantile(0.5) as f64 / 1_000.0
}
fn stdev_duration(&self) -> f64 {
self.hist.stdev() / 1_000.0
}
fn value_at_quantile(&self, quantile: f64) -> f64 {
self.hist.value_at_quantile(quantile) as f64 / 1_000.0
}
}

fn compute_stats(sub_reports: &[Report]) -> DrillStats {
let mut hist = Histogram::<u64>::new_with_bounds(1, 60 * 60 * 1000, 2).unwrap();
let mut group_by_status = HashMap::new();

for req in sub_reports {
group_by_status.entry(req.status / 100).or_insert_with(Vec::new).push(req);
}

let mut durations = sub_reports.iter().map(|r| r.duration).collect::<Vec<f64>>();
let mean_duration = durations.iter().fold(0f64, |a, &b| a + b) / durations.len() as f64;
let deviations = durations.iter().map(|a| (mean_duration - a).powf(2.0)).collect::<Vec<f64>>();
let stdev_duration = (deviations.iter().fold(0f64, |a, &b| a + b) / durations.len() as f64).sqrt();

durations.sort_by(|a, b| a.partial_cmp(b).unwrap());
let durlen = durations.len();
let median_duration = if durlen == 0 {
f64::NAN
} else if durlen % 2 == 0 {
durations[durlen / 2]
} else if durlen > 1 {
(durations[durlen / 2] + durations[durlen / 2 + 1]) / 2f64
} else {
durations[0]
};
for r in sub_reports.iter() {
hist += (r.duration * 1_000.0) as u64;
}

let total_requests = sub_reports.len();
let successful_requests = group_by_status.entry(2).or_insert_with(Vec::new).len();
Expand All @@ -100,9 +102,7 @@ fn compute_stats(sub_reports: &[Report]) -> DrillStats {
total_requests,
successful_requests,
failed_requests,
mean_duration,
median_duration,
stdev_duration,
hist,
}
}

Expand Down Expand Up @@ -132,9 +132,12 @@ fn show_stats(list_reports: &[Vec<Report>], stats_option: bool, nanosec: bool, d
println!("{:width$} {:width2$} {}", name.green(), "Total requests".yellow(), substats.total_requests.to_string().purple(), width = 25, width2 = 25);
println!("{:width$} {:width2$} {}", name.green(), "Successful requests".yellow(), substats.successful_requests.to_string().purple(), width = 25, width2 = 25);
println!("{:width$} {:width2$} {}", name.green(), "Failed requests".yellow(), substats.failed_requests.to_string().purple(), width = 25, width2 = 25);
println!("{:width$} {:width2$} {}", name.green(), "Median time per request".yellow(), format_time(substats.median_duration, nanosec).purple(), width = 25, width2 = 25);
println!("{:width$} {:width2$} {}", name.green(), "Average time per request".yellow(), format_time(substats.mean_duration, nanosec).purple(), width = 25, width2 = 25);
println!("{:width$} {:width2$} {}", name.green(), "Sample standard deviation".yellow(), format_time(substats.stdev_duration, nanosec).purple(), width = 25, width2 = 25);
println!("{:width$} {:width2$} {}", name.green(), "Median time per request".yellow(), format_time(substats.median_duration(), nanosec).purple(), width = 25, width2 = 25);
println!("{:width$} {:width2$} {}", name.green(), "Average time per request".yellow(), format_time(substats.mean_duration(), nanosec).purple(), width = 25, width2 = 25);
println!("{:width$} {:width2$} {}", name.green(), "Sample standard deviation".yellow(), format_time(substats.stdev_duration(), nanosec).purple(), width = 25, width2 = 25);
println!("{:width$} {:width2$} {}", name.green(), "99.0'th percentile".yellow(), format_time(substats.value_at_quantile(0.99), nanosec).purple(), width = 25, width2 = 25);
println!("{:width$} {:width2$} {}", name.green(), "99.5'th percentile".yellow(), format_time(substats.value_at_quantile(0.995), nanosec).purple(), width = 25, width2 = 25);
println!("{:width$} {:width2$} {}", name.green(), "99.9'th percentile".yellow(), format_time(substats.value_at_quantile(0.999), nanosec).purple(), width = 25, width2 = 25);
}

// compute global stats
Expand All @@ -148,9 +151,12 @@ fn show_stats(list_reports: &[Vec<Report>], stats_option: bool, nanosec: bool, d
println!("{:width2$} {}", "Successful requests".yellow(), global_stats.successful_requests.to_string().purple(), width2 = 25);
println!("{:width2$} {}", "Failed requests".yellow(), global_stats.failed_requests.to_string().purple(), width2 = 25);
println!("{:width2$} {} {}", "Requests per second".yellow(), format!("{:.2}", requests_per_second).purple(), "[#/sec]".purple(), width2 = 25);
println!("{:width2$} {}", "Median time per request".yellow(), format_time(global_stats.median_duration, nanosec).purple(), width2 = 25);
println!("{:width2$} {}", "Average time per request".yellow(), format_time(global_stats.mean_duration, nanosec).purple(), width2 = 25);
println!("{:width2$} {}", "Sample standard deviation".yellow(), format_time(global_stats.stdev_duration, nanosec).purple(), width2 = 25);
println!("{:width2$} {}", "Median time per request".yellow(), format_time(global_stats.median_duration(), nanosec).purple(), width2 = 25);
println!("{:width2$} {}", "Average time per request".yellow(), format_time(global_stats.mean_duration(), nanosec).purple(), width2 = 25);
println!("{:width2$} {}", "Sample standard deviation".yellow(), format_time(global_stats.stdev_duration(), nanosec).purple(), width2 = 25);
println!("{:width2$} {}", "99.0'th percentile".yellow(), format_time(global_stats.value_at_quantile(0.99), nanosec).purple(), width2 = 25);
println!("{:width2$} {}", "99.5'th percentile".yellow(), format_time(global_stats.value_at_quantile(0.995), nanosec).purple(), width2 = 25);
println!("{:width2$} {}", "99.9'th percentile".yellow(), format_time(global_stats.value_at_quantile(0.999), nanosec).purple(), width2 = 25);
}

fn compare_benchmark(list_reports: &[Vec<Report>], compare_path_option: Option<&str>, threshold_option: Option<&str>) {
Expand Down

0 comments on commit 2ae5b48

Please sign in to comment.