use colored::Colorize;
use super::layout::{
bar, fit, fmt_ms, fmt_tick, fmt_us, grouped, pad, precise_duration, short_duration, split_bar,
trim_lines, visible_width, worker_leaf, wrap_visible, Muted,
};
use super::metrics::{
percentile_us, rate_spread, Buckets, Placement, MAX_BUCKET_ROWS, MIN_RATE_SAMPLES,
};
use super::{BenchResults, StopReason};
fn ending(results: &BenchResults) -> Option<String> {
match results.stopped {
Some(StopReason::Interrupt) => Some("stopped by ctrl-c".to_string()),
Some(StopReason::Failing) => Some("stopped: too many failures".to_string()),
None => None,
}
}
pub(crate) fn render(results: &BenchResults, width: usize) -> String {
let mut out = String::new();
let rule = width.saturating_sub(4).min(116);
out.push('\n');
out.push_str(&format!(" {}\n", "═".repeat(rule).muted()));
let ident = format!(
"{} · {} · c={}",
results.config.workload.name(),
results.config.strategy.as_str(),
results.config.concurrency
);
let version = format!("zc {}", env!("CARGO_PKG_VERSION"));
let meta = format!(
"{} {} {}",
results.broker_label(),
version,
results.started_at
);
out.push_str(&fit(
width,
&[
format!(
" {}{}{}",
pad(&"zc bench".bold().to_string(), 11, false),
pad(&ident, 32, false),
meta.muted()
),
format!(
" {}{}\n {}{}",
pad(&"zc bench".bold().to_string(), 11, false),
ident,
" ".repeat(11),
meta.muted()
),
format!(" {}\n {}\n {}", "zc bench".bold(), ident, meta.muted()),
],
));
out.push('\n');
let ending_text = ending(results);
let outcome = format!(
"{}{} · {}",
pad(
&format!("{:.1} req/s", results.rps)
.yellow()
.bold()
.to_string(),
16,
false
),
format!("{} ok", grouped(results.successful)).green(),
match results.failed {
0 => "0 failed".muted(),
n => format!("{n} failed").red().to_string(),
},
);
let did = format!(
"{}{}",
pad(&grouped(results.completed).bold().to_string(), 11, false),
format!(
"of {} requests in {}",
grouped(results.total_requests),
precise_duration(results.duration)
)
.muted(),
);
let stopped_note = match &ending_text {
Some(e) => format!(" {}", e.yellow()),
None => String::new(),
};
out.push_str(&fit(
width,
&[
format!(" {did}{stopped_note} {outcome}"),
format!(" {did}{stopped_note}\n {}{outcome}", " ".repeat(11)),
format!(
" {did}\n {}{}\n {}{outcome}",
" ".repeat(11),
ending_text.clone().unwrap_or_default().yellow(),
" ".repeat(11)
),
],
));
out.push('\n');
out.push_str(&format!(" {}\n\n", "═".repeat(rule).muted()));
summaries(results, width, &mut out);
distributions(results, width, &mut out);
where_it_ran(results, width, &mut out);
notes(results, width, &mut out);
trim_lines(&out)
}
fn summaries(results: &BenchResults, width: usize, out: &mut String) {
let lat = &results.latencies_us;
let (free_n, _) = results.ledger.totals(Placement::Free);
let (billed_n, billed) = results.ledger.totals(Placement::Billed);
let placed = results.ledger.placed().max(1);
let rates = &results.rates_per_second;
let dip = match rates.len() {
0..=2 => 0,
_ => rates[1..].iter().copied().min().unwrap_or(0),
};
let blocks: [(&str, Vec<(String, String)>); 4] = [
(
"latency ms",
vec![
("min".into(), fmt_us(lat.iter().copied().min().unwrap_or(0))),
("p50".into(), fmt_us(percentile_us(lat, 50.0))),
("p90".into(), fmt_us(percentile_us(lat, 90.0))),
("p99".into(), fmt_us(percentile_us(lat, 99.0))),
("max".into(), fmt_us(lat.iter().copied().max().unwrap_or(0))),
],
),
("throughput req/s", {
let spread = rate_spread(rates);
let dash = "—".to_string();
vec![
("mean".into(), format!("{:.1}", results.rps)),
(
"peak".into(),
match rates.iter().copied().max() {
Some(v) => v.to_string(),
None => dash.clone(),
},
),
(
"dip".into(),
match (rates.len() >= 3, dip) {
(true, d) => d.to_string(),
(false, _) => dash.clone(),
},
),
(
"stdev".into(),
match spread {
Some((_, sd, _)) => format!("{sd:.1}"),
None => dash.clone(),
},
),
(
"cv".into(),
match spread {
Some((_, _, cv)) => format!("{cv:.0}%"),
None => dash,
},
),
]
}),
(
"placement",
vec![
(
"free".into(),
format!(
"{} · {:.0}%",
grouped(free_n),
free_n as f64 / placed as f64 * 100.0
),
),
("".into(), "cost nothing".green().to_string()),
(
"billed".into(),
format!(
"{} · {:.0}%",
grouped(billed_n),
billed_n as f64 / placed as f64 * 100.0
),
),
(
"".into(),
match billed_n {
0 => "—".muted(),
_ => format!("¥{billed:.4}").yellow().to_string(),
},
),
("".into(), String::new()),
],
),
(
"spend ¥",
vec![
("spent".into(), format!("{:.4}", results.total_charged)),
("per req".into(), format!("{:.6}", results.avg_charge())),
(
"billed".into(),
format!("{} of {}", grouped(billed_n), grouped(results.completed)),
),
("".into(), String::new()),
("".into(), String::new()),
],
),
];
let per_row = match width {
0..=59 => 1,
60..=111 => 2,
_ => 4,
};
let col_w = 28;
for chunk in blocks.chunks(per_row) {
let mut head = String::from(" ");
for (title, _) in chunk {
head.push_str(&pad(&title.bold().to_string(), col_w, false));
}
out.push_str(head.trim_end());
out.push('\n');
for i in 0..5 {
let mut line = String::from(" ");
for (_, rows) in chunk {
let (k, v) = &rows[i];
line.push_str(&pad(&k.muted(), 9, false));
line.push_str(&pad(v, col_w - 9, false));
}
let line = line.trim_end().to_string();
if !line.is_empty() {
out.push_str(&line);
}
out.push('\n');
}
out.push('\n');
}
}
fn distributions(results: &BenchResults, width: usize, out: &mut String) {
let lat = Buckets::of(&results.latencies_us, MAX_BUCKET_ROWS);
let thr = Buckets::linear(&results.rates_per_second, 7);
let lat_rows: Vec<usize> = lat.interesting().collect();
let thr_rows: Vec<usize> = match results.rates_per_second.len() < MIN_RATE_SAMPLES {
true => Vec::new(),
false => thr.interesting().collect(),
};
let thr_fmt = |v: u64| format!("{v} req/s");
let lat_label_w = lat_rows
.iter()
.map(|i| visible_width(&lat.label(*i, &fmt_tick)))
.max()
.unwrap_or(8);
let thr_label_w = thr_rows
.iter()
.map(|i| visible_width(&thr.label(*i, &thr_fmt)))
.max()
.unwrap_or(0);
let left_fixed = 2 + lat_label_w + 1 + 7 + 5;
let right_fixed = thr_label_w + 1 + 5 + 5;
let two_col = width >= 104 && !thr_rows.is_empty();
let lat_bar = match two_col {
true => (width.saturating_sub(left_fixed + right_fixed + 5) * 58 / 100).min(26),
false => width.saturating_sub(left_fixed).min(30),
};
let thr_bar = match two_col {
true => width
.saturating_sub(left_fixed + right_fixed + 5 + lat_bar)
.min(22),
false => 0,
};
let mut head = format!(
" {}",
pad(
&"latency distribution".bold().to_string(),
2 + lat_label_w + lat_bar + 12 + 5,
false
)
);
if two_col {
head.push_str(&format!(
"{} {}",
"throughput distribution".bold(),
format!("{} per-second samples", results.rates_per_second.len()).muted()
));
}
out.push_str(head.trim_end());
out.push('\n');
let lat_peak = lat.peak();
let thr_peak = thr.peak();
let lat_total = lat.total().max(1);
let thr_total = thr.total().max(1);
for (row, i) in lat_rows.iter().enumerate() {
let n = lat.counts[*i];
let mut line = format!(
" {}{}{}{}",
pad(&lat.label(*i, &fmt_tick).muted(), lat_label_w + 1, false),
bar(n, lat_peak, lat_bar).cyan(),
pad(&grouped(n), 7, true),
pad(
&format!("{:.0}%", n as f64 / lat_total as f64 * 100.0)
.muted()
.to_string(),
5,
true
),
);
if two_col {
line.push_str(" ");
if let Some(j) = thr_rows.get(row) {
let tn = thr.counts[*j];
line.push_str(&format!(
"{}{}{}{}",
pad(&thr.label(*j, &thr_fmt).muted(), thr_label_w + 1, false),
bar(tn, thr_peak, thr_bar).green(),
pad(&tn.to_string(), 5, true),
pad(
&format!("{:.0}%", tn as f64 / thr_total as f64 * 100.0)
.muted()
.to_string(),
5,
true
),
));
}
}
out.push_str(line.trim_end());
out.push('\n');
}
if !two_col && !thr_rows.is_empty() {
out.push_str(&format!("\n {}\n", "throughput distribution".bold()));
for j in &thr_rows {
let tn = thr.counts[*j];
out.push_str(
format!(
" {}{}{}{}",
pad(&thr.label(*j, &thr_fmt).muted(), thr_label_w + 1, false),
bar(tn, thr_peak, lat_bar).green(),
pad(&tn.to_string(), 7, true),
pad(
&format!("{:.0}%", tn as f64 / thr_total as f64 * 100.0)
.muted()
.to_string(),
5,
true
),
)
.trim_end(),
);
out.push('\n');
}
}
out.push('\n');
}
struct Cols {
class: usize,
node: usize,
price: usize,
via: usize,
bar: usize,
req: usize,
share: usize,
p50: usize,
spend: usize,
}
impl Cols {
fn for_width(width: usize, name_w: usize) -> Cols {
let mut c = match width {
0..=79 => Cols {
class: 9,
node: name_w + 2,
price: 0,
via: 0,
bar: 0,
req: 8,
share: 0,
p50: 0,
spend: 13,
},
80..=103 => Cols {
class: 9,
node: name_w + 2,
price: 11,
via: 0,
bar: 0,
req: 8,
share: 7,
p50: 10,
spend: 13,
},
_ => Cols {
class: 10,
node: name_w + 2,
price: 11,
via: 10,
bar: 0,
req: 8,
share: 7,
p50: 11,
spend: 14,
},
};
let fixed = 2 + c.class + c.node + c.price + c.via + c.req + c.share + c.p50 + c.spend;
c.bar = width.saturating_sub(fixed).min(30);
c
}
fn cell(width: usize, text: String) -> String {
match width {
0 => String::new(),
w => pad(&text, w, true),
}
}
fn indent(&self) -> usize {
self.class + self.node + self.price + self.via
}
}
fn where_it_ran(results: &BenchResults, width: usize, out: &mut String) {
if results.ledger.is_empty() {
return;
}
out.push_str(&format!(" {}\n", "where it ran".bold()));
let rows = results.ledger.rows();
let placed = results.ledger.placed().max(1);
let busiest = rows.iter().map(|(_, _, s)| s.requests).max().unwrap_or(0);
let name_w = rows
.iter()
.map(|(_, n, _)| visible_width(n))
.max()
.unwrap_or(0)
.clamp(8, 20);
let c = Cols::for_width(width, name_w);
out.push_str(
format!(
" {}{}{}{}{}{}{}{}{}",
" ".repeat(c.class),
pad(&"node".muted(), c.node, false),
match c.price {
0 => String::new(),
w => pad(&"price".muted(), w, false),
},
match c.via {
0 => String::new(),
w => pad(&"via".muted(), w, false),
},
" ".repeat(c.bar),
Cols::cell(c.req, "req".muted()),
Cols::cell(c.share, "share".muted()),
Cols::cell(c.p50, "p50".muted()),
Cols::cell(c.spend, "spend".muted()),
)
.trim_end(),
);
out.push('\n');
for (class, node, stat) in &rows {
let tint = match class {
Placement::Free => colored::Color::Green,
Placement::Billed => colored::Color::Yellow,
};
let drawn = bar(stat.requests, busiest, c.bar);
out.push_str(
format!(
" {}{}{}{}{}{}{}{}{}",
pad(
&class.as_str().color(tint).bold().to_string(),
c.class,
false
),
pad(&node.cyan().to_string(), c.node, false),
match c.price {
0 => String::new(),
w => pad(
&match stat.priced_but_unbilled() {
true => stat.price().yellow().to_string(),
false => stat.price().muted(),
},
w,
false
),
},
match c.via {
0 => String::new(),
w => pad(&stat.via().muted(), w, false),
},
drawn.color(tint),
Cols::cell(c.req, grouped(stat.requests)),
Cols::cell(
c.share,
format!("{:.0}%", stat.requests as f64 / placed as f64 * 100.0)
.muted()
.to_string()
),
Cols::cell(c.p50, fmt_us(stat.p50_us())),
Cols::cell(
c.spend,
match class {
Placement::Free => "free".green().to_string(),
Placement::Billed => format!("¥{:.4}", stat.charged).yellow().to_string(),
}
),
)
.trim_end(),
);
out.push('\n');
let mut detail: Vec<String> = Vec::new();
if stat.workers.len() > 1 {
let mut mine: Vec<(&String, &u64)> = stat.workers.iter().collect();
mine.sort_by(|a, b| b.1.cmp(a.1).then_with(|| a.0.cmp(b.0)));
let shown: Vec<String> = mine
.iter()
.take(8)
.map(|(w, n)| format!("{} {}", worker_leaf(w), n))
.collect();
let more = match mine.len() > 8 {
true => format!(" · … {} more", mine.len() - 8),
false => String::new(),
};
detail.push(format!(
"{} workers · {}{}",
stat.workers.len(),
shown.join(" · "),
more
));
}
if let Some(expected) = stat.expectation.avg_expected_ms() {
detail.push(format!(
"router expected {} · delivered {}",
fmt_ms(expected),
fmt_ms(stat.expectation.avg_delivered_ms())
));
}
for d in detail {
let indent = 2 + c.indent();
for line in wrap_visible(&d, width.saturating_sub(indent).max(20), 0) {
out.push_str(&format!("{}{}\n", " ".repeat(indent), line.muted()));
}
}
}
let (free_n, _) = results.ledger.totals(Placement::Free);
let (billed_n, billed) = results.ledger.totals(Placement::Billed);
let summary = format!(
"{} {} {} · {} {}",
"free".muted(),
format!("{:.0}%", free_n as f64 / placed as f64 * 100.0)
.green()
.bold(),
"of the work".muted(),
"billed".muted(),
match billed_n {
0 => "0%".muted(),
_ => format!("¥{billed:.4}").yellow().bold().to_string(),
},
);
let bar_w = width
.saturating_sub(2 + 10 + 4 + visible_width(&summary))
.min(30);
out.push('\n');
if bar_w >= 8 {
let (free, billed) = split_bar(free_n, billed_n, bar_w);
out.push_str(&format!(
" {}{}{} {}\n\n",
pad(&"split".bold().to_string(), 10, false),
free.green(),
billed.yellow(),
summary
));
} else {
out.push_str(&format!(
" {}{}\n\n",
pad(&"split".bold().to_string(), 10, false),
summary
));
}
}
fn notes(results: &BenchResults, width: usize, out: &mut String) {
let mut notes: Vec<(String, String)> = Vec::new();
for (node, exp) in results.ledger.miscalibrated() {
if let (Some(e), Some(ratio)) = (exp.avg_expected_ms(), exp.ratio()) {
let (factor, direction) = match ratio < 1.0 {
true => (1.0 / ratio, "pessimistic"),
false => (ratio, "optimistic"),
};
notes.push((
"⚠".red().bold().to_string(),
format!(
"{} expected {} of {} and got {} — {} {}. Routing is choosing on that estimate; {} shows each worker's posterior.",
results.config.strategy.as_str(),
fmt_ms(e).red(),
node.cyan(),
fmt_ms(exp.avg_delivered_ms()).green(),
format!("{factor:.1}×").red(),
direction,
"zc ls".cyan(),
),
));
}
}
let (free_n, _) = results.ledger.totals(Placement::Free);
let (_, billed) = results.ledger.totals(Placement::Billed);
let placed = results.ledger.placed();
let unbilled: Vec<String> = results
.ledger
.rows()
.into_iter()
.filter(|(_, _, s)| s.priced_but_unbilled())
.map(|(_, node, s)| format!("{node} at {}", s.price()))
.collect();
if placed > 0 && billed == 0.0 {
match unbilled.is_empty() {
true => notes.push((
"○".green().bold().to_string(),
"Nothing was billed, and no worker asked to be paid.".to_string(),
)),
false => {
notes.push((
"⚠".yellow().bold().to_string(),
format!(
"Nothing was billed, but {} advertised a price: {}.",
match unbilled.len() {
1 => "one node".to_string(),
n => format!("{n} nodes"),
},
unbilled.join(", ").yellow(),
),
));
notes.push((
" ".to_string(),
"Free is correct on hardware you own. On anyone else's it is zc#297, \
and the owner of that machine was not paid."
.muted(),
));
}
}
} else if billed > 0.0 {
notes.push((
"○".yellow().bold().to_string(),
format!(
"{} {} {}",
format!("¥{billed:.4}").yellow(),
"was charged for work sent across the network.",
format!(
"{:.0}% of the work cost nothing.",
free_n as f64 / placed.max(1) as f64 * 100.0
)
.muted()
),
));
}
if let Some(pin) = results.config.effective_pin() {
let touched = results.ledger.nodes_touched();
match touched.len() {
0 => {}
1 => notes.push((
"○".green().bold().to_string(),
format!(
"All {} placed requests ran on {}, as pinned.",
grouped(placed),
touched[0].0.clone().green(),
),
)),
n => {
notes.push((
"⚠".red().bold().to_string(),
format!(
"Pinned to {} but the work ran on {} nodes: {}.",
pin.cyan(),
n.to_string().red(),
touched
.iter()
.map(|(node, reqs)| format!("{node} ×{reqs}"))
.collect::<Vec<_>>()
.join(", ")
.yellow(),
),
));
notes.push((
" ".to_string(),
format!(
"This broker's build spreads a pinned job across the mesh, so the run \
measures the mesh rather than the node. {} names each broker's build.",
"zc brokers".cyan()
)
.muted(),
));
}
}
}
if let Some((reason, count)) = results.ranked_failure_reasons().first() {
notes.push((
"⚠".red().bold().to_string(),
format!("{} {}", format!("{count}×").red(), reason.muted()),
));
}
if results.completed < results.total_requests && results.rps > 0.0 {
let full = std::time::Duration::from_secs_f64(results.total_requests as f64 / results.rps);
notes.push((
"○".muted_bold(),
format!(
"{} {}",
format!(
"{} of {} requested. At {:.1} req/s the whole run needs {}; try",
grouped(results.completed),
grouped(results.total_requests),
results.rps,
short_duration(full),
)
.muted(),
format!(
"{} for a shorter sample.",
format!("-n {}", suggest_n(results.rps)).cyan()
)
.muted(),
),
));
}
if notes.is_empty() {
return;
}
out.push_str(&format!(" {}\n", "notes".bold()));
const MARKER: usize = 5;
for (marker, body) in notes {
let room = width.saturating_sub(2 + MARKER).max(30);
for (i, line) in wrap_visible(&body, room, 0).into_iter().enumerate() {
match i {
0 => out.push_str(&format!(" {}{}\n", pad(&marker, MARKER, false), line)),
_ => out.push_str(&format!(" {}{}\n", " ".repeat(MARKER), line)),
}
}
}
out.push('\n');
}
pub(crate) fn suggest_n(rps: f64) -> u64 {
let raw = (rps * 120.0).max(100.0);
let mag = 10f64.powf(raw.log10().floor());
((raw / mag).round() * mag) as u64
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_suggested_sample_is_a_number_a_person_would_type() {
assert_eq!(suggest_n(37.6), 5000);
assert_eq!(suggest_n(0.1), 100);
assert_eq!(suggest_n(800.0), 100000);
}
}