use std::{sync::Arc, time::Duration};
use super::{
capture::{Counter, IDLE_GAP_MS, MAX_TIMELINE_PASSES, causes, fps, pass_detail, pass_of, phase_rows},
sidebar::PREVIEW_ID,
timeline::{COUNTER_HEIGHT, GPU_HEIGHT, bar_width, filmstrip_id, track_id},
*,
};
use crate::{
app::profiler::{
FrameProfile, GpuTimestampAvailability, GpuTiming, PassAllocations, PassCounters, PassSample, Phase, PhaseSpan,
ProfileSample, SampleData, producer::WindowProfiler,
},
components::Text,
};
fn presented_pass(producer: &mut WindowProfiler, frame_id: u64) {
let (started, guard) = producer.begin_pass(frame_id);
producer.finish_pass(
started,
&crate::app::PassReport {
required: true,
rendered: true,
..Default::default()
},
"none",
Some(&FrameProfile::default()),
);
drop(guard);
}
fn capture_of(state: &ProfilerState) -> Arc<ProfilerCapture> {
state.capture.clone().expect("Stop keeps the recording")
}
#[test]
fn start_and_stop_keep_every_pass_of_the_inspected_windows_and_not_devtools() {
let mut main = WindowProfiler::new();
let mut second = WindowProfiler::new();
second.attach(&main, "w1".into(), false);
second.set_open(true);
let mut devtools = WindowProfiler::new();
devtools.attach(&main, "w2".into(), true);
devtools.set_open(true);
let mut state = ProfilerState::default();
presented_pass(&mut main, 1);
assert!(state.start(&main.handle()));
assert!(state.recording());
let id = state.recording.as_ref().unwrap().id;
for frame in 2..=6 {
presented_pass(&mut main, frame);
presented_pass(&mut second, frame);
presented_pass(&mut devtools, frame);
}
state.stop();
presented_pass(&mut main, 7);
assert!(!state.recording());
let capture = capture_of(&state);
let frames = |window: &str| {
capture
.passes
.iter()
.filter(|sample| sample.window == window)
.map(|sample| pass_of(sample).unwrap().frame_id.unwrap())
.collect::<Vec<_>>()
};
assert_eq!(frames("main"), vec![2, 3, 4, 5, 6]);
assert_eq!(frames("w1"), vec![2, 3, 4, 5, 6]);
assert!(frames("w2").is_empty(), "DevTools' own window was recorded");
assert_eq!(capture.passes.len(), 10);
assert!(capture.size_bytes > 0);
for sample in &capture.passes {
let pass = pass_of(sample).unwrap();
assert!(pass.frame.is_some());
assert_eq!(phase_rows(pass)[0].name, "pass");
}
assert!(main.handle().status(id).is_err());
}
#[test]
fn include_devtools_records_devtools_windows_too() {
let main = WindowProfiler::new();
let mut devtools = WindowProfiler::new();
devtools.attach(&main, "w1".into(), true);
devtools.set_open(true);
let mut state = ProfilerState {
include_devtools: true,
..ProfilerState::default()
};
assert!(state.start(&main.handle()));
presented_pass(&mut devtools, 1);
state.stop();
assert_eq!(capture_of(&state).passes.len(), 1);
assert!(state.include_devtools);
}
#[test]
fn a_recording_on_a_tree_keeps_each_headless_pass_with_its_phases() {
use crate::{
app::{App, Tree},
components::{Column, Text},
};
let mut app = App::new();
let mut tree = Tree::new();
tree.set_root(Column::new().child(Text::new("Profiler fixture")));
let mut state = ProfilerState::default();
assert!(state.start(&tree.profiling_handle()));
let status = state.recording.as_ref().and_then(|recording| recording.status).unwrap();
assert!(status.recording.is_some());
for _ in 0..4 {
tree.pass_headless(&mut app);
}
state.refresh_status();
let status = state.recording.as_ref().and_then(|recording| recording.status).unwrap();
assert_eq!(status.completed_samples, 4);
assert!(status.recording.unwrap().size_bytes > 0);
state.stop();
let capture = capture_of(&state);
assert_eq!(capture.passes.len(), 4);
for sample in &capture.passes {
let rows = phase_rows(pass_of(sample).unwrap());
assert!(rows.iter().any(|row| row.name == "layout_update" && row.depth == 1));
}
assert_eq!(fps(capture.passes_in(capture.whole())), None);
}
thread_local! {
static TICKS: std::cell::RefCell<Option<Signal<u32>>> = const { std::cell::RefCell::new(None) };
}
struct TickLabel {
ticks: Signal<u32>,
}
impl crate::app::component::Component for TickLabel {
type Props = ();
fn create(ctx: &mut crate::app::ctx::Ctx) -> Self {
let ticks = ctx.signal(0_u32);
TICKS.with(|slot| *slot.borrow_mut() = Some(ticks.clone()));
Self { ticks }
}
fn render(&self, _ctx: &mut crate::app::ctx::Ctx) -> impl Into<Element> {
Text::new(&format!("ticks {}", self.ticks.get()))
}
}
#[test]
fn a_detailed_recording_shows_components_and_render_causes_in_frame_details() {
use crate::app::{App, Tree};
let mut app = App::new();
let mut tree = Tree::new();
tree.mount_root::<TickLabel>(&mut app, ());
tree.pass_headless(&mut app);
let ticks = TICKS.with(|slot| slot.borrow().clone().unwrap());
let mut state = ProfilerState {
detailed: true,
..ProfilerState::default()
};
assert!(state.start(&tree.profiling_handle()));
ticks.set(1);
tree.pass_headless(&mut app);
state.stop();
assert!(state.detailed);
let capture = capture_of(&state);
let sample = capture
.passes
.iter()
.find(|sample| {
phase_rows(pass_of(sample).unwrap())
.iter()
.any(|row| row.name == "TickLabel")
})
.expect("the pass that rendered the component names it");
let found = causes(&capture.updates_of(sample));
assert_eq!(found.len(), 1, "the update's cause");
assert_eq!(found[0].component, "TickLabel");
assert_eq!(found[0].source.trigger.name(), "signal");
assert_eq!(found[0].source.value_type, Some("u32"));
assert!(found[0].via.is_none());
assert!(
pass_detail(sample, pass_of(sample).unwrap())
.iter()
.any(|section| section.title == "COMPONENT LAYOUTS")
);
let mut plain = ProfilerState::default();
assert!(plain.start(&tree.profiling_handle()));
ticks.set(2);
tree.pass_headless(&mut app);
plain.stop();
let capture = capture_of(&plain);
for sample in &capture.passes {
assert!(
phase_rows(pass_of(sample).unwrap())
.iter()
.all(|row| row.name != "TickLabel")
);
assert!(causes(&capture.updates_of(sample)).is_empty());
}
}
fn drawn(capture: &ProfilerCapture) -> impl PartialEq + std::fmt::Debug + use<> {
use crate::app::profiler::{SpanTrack, forest_of};
let times = |samples: &[Arc<ProfileSample>]| {
samples
.iter()
.map(|sample| {
(
sample.sequence,
sample.window.clone(),
sample.started_ms,
sample.completed_ms,
)
})
.collect::<Vec<_>>()
};
let whole = capture.whole();
let forest = forest_of(capture.passes_in(whole), capture.scopes_in(whole));
(
(
whole,
capture.default_range(),
capture.size_bytes,
capture.boundary_excluded,
),
times(&capture.passes),
times(&capture.markers),
capture
.scopes
.iter()
.map(|scope| (scope.scope.label, scope.scope.started_ms, scope.wall))
.collect::<Vec<_>>(),
SpanTrack::ALL.map(|track| forest.call_tree(track)),
capture
.passes
.iter()
.map(|sample| phase_rows(pass_of(sample).unwrap()))
.collect::<Vec<_>>(),
)
}
#[test]
fn a_saved_recording_loads_back_into_the_same_timeline_and_call_tree() {
use crate::{
app::{App, Tree},
components::{Column, Text},
};
let mut app = App::new();
let mut tree = Tree::new();
tree.set_root(Column::new().child(Text::new("Profiler fixture")));
let mut state = ProfilerState::default();
assert!(state.start(&tree.profiling_handle()));
for _ in 0..3 {
tree.pass_headless(&mut app);
}
state.stop();
let saved = state
.report
.as_ref()
.expect("Stop keeps the report")
.to_recording_file();
let mut loaded = ProfilerState::default();
loaded.load(ProfileReport::from_recording_file(&saved).map_err(|error| error.to_string()));
assert!(loaded.error.is_none());
assert_eq!(drawn(&capture_of(&loaded)), drawn(&capture_of(&state)));
assert_eq!(capture_of(&loaded).passes.len(), 3);
assert_eq!(loaded.range, state.range);
assert_eq!(loaded.selected, state.selected);
assert_eq!(loaded.report.as_ref().unwrap().to_recording_file(), saved);
tab_tree(Signal::new(loaded.clone()), 1440, 860);
loaded.load(Err("Load failed: not a lurq recording".to_owned()));
assert_eq!(drawn(&capture_of(&loaded)), drawn(&capture_of(&state)));
assert_eq!(loaded.error.as_deref(), Some("Load failed: not a lurq recording"));
tab_tree(Signal::new(loaded.clone()), 1440, 860);
assert!(loaded.start(&tree.profiling_handle()));
loaded.load(ProfileReport::from_recording_file(&saved).map_err(|error| error.to_string()));
assert!(loaded.capture.is_none());
assert!(loaded.recording());
assert_eq!(loaded.error.as_deref(), Some(LOAD_WHILE_RECORDING));
let (_app, recording_tab) = tab_tree(Signal::new(loaded.clone()), 1440, 860);
assert!(has_text(&recording_tab, LOAD_WHILE_RECORDING));
loaded.stop();
assert!(loaded.error.is_none());
assert!(loaded.capture.is_some());
}
fn has_text(tree: &crate::app::Tree, text: &str) -> bool {
fn find(element: crate::node::ElementRef<'_>, text: &str) -> bool {
element.text_content() == Some(text) || element.children().iter().any(|child| find(child, text))
}
tree.root().is_some_and(|root| find(root, text))
}
#[test]
fn clear_ends_a_running_recording() {
let main = WindowProfiler::new();
let handle = main.handle();
let mut state = ProfilerState::default();
assert!(state.start(&handle));
let id = state.recording.as_ref().unwrap().id;
state.clear();
assert!(!state.recording());
assert!(state.capture.is_none());
assert!(handle.status(id).is_err());
}
fn presented_pass_with_screenshot(producer: &mut WindowProfiler, frame_id: u64) -> bool {
use crate::app::render_engine::{CapturedFrame, RenderCaptureTarget};
let (started, guard) = producer.begin_pass(frame_id);
let target = producer.screenshot_target();
let taken = target.is_some();
if let Some(RenderCaptureTarget::Bytes(deliver)) = target {
deliver(Ok(CapturedFrame {
width: 8,
height: 4,
rgba: vec![frame_id as u8; 8 * 4 * 4],
}));
}
producer.finish_pass(
started,
&crate::app::PassReport {
required: true,
rendered: true,
..Default::default()
},
"none",
Some(&FrameProfile::default()),
);
drop(guard);
taken
}
#[test]
fn the_screenshots_toggle_records_a_filmstrip_of_its_frames() {
let mut main = WindowProfiler::new();
let handle = main.handle();
let mut state = ProfilerState::default();
assert!(!state.screenshots);
assert!(state.start(&handle));
assert!(!presented_pass_with_screenshot(&mut main, 1));
state.stop();
assert!(capture_of(&state).screenshots.is_empty());
state.screenshots = true;
assert!(state.start(&handle));
assert!(presented_pass_with_screenshot(&mut main, 2));
assert!(!presented_pass_with_screenshot(&mut main, 3));
state.stop();
assert!(state.screenshots, "the toggle holds for the next recording");
let capture = capture_of(&state);
let pass = |frame| {
capture
.passes
.iter()
.find(|sample| pass_of(sample).unwrap().frame_id == Some(frame))
.unwrap()
.clone()
};
let (second, third) = (pass(2), pass(3));
assert!(pass_of(&second).unwrap().screenshot);
assert!(!pass_of(&third).unwrap().screenshot);
let [shot] = &capture.screenshots[..] else {
panic!("{} screenshots", capture.screenshots.len());
};
assert_eq!(shot.sequence, Some(second.sequence));
assert_eq!(shot.at_ms, second.started_ms);
assert_eq!((shot.screenshot.width, shot.screenshot.height), (8, 4));
assert_eq!(
capture.screenshot_for(third.sequence).and_then(|shot| shot.sequence),
Some(second.sequence)
);
let signal = Signal::new(ProfilerState {
selected: Some(third.sequence),
..state
});
let (mut app, mut tree) = tab_tree(signal.clone(), 1440, 860);
assert!(tree.get_element_by_id(PREVIEW_ID).is_some());
let thumbnail = filmstrip_id(0);
let thumbnail = tree
.find_element(|element: crate::node::ElementRef<'_>| element.id() == Some(thumbnail.as_str()))
.map(|element| element.bounds())
.expect("the filmstrip's thumbnail");
let (x, y) = (
thumbnail.x + thumbnail.width / 2.0,
thumbnail.y + thumbnail.height / 2.0,
);
tree.mouse_move(x, y);
tree.pass_headless(&mut app);
assert_eq!(signal.get_untracked().hovered_screenshot, Some(0));
tree.mouse_down(x, y, crate::app::events::MouseButton::Left);
tree.mouse_up(x, y, crate::app::events::MouseButton::Left);
tree.pass_headless(&mut app);
assert_eq!(signal.get_untracked().selected, Some(second.sequence));
}
fn sample(sequence: u64, window: &str, started_ms: f64, total_ms: f64, rendered: bool) -> Arc<ProfileSample> {
Arc::new(ProfileSample {
sequence,
window: window.into(),
started_ms,
completed_ms: started_ms + total_ms,
data: SampleData::Pass(PassSample {
frame_id: rendered.then_some(sequence),
rendered,
cached_render_list: false,
total: Duration::from_secs_f64(total_ms / 1000.0),
layout_update: Duration::ZERO,
layout_compute: Duration::ZERO,
component_after_layout: Duration::ZERO,
layout_recalculated: false,
canvas_recording: Duration::ZERO,
canvas_preparation: Duration::ZERO,
canvas_text: None,
backend: "none",
frame: rendered.then(FrameProfile::default),
spans: Vec::new(),
dropped_spans: 0,
reasons: crate::app::PassReasons::default(),
present: None,
counters: None,
screenshot: false,
gpu: None,
component_layouts: Vec::new(),
}),
})
}
fn capture(passes: Vec<Arc<ProfileSample>>, ended_ms: f64) -> ProfilerCapture {
ProfilerCapture {
started_ms: 0.0,
ended_ms,
passes,
markers: Vec::new(),
scopes: Vec::new(),
dropped_scopes: 0,
screenshots: Vec::new(),
size_bytes: 0,
stopped_at_limit: false,
boundary_excluded: 0,
gpu_timestamps: GpuTimestampAvailability::default(),
}
}
fn with_gpu(sample: Arc<ProfileSample>, gpu: GpuTiming) -> Arc<ProfileSample> {
let mut sample = (*sample).clone();
if let SampleData::Pass(pass) = &mut sample.data {
pass.gpu = Some(gpu);
}
Arc::new(sample)
}
#[test]
fn fps_comes_from_intervals_between_presented_frames() {
let passes: Vec<_> = (0..10)
.map(|index| sample(index, "main", index as f64 * 20.0, 2.0, true))
.collect();
let rate = fps(&passes).unwrap();
assert!((rate - 50.0).abs() < 1e-6, "{rate}");
let mut with_unpresented = passes.clone();
with_unpresented.push(sample(100, "main", 5.0, 1.0, false));
assert!((fps(&with_unpresented).unwrap() - 50.0).abs() < 1e-6);
let mut two_windows = passes;
two_windows.push(sample(200, "w1", 0.0, 1.0, true));
two_windows.push(sample(201, "w1", 10.0, 1.0, true));
assert!((fps(&two_windows).unwrap() - 50.0).abs() < 1e-6);
}
#[test]
fn fps_is_blank_when_the_app_is_idle() {
assert_eq!(fps(&[]), None);
assert_eq!(fps(&[sample(1, "main", 0.0, 2.0, true)]), None);
let idle = [
sample(1, "main", 0.0, 2.0, true),
sample(2, "main", IDLE_GAP_MS * 2.0, 2.0, true),
];
assert_eq!(fps(&idle), None);
let bursts = [
sample(1, "main", 0.0, 2.0, true),
sample(2, "main", 10.0, 2.0, true),
sample(3, "main", 5000.0, 2.0, true),
sample(4, "main", 5010.0, 2.0, true),
];
assert!((fps(&bursts).unwrap() - 100.0).abs() < 1e-6);
}
#[test]
fn ranges_select_passes_by_start_and_keep_a_selection_inside() {
let passes: Vec<_> = (0..10)
.map(|index| sample(index + 1, "main", index as f64 * 100.0 + 30.0, 1.0 + index as f64, true))
.collect();
let capture = capture(passes, 1000.0);
assert_eq!(capture.default_range(), capture.whole());
let first_half = TimeRange {
start_ms: 0.0,
end_ms: 500.0,
};
let sequences = |range| {
capture
.passes_in(range)
.iter()
.map(|sample| sample.sequence)
.collect::<Vec<_>>()
};
assert_eq!(sequences(first_half), vec![1, 2, 3, 4, 5]);
assert_eq!(capture.slowest_in(first_half), Some(5));
assert_eq!(capture.slowest_in(capture.whole()), Some(10));
let bucket_ms = 1000.0 / OVERVIEW_BUCKETS as f64;
let bucket_of_pass_5 = (430.0 / bucket_ms) as usize;
let mut state = ProfilerState {
capture: Some(Arc::new(capture)),
selected: Some(10),
..ProfilerState::default()
};
state.press_bucket(bucket_of_pass_5, false);
assert_eq!(state.selected, Some(5));
assert!(!state.drag_changes_range(bucket_of_pass_5));
assert!(state.drag_changes_range(0));
state.drag_bucket(0);
let range = state.range.unwrap();
assert_eq!(range.start_ms, 0.0);
assert!((range.end_ms - bucket_ms * (bucket_of_pass_5 + 1) as f64).abs() < 1e-9);
assert_eq!(sequences_of(&state, range), vec![1, 2, 3, 4, 5]);
assert_eq!(state.selected, Some(5));
state.range_dragging = false;
state.drag_bucket(OVERVIEW_BUCKETS - 1);
assert_eq!(state.range, Some(range));
state.press_bucket(OVERVIEW_BUCKETS - 1, true);
let range = state.range.unwrap();
assert!((range.start_ms - bucket_ms * bucket_of_pass_5 as f64).abs() < 1e-9);
assert_eq!(range.end_ms, 1000.0);
assert_eq!(sequences_of(&state, range), vec![5, 6, 7, 8, 9, 10]);
assert_eq!(state.selected, Some(5));
}
fn sequences_of(state: &ProfilerState, range: TimeRange) -> Vec<u64> {
capture_of(state)
.passes_in(range)
.iter()
.map(|sample| sample.sequence)
.collect()
}
#[test]
fn a_long_recording_opens_on_its_last_passes() {
let count = MAX_TIMELINE_PASSES + 10;
let passes: Vec<_> = (0..count)
.map(|index| sample(index as u64 + 1, "main", index as f64 * 16.0, 1.0, true))
.collect();
let capture = capture(passes, count as f64 * 16.0);
let range = capture.default_range();
assert_eq!(capture.passes_in(range).len(), MAX_TIMELINE_PASSES);
assert_eq!(range.end_ms, capture.ended_ms);
}
#[test]
fn every_stacked_pass_bar_takes_its_own_click() {
use crate::app::events::MouseButton;
let passes: Vec<_> = [100.0, 500.0, 900.0]
.into_iter()
.enumerate()
.map(|(index, start)| sample(index as u64 + 1, "main", start, 30.0, true))
.collect();
let capture = capture(passes, 1000.0);
let range = capture.whole();
let state = Signal::new(ProfilerState {
capture: Some(Arc::new(capture)),
range: Some(range),
..ProfilerState::default()
});
let (mut app, mut tree) = tab_tree(state.clone(), 1440, 860);
let chart = bounds_under(&mut tree, &track_id("passes"), &[1]);
let mut click = |x: f32| {
let y = chart.y + chart.height - 2.0;
tree.mouse_down(x, y, MouseButton::Left);
tree.mouse_up(x, y, MouseButton::Left);
tree.pass_headless(&mut app);
};
click(chart.x + chart.width * 0.3);
assert_eq!(state.get_untracked().selected, None);
let bar = bar_width(3);
let scale = chart.width - bar;
for (sequence, fraction) in [(1, 0.1), (2, 0.5), (3, 0.9)] {
click(chart.x + scale * fraction + bar / 2.0);
assert_eq!(state.get_untracked().selected, Some(sequence));
}
}
fn node_under(tree: &mut crate::app::Tree, id: &str, path: &[usize]) -> crate::core::NodeId {
let mut element = tree.get_element_by_id(id).unwrap_or_else(|| panic!("no element {id}"));
for &index in path {
element = element.children().iter().nth(index).expect("a child at the path");
}
element.node_id()
}
fn bounds_under(tree: &mut crate::app::Tree, id: &str, path: &[usize]) -> crate::core::ElementRect {
let node = node_under(tree, id, path);
tree
.find_element(|element: crate::node::ElementRef<'_>| element.node_id() == node)
.expect("laid out")
.bounds()
}
#[test]
fn bars_markers_and_axis_ticks_share_one_time_scale() {
use crate::app::profiler::InputDispatchSample;
let gpu = GpuTiming {
ui: Duration::from_millis(4),
canvas: Some(Duration::from_millis(2)),
};
let passes = vec![
with_gpu(with_nodes(sample(1, "main", 500.0, 30.0, true), 10), gpu),
with_nodes(sample(2, "main", 1000.0, 30.0, true), 20),
];
let mut capture = capture(passes, 1000.0);
capture.markers = vec![Arc::new(ProfileSample {
sequence: 3,
window: "main".into(),
started_ms: 500.0,
completed_ms: 501.0,
data: SampleData::InputDispatch(InputDispatchSample {
frame_id: None,
kind: crate::app::profiler::InputKind::Pointer,
total: Duration::from_millis(1),
interaction_id: 1,
queue_delay: Duration::ZERO,
presented_frame_id: None,
input_to_present: None,
}),
})];
let range = capture.whole();
let (_app, mut tree) = tab_tree(
Signal::new(ProfilerState {
capture: Some(Arc::new(capture)),
range: Some(range),
..ProfilerState::default()
}),
1440,
860,
);
let body = bounds_under(&mut tree, &track_id("passes"), &[1]);
let half_bar = bounds_under(&mut tree, &track_id("passes"), &[1, 1, 1]);
let end_bar = bounds_under(&mut tree, &track_id("passes"), &[1, 2, 1]);
let half_gpu_bar = bounds_under(&mut tree, &track_id("gpu"), &[1, 0, 1]);
let half_nodes = bounds_under(&mut tree, &track_id("nodes"), &[1, 0, 1]);
let end_nodes = bounds_under(&mut tree, &track_id("nodes"), &[1, 1, 1]);
let half_marker = bounds_under(&mut tree, &track_id("input"), &[1, 1]);
let half_tick = bounds_under(&mut tree, "devtools-profiler-axis", &[2]);
let last_part = bounds_under(&mut tree, "devtools-profiler-axis", &[3]);
let close = |a: f32, b: f32, what: &str| assert!((a - b).abs() < 0.5, "{what}: {a} vs {b}");
let bar = bar_width(2);
let scale = body.width - bar;
close(half_bar.width, bar, "a bar as wide as two passes have it");
close(half_bar.x - body.x, scale * 0.5, "bar at half the range");
close(half_gpu_bar.x, half_bar.x, "GPU bar at the pass's time");
close(
half_gpu_bar.height,
(6.0 / FRAME_BUDGET_MS) as f32 * GPU_HEIGHT,
"GPU bar height",
);
close(half_nodes.x, half_bar.x, "counter bar at the pass bar's time");
close(end_nodes.x, end_bar.x, "last counter bar at the last pass bar");
close(
half_nodes.height * 2.0,
end_nodes.height,
"counter bars scaled by value",
);
close(end_nodes.height, COUNTER_HEIGHT, "the highest value fills the track");
close(half_marker.x, half_bar.x, "marker at the bar's time");
close(end_bar.x - body.x, scale, "bar at the range's end");
close(half_tick.x, half_bar.x, "tick at the bar's time");
close(last_part.x + last_part.width, end_bar.x, "axis end at the last bar");
}
fn with_nodes(sample: Arc<ProfileSample>, nodes: usize) -> Arc<ProfileSample> {
let mut sample = (*sample).clone();
if let SampleData::Pass(pass) = &mut sample.data {
pass.counters = Some(PassCounters {
nodes,
..PassCounters::default()
});
}
Arc::new(sample)
}
#[test]
fn counter_tracks_take_values_per_pass_and_mark_missing_allocations_unavailable() {
let passes = vec![
with_nodes(sample(1, "main", 0.0, 2.0, true), 5),
with_nodes(sample(2, "main", 20.0, 2.0, true), 6),
with_nodes(sample(3, "main", 40.0, 2.0, false), 7),
with_nodes(sample(4, "main", 40.0 + IDLE_GAP_MS * 2.0, 2.0, true), 8),
];
let values = |counter| {
capture::counter_points(counter, &passes).map(|points| {
points
.iter()
.map(|point| (point.sequence, point.value))
.collect::<Vec<_>>()
})
};
assert_eq!(values(Counter::Fps), Some(vec![(2, 50.0)]));
assert_eq!(
values(Counter::Nodes),
Some(vec![(1, 5.0), (2, 6.0), (3, 7.0), (4, 8.0)])
);
assert_eq!(values(Counter::Memory).map(|points| points.len()), Some(4));
assert_eq!(values(Counter::Allocations), None);
let mut counted = (*passes[0]).clone();
if let SampleData::Pass(pass) = &mut counted.data {
pass.counters.as_mut().unwrap().allocations = Some(PassAllocations {
allocations: 12,
..PassAllocations::default()
});
}
assert_eq!(
capture::counter_points(Counter::Allocations, &[Arc::new(counted)]).map(|points| points[0].value),
Some(12.0)
);
assert_eq!(
capture::counter_points(Counter::Nodes, &[sample(1, "main", 0.0, 2.0, true)]),
None
);
let detail = pass_detail(&passes[0], pass_of(&passes[0]).unwrap());
let counters = detail.iter().find(|section| section.title == "COUNTERS").unwrap();
assert!(counters.rows.contains(&("nodes", "5".to_owned())));
assert!(counters.rows.contains(&("allocations", "unavailable".to_owned())));
}
struct TabHost {
tab: ProfilerTab,
}
#[derive(Clone)]
struct TabHostProps {
state: Signal<ProfilerState>,
}
impl PartialEq for TabHostProps {
fn eq(&self, other: &Self) -> bool {
self.state.id() == other.state.id()
}
}
impl crate::app::component::DevtoolsInspectable for TabHostProps {
fn write_info(&self, _buffer: &mut Vec<crate::app::component::ComponentInfo>) {}
}
impl crate::app::component::Component for TabHost {
type Props = TabHostProps;
fn create(ctx: &mut crate::app::ctx::Ctx) -> Self {
Self {
tab: ProfilerTab::new(ctx),
}
}
fn render(&self, ctx: &mut crate::app::ctx::Ctx) -> impl Into<Element> {
let tab = ProfilerTab {
state: ctx.props::<TabHostProps>().state.clone(),
..self.tab.clone()
};
view(ctx, &tab, None)
}
}
fn tab_tree(state: Signal<ProfilerState>, width: u32, height: u32) -> (crate::app::App, crate::app::Tree) {
let mut app = crate::app::App::new();
let mut tree = crate::app::Tree::new();
tree.resize(width, height);
tree.mount_root::<TabHost>(&mut app, TabHostProps { state });
tree.pass_headless(&mut app);
tree.pass_headless(&mut app);
(app, tree)
}
#[test]
fn the_gpu_track_says_why_no_gpu_time_was_measured() {
let passes = vec![sample(1, "main", 500.0, 30.0, true)];
let mut capture = capture(passes, 1000.0);
capture.gpu_timestamps = GpuTimestampAvailability::unavailable(GpuTimestampAvailability::NATIVE_DX12);
assert_eq!(capture.gpu_missing_reason(), GpuTimestampAvailability::NATIVE_DX12);
let range = capture.whole();
let (_app, mut tree) = tab_tree(
Signal::new(ProfilerState {
capture: Some(Arc::new(capture)),
range: Some(range),
..ProfilerState::default()
}),
1440,
860,
);
let track = tree.get_element_by_id(&track_id("gpu")).expect("the GPU track");
fn says(element: crate::node::ElementRef<'_>, what: &str) -> bool {
element.text_content().is_some_and(|text| text.contains(what))
|| element.children().iter().any(|child| says(child, what))
}
assert!(
says(track, GpuTimestampAvailability::NATIVE_DX12),
"the track names the reason"
);
let mut measuring = self::capture(vec![sample(1, "main", 500.0, 30.0, true)], 1000.0);
measuring.gpu_timestamps = GpuTimestampAvailability {
available: true,
reason: None,
adapter_backend: Some("vulkan"),
};
assert_eq!(measuring.gpu_missing_reason(), "its GPU result never arrived");
}
#[test]
fn the_tab_lays_out_in_every_state() {
let lay_out = |state: ProfilerState| {
tab_tree(Signal::new(state), 1440, 860);
};
lay_out(ProfilerState::default());
lay_out(ProfilerState {
error: Some("profiling requires the perf_profile Cargo feature".to_owned()),
..ProfilerState::default()
});
let main = WindowProfiler::new();
let mut recording = ProfilerState::default();
assert!(recording.start(&main.handle()));
lay_out(recording.clone());
recording.clear();
let mut presented = match &sample(2, "main", 400.0, 40.0, true).data {
SampleData::Pass(pass) => pass.clone(),
_ => unreachable!(),
};
presented.spans = vec![
PhaseSpan {
phase: Phase::PassSetup,
depth: 0,
start_ns: 400_000_000,
end_ns: 440_000_000,
component: None,
},
PhaseSpan {
phase: Phase::LayoutUpdate,
depth: 1,
start_ns: 401_000_000,
end_ns: 420_000_000,
component: None,
},
];
let mut with_spans = (*sample(2, "main", 400.0, 40.0, true)).clone();
with_spans.data = SampleData::Pass(presented);
let capture = capture(
vec![
sample(1, "main", 100.0, 4.0, true),
Arc::new(with_spans),
sample(3, "main", 700.0, 20.0, false),
],
1000.0,
);
let range = capture.whole();
lay_out(ProfilerState {
capture: Some(Arc::new(capture)),
range: Some(range),
selected: Some(2),
..ProfilerState::default()
});
}
fn element_count(tree: &crate::app::Tree) -> usize {
fn count(element: crate::node::ElementRef<'_>) -> usize {
1 + element.children().iter().map(count).sum::<usize>()
}
tree.root().map_or(0, count)
}
fn timed(times: usize, mut run: impl FnMut(usize)) -> Vec<f64> {
(0..times)
.map(|index| {
let started = std::time::Instant::now();
run(index);
started.elapsed().as_secs_f64() * 1000.0
})
.collect()
}
fn median_and_max(mut times: Vec<f64>) -> (f64, f64) {
times.sort_by(f64::total_cmp);
(times[times.len() / 2], times[times.len() - 1])
}
#[test]
fn a_recording_of_2000_frames_stays_interactive() {
use super::{
analysis::{ROW_HEIGHT, TRACK_HEADER_HEIGHT, tests::synthetic_capture},
flame::{CHART_ID, TOOLTIP_ID},
};
use crate::app::events::{MouseButton, ScrollPhase};
let capture = Arc::new(synthetic_capture(2000, 20));
let whole = capture.whole();
let signal = Signal::new(ProfilerState {
selected: capture.slowest_in(whole),
range: Some(whole),
capture: Some(capture.clone()),
..ProfilerState::default()
});
let started = std::time::Instant::now();
let (mut app, mut tree) = tab_tree(signal.clone(), 1440, 860);
let first_ms = started.elapsed().as_secs_f64() * 1000.0;
let bounds = |tree: &mut crate::app::Tree, id: &str| {
tree
.find_element(|element: crate::node::ElementRef<'_>| element.id() == Some(id))
.map(|element| element.bounds())
};
let chart = bounds(&mut tree, CHART_ID).expect("the flame chart");
assert!(chart.width > 600.0, "{chart:?}");
let elements = element_count(&tree);
let chart_blocks = tree
.get_element_by_id(CHART_ID)
.map(|chart| chart.children().len())
.unwrap();
assert!(chart_blocks < 2000, "{chart_blocks} chart elements");
let y = chart.y + TRACK_HEADER_HEIGHT + ROW_HEIGHT * 1.5;
let hover = timed(120, |step| {
tree.mouse_move(chart.x + chart.width * (step as f32 + 0.5) / 120.0, y);
tree.pass_headless(&mut app);
});
assert!(bounds(&mut tree, TOOLTIP_ID).is_some(), "hovering shows the tooltip");
let x = chart.x + chart.width * 0.5;
let zoom = timed(15, |_| {
tree.scroll(x, y, 0.0, 120.0, ScrollPhase::Scroll);
tree.pass_headless(&mut app);
});
tree.mouse_down(x, y, MouseButton::Left);
let pan = timed(20, |step| {
tree.mouse_move(x - 10.0 * (step as f32 + 1.0), y);
tree.pass_headless(&mut app);
});
tree.mouse_up(x - 200.0, y, MouseButton::Left);
let mut switch = |label: &str| {
let segment = tree
.find_element(|element: crate::node::ElementRef<'_>| element.text_content() == Some(label))
.map(|element| element.bounds())
.unwrap_or_else(|| panic!("no segment {label}"));
let started = std::time::Instant::now();
tree.mouse_down(segment.x + 4.0, segment.y + 4.0, MouseButton::Left);
tree.mouse_up(segment.x + 4.0, segment.y + 4.0, MouseButton::Left);
tree.pass_headless(&mut app);
started.elapsed().as_secs_f64() * 1000.0
};
let call_tree_ms = switch("Call tree");
let bottom_up_ms = switch("Bottom-up");
let back_ms = switch("Call tree");
assert!(
tree
.find_element(|element: crate::node::ElementRef<'_>| element.text_content() == Some("pass"))
.is_some()
);
let (hover_median, hover_max) = median_and_max(hover);
let (zoom_median, zoom_max) = median_and_max(zoom);
let (pan_median, pan_max) = median_and_max(pan);
eprintln!(
"2000 frames, {} spans, {elements} elements ({chart_blocks} in the chart): first passes {first_ms:.1} ms; \
hover median {hover_median:.2} ms max {hover_max:.2}; zoom median {zoom_median:.2} max {zoom_max:.2}; pan \
median {pan_median:.2} max {pan_max:.2}; call tree {call_tree_ms:.1} ms, bottom-up {bottom_up_ms:.1} ms, call \
tree again {back_ms:.1} ms",
capture
.passes
.iter()
.map(|sample| pass_of(sample).unwrap().spans.len())
.sum::<usize>(),
);
}
#[test]
fn phase_rows_place_spans_on_the_pass_time_axis() {
let mut pass = match &sample(1, "main", 0.0, 4.0, true).data {
SampleData::Pass(pass) => pass.clone(),
_ => unreachable!(),
};
let span = |phase, depth, start_ns, end_ns| PhaseSpan {
phase,
depth,
start_ns,
end_ns,
component: None,
};
pass.spans = vec![
span(Phase::PassSetup, 0, 10_000_000, 14_000_000),
span(Phase::LayoutUpdate, 1, 10_500_000, 12_000_000),
span(Phase::LayoutCompute, 2, 11_000_000, 11_500_000),
];
let rows = phase_rows(&pass);
assert_eq!(
rows.iter().map(|row| (row.name, row.depth)).collect::<Vec<_>>(),
vec![("pass", 0), ("layout_update", 1), ("layout_compute", 2)]
);
assert_eq!(rows[0].offset_ms, 0.0);
assert!((rows[0].duration_ms - 4.0).abs() < 1e-9);
assert!((rows[1].offset_ms - 0.5).abs() < 1e-9);
assert!((rows[2].duration_ms - 0.5).abs() < 1e-9);
}
#[test]
fn pass_detail_shows_the_whole_frame_profile() {
let presented = sample(1, "main", 0.0, 4.0, true);
let titles = |sample: &ProfileSample| {
pass_detail(sample, pass_of(sample).unwrap())
.into_iter()
.map(|section| section.title)
.collect::<Vec<_>>()
};
assert_eq!(
titles(&presented),
vec!["PASS", "FRAME", "COUNTS", "RENDER", "CANVAS", "TEXT", "MEMORY"]
);
assert_eq!(titles(&sample(2, "main", 0.0, 4.0, false)), vec!["PASS"]);
let detail = pass_detail(&presented, pass_of(&presented).unwrap());
let row = |title: &str, label: &str| {
detail
.iter()
.find(|section| section.title == title)
.and_then(|section| section.rows.iter().find(|(name, _)| *name == label))
.map(|(_, value)| value.clone())
};
assert_eq!(row("PASS", "total").as_deref(), Some("4.00ms"));
assert_eq!(row("COUNTS", "quad_count").as_deref(), Some("0"));
assert_eq!(row("TEXT", "shape_text").as_deref(), Some("0.00ms"));
assert_eq!(row("MEMORY", "total_bytes").as_deref(), Some("0"));
}
#[test]
fn pass_detail_shows_gpu_time_once_it_arrived() {
let measured = with_gpu(
sample(1, "main", 0.0, 4.0, true),
GpuTiming {
ui: Duration::from_micros(1500),
canvas: None,
},
);
let detail = pass_detail(&measured, pass_of(&measured).unwrap());
assert_eq!(detail[1].title, "GPU");
assert_eq!(
detail[1].rows,
vec![
("ui", "1.50ms".to_owned()),
("canvas", "-".to_owned()),
("total", "1.50ms".to_owned())
]
);
}
fn with_present(sample: Arc<ProfileSample>, missed_vsyncs: u32) -> Arc<ProfileSample> {
let mut sample = (*sample).clone();
if let SampleData::Pass(pass) = &mut sample.data {
pass.reasons.redraw_requested = true;
pass.present = Some(crate::app::profiler::PresentTiming {
presented_ms: sample.completed_ms,
interval: Some(Duration::from_millis(16 * (u64::from(missed_vsyncs) + 1))),
refresh_interval: Some(Duration::from_millis(16)),
continuous: true,
missed_vsyncs,
});
}
Arc::new(sample)
}
fn input_marker(sequence: u64, started_ms: f64, frame: Option<(u64, f64)>) -> Arc<ProfileSample> {
Arc::new(ProfileSample {
sequence,
window: "main".into(),
started_ms,
completed_ms: started_ms + 1.0,
data: SampleData::InputDispatch(crate::app::profiler::InputDispatchSample {
frame_id: None,
kind: crate::app::profiler::InputKind::Pointer,
total: Duration::from_micros(500),
interaction_id: sequence,
queue_delay: Duration::from_micros(250),
presented_frame_id: frame.map(|(frame_id, _)| frame_id),
input_to_present: frame.map(|(_, latency_ms)| Duration::from_secs_f64(latency_ms / 1000.0)),
}),
})
}
#[test]
fn missed_frames_and_input_latencies_fill_their_lanes_and_the_frame_details() {
let passes = vec![
with_present(sample(1, "main", 100.0, 4.0, true), 0),
with_present(sample(2, "main", 600.0, 40.0, true), 2),
];
let mut capture = capture(passes, 1000.0);
capture.markers = vec![
input_marker(10, 550.0, Some((2, 90.0))),
input_marker(11, 560.0, Some((2, 80.0))),
input_marker(12, 900.0, None),
];
let range = capture.whole();
let (missed, count) = capture.missed_vsync_buckets(range, 10);
assert_eq!(count, 1);
let marked: Vec<_> = missed
.iter()
.enumerate()
.filter_map(|(bucket, missed)| missed.then_some(bucket))
.collect();
assert_eq!(marked, vec![6], "only the bucket where frame 2 starts");
let inputs = capture.input_buckets(range, 10);
assert_eq!(inputs[5].inputs, 2);
assert!((inputs[5].max_latency_ms.unwrap() - 90.0).abs() < 1e-6);
assert_eq!(inputs[9].inputs, 1);
assert_eq!(inputs[9].max_latency_ms, None);
assert_eq!(inputs.iter().map(|bucket| bucket.inputs).sum::<usize>(), 3);
let presented = capture.inputs_presented_by("main", 2);
assert_eq!(
presented.iter().map(|input| input.interaction_id).collect::<Vec<_>>(),
vec![10, 11]
);
assert!(capture.inputs_presented_by("main", 1).is_empty());
let first = presented[0];
assert_eq!(first.kind.name(), "pointer");
assert_eq!(capture::format_duration(first.queue_delay), "0.25ms");
assert_eq!(capture::format_duration(first.total), "0.50ms");
assert_eq!(
first.input_to_present.map(capture::format_duration).as_deref(),
Some("90.00ms")
);
let late = &capture.passes[1];
let sections = pass_detail(late, pass_of(late).unwrap());
assert_eq!(sections[1].title, "PRESENT");
let row = |title: &str, label: &str| {
sections
.iter()
.find(|section| section.title == title)
.and_then(|section| section.rows.iter().find(|(name, _)| *name == label))
.map(|(_, value)| value.clone())
};
assert_eq!(row("PASS", "reasons").as_deref(), Some("redraw_requested"));
assert_eq!(row("PRESENT", "missed_vsyncs").as_deref(), Some("2"));
assert_eq!(row("PRESENT", "continuous").as_deref(), Some("true"));
assert_eq!(row("PRESENT", "interval").as_deref(), Some("48.00ms"));
let (_app, tree) = tab_tree(
Signal::new(ProfilerState {
capture: Some(Arc::new(capture)),
range: Some(range),
selected: Some(2),
..ProfilerState::default()
}),
1440,
860,
);
assert!(has_text(&tree, "Pass #2"));
assert!(has_text(&tree, "over 2× budget"));
assert!(has_text(&tree, "yes · 2"), "two vsyncs missed");
assert!(has_text(&tree, "redraw_requested"));
}
#[test]
fn a_time_is_within_the_budget_over_it_or_over_twice_it() {
assert_eq!(Budget::of(FRAME_BUDGET_MS), Budget::Within);
assert_eq!(Budget::of(FRAME_BUDGET_MS + 0.01), Budget::Over);
assert_eq!(Budget::of(FRAME_BUDGET_MS * 2.0 + 0.01), Budget::OverTwice);
assert_eq!(Budget::of_latency(FRAME_BUDGET_MS * 3.0), Budget::Within);
assert_eq!(Budget::of_latency(FRAME_BUDGET_MS * 3.0 + 0.01), Budget::Over);
assert_eq!(Budget::of_latency(FRAME_BUDGET_MS * 6.0 + 0.01), Budget::OverTwice);
}
#[test]
fn a_span_has_the_color_of_its_phase_and_a_component_none_of_its_own() {
use crate::app::profiler::ApplicationLane;
assert_eq!(phase_color("pass"), Some(PaletteColor::MutedForeground));
assert_eq!(phase_color("ui_rebuild"), Some(PaletteColor::Chart1));
assert_eq!(phase_color("layout_compute"), Some(PaletteColor::Chart2));
assert_eq!(phase_color("glyph_rasterize"), Some(PaletteColor::Chart3));
assert_eq!(phase_color("render_encode"), Some(PaletteColor::Chart4));
assert_eq!(phase_color("canvas_commands"), Some(PaletteColor::Chart4));
assert_eq!(phase_color("Counter"), None);
assert_eq!(span_color("Counter", SpanTrack::Frames), PaletteColor::Chart1);
assert_eq!(
span_color("render", SpanTrack::Application(ApplicationLane::Worker)),
PaletteColor::Chart5
);
}
#[test]
fn a_bar_is_as_wide_as_the_board_has_it_for_few_passes_and_narrower_for_many() {
assert_eq!(bar_width(1), 9.0);
assert_eq!(bar_width(72), 9.0);
assert_eq!(bar_width(150), 4.0);
assert_eq!(bar_width(MAX_TIMELINE_PASSES), 2.0);
}
#[test]
fn the_toolbar_says_the_error_and_the_tab_what_it_waits_for() {
let (_app, tree) = tab_tree(
Signal::new(ProfilerState {
error: Some("Load failed: expected value at line 1 column 1".to_owned()),
..ProfilerState::default()
}),
1440,
860,
);
assert!(has_text(&tree, "Load failed: expected value at line 1 column 1"));
assert!(has_text(&tree, "No recording"));
assert!(has_text(&tree, "Start recording"));
let main = WindowProfiler::new();
let mut recording = ProfilerState::default();
assert!(recording.start(&main.handle()));
let (_app, tree) = tab_tree(Signal::new(recording.clone()), 1440, 860);
assert!(has_text(&tree, "Stop recording"));
assert!(has_text(&tree, "Recording"));
assert!(!has_text(&tree, "Include DevTools"));
recording.clear();
}
#[test]
fn a_range_of_more_passes_than_are_drawn_asks_for_a_narrower_one() {
let count = MAX_TIMELINE_PASSES + 10;
let passes: Vec<_> = (0..count)
.map(|index| sample(index as u64 + 1, "main", index as f64 * 16.0, 1.0, true))
.collect();
let capture = capture(passes, count as f64 * 16.0);
let whole = capture.whole();
let state = Signal::new(ProfilerState {
capture: Some(Arc::new(capture)),
range: Some(whole),
..ProfilerState::default()
});
let (mut app, mut tree) = tab_tree(state.clone(), 1440, 860);
assert!(has_text(&tree, &format!("{count} passes in this range")));
assert!(tree.get_element_by_id(&track_id("passes")).is_none());
state.update(|state| {
let range = state.capture.as_ref().unwrap().default_range();
state.set_range(range);
});
tree.pass_headless(&mut app);
assert!(tree.get_element_by_id(&track_id("passes")).is_some());
}