//! Speed: how long a cache fetch, a cache write and a build take on average, //! and how build times spread for each language. use eframe::egui::{self, Color32, Layout, RichText, Sense, Ui}; use sccache_dash_api::{BuildTimes, View}; use crate::fmt; use crate::style; /// Every histogram is this tall, whatever it holds. const HISTOGRAM_HEIGHT: f32 = 44.0; /// The slowest build time the axis reaches, as log10 of milliseconds (1,000 s). const AXIS_TOP: f64 = 6.0; pub fn section(ui: &mut Ui, view: &View) { let timing = view.timing; style::section(ui, "speed", "Speed", |ui| { let rows = [ ("Cache fetch (a hit)", timing.avg_hit_read_ms, style::CACHE), ("Cache write", timing.avg_cache_write_ms, style::BUILT), ("Build (compile)", timing.avg_compile_ms, Color32::GRAY), ]; let label_width = if ui.available_rect_before_wrap().width() < 480.0 { 124.0 } else { 170.0 }; for (label, ms, color) in rows { ui.horizontal(|ui| { cell(ui, label_width, Layout::left_to_right(egui::Align::Center), |ui| { ui.add(egui::Label::new(label).truncate()); }); let text = ms.map_or("—".into(), |ms| fmt::duration(ms / 1000.0)); cell(ui, 72.0, Layout::right_to_left(egui::Align::Center), |ui| { ui.label(RichText::new(text).strong()); }); style::meter(ui, 12.0, ms.map_or(0.0, style::log_fraction), color); }); } let hit = timing.avg_hit_read_ms; let write = timing.avg_cache_write_ms; let build = timing.avg_compile_ms; let sentence = if let Some(times) = faster(build, hit) { format!("A cache hit is about {times}x faster than building.") } else if let Some(times) = faster(build, write) { format!("Writing to the cache is about {times}x faster than building.") } else { "There are no timings to compare in this range.".to_owned() }; ui.label(sentence); ui.label( RichText::new( "The bars use a log scale, so a fraction of a millisecond and several \ seconds are both visible.", ) .small() .weak(), ); build_times(ui, view); }); } /// `faster` is about how many times shorter than `slower`, when both are known and positive. fn faster(slower: Option, faster: Option) -> Option { let (slower, faster) = (slower?, faster?); (slower > 0.0 && faster > 0.0).then(|| fmt::round_number(slower / faster)) } /// Draw with a fixed width, so the bars all start at the same place. fn cell(ui: &mut Ui, width: f32, layout: Layout, contents: impl FnOnce(&mut Ui)) { let height = ui.text_style_height(&egui::TextStyle::Body); ui.allocate_ui_with_layout(egui::vec2(width, height), layout, contents); } /// "How long builds take, by language": one histogram per language, all on the /// same log scale of build time, with the mean, median and p90 marked. fn build_times(ui: &mut Ui, view: &View) { let languages: Vec<&BuildTimes> = view .build_times .iter() .filter(|times| times.compiles > 0 && !times.bins.is_empty()) .collect(); if languages.is_empty() { return; } ui.add_space(8.0); ui.label(RichText::new("How long builds take, by language").strong()); // One shared axis, from a quarter step before the fastest bin to a quarter step after the slowest. let (mut low, mut high) = (f64::MAX, 0.0_f64); for times in &languages { for bin in ×.bins { low = low.min(bin.from_ms); high = high.max(bin.to_ms); } } let low = (low.max(1.0).log10() - 0.25).max(0.0); let high = (high.log10() + 0.25).min(AXIS_TOP); let [_, median_color, p90_color] = style::marks(ui.visuals().dark_mode); let mean_color = ui.visuals().text_color(); for times in languages { ui.horizontal_wrapped(|ui| { ui.label(RichText::new(×.language).strong()); ui.label(format!("{} builds", fmt::count(times.compiles))); ui.add_space(6.0); ui.label(RichText::new(format!("mean {}", ms(times.mean_ms))).small().color(mean_color)); ui.label( RichText::new(format!("median {}", ms(times.median_ms))) .small() .color(median_color), ); ui.label( RichText::new(format!("p90 {}", ms(times.p90_ms))) .small() .color(p90_color), ); }); histogram( ui, times, low, high, [mean_color, median_color, p90_color], ); axis(ui, low, high); ui.add_space(4.0); } ui.add( egui::Label::new( RichText::new( "Each point is the average of the builds that finished within one 10-second reading.", ) .small() .weak(), ) .wrap(), ); } fn ms(value: f64) -> String { fmt::duration(value / 1000.0) } /// The bins as bars, build time along the bottom on a log scale, with a line /// for the mean, median and p90. fn histogram(ui: &mut Ui, times: &BuildTimes, low: f64, high: f64, colors: [Color32; 3]) { let width = ui.available_rect_before_wrap().width(); let (rect, response) = ui.allocate_exact_size(egui::vec2(width, HISTOGRAM_HEIGHT), Sense::hover()); let painter = ui.painter(); painter.rect_filled(rect, 3.0, ui.visuals().extreme_bg_color); let at = |value: f64| { let position = ((value.max(1.0).log10() - low) / (high - low)).clamp(0.0, 1.0); rect.left() + rect.width() * position as f32 }; let top = times.bins.iter().map(|bin| bin.compiles).max().unwrap_or(0); for bin in ×.bins { if bin.compiles == 0 { continue; } let height = HISTOGRAM_HEIGHT * bin.compiles as f32 / top.max(1) as f32; let bar = egui::Rect::from_min_max( egui::pos2(at(bin.from_ms), rect.bottom() - height), egui::pos2((at(bin.to_ms) - 1.0).max(at(bin.from_ms) + 1.0), rect.bottom()), ); painter.rect_filled(bar, 1.0, style::BUILT.gamma_multiply(0.75)); } for (value, color) in [times.mean_ms, times.median_ms, times.p90_ms].into_iter().zip(colors) { let x = at(value); painter.rect_filled( egui::Rect::from_min_max(egui::pos2(x, rect.top()), egui::pos2(x + 2.0, rect.bottom())), 0.0, color, ); } response.on_hover_text(format!( "{} builds: mean {}, median {}, p90 {}", fmt::count(times.compiles), ms(times.mean_ms), ms(times.median_ms), ms(times.p90_ms) )); } /// The times along the bottom of a histogram: one mark every tenfold step. fn axis(ui: &mut Ui, low: f64, high: f64) { let width = ui.available_rect_before_wrap().width(); let (rect, _) = ui.allocate_exact_size(egui::vec2(width, 14.0), Sense::hover()); let font = egui::TextStyle::Small.resolve(ui.style()); let color = ui.visuals().weak_text_color(); for step in 0..=AXIS_TOP as i64 { let value = step as f64; if value < low || value > high { continue; } let x = rect.left() + rect.width() * ((value - low) / (high - low)) as f32; ui.painter().text( egui::pos2(x, rect.center().y), egui::Align2::CENTER_CENTER, ms(10.0_f64.powf(value)), font.clone(), color, ); } }