.cargo/
crates/
brain/
diff/
files/
highlight/
repo/
signals/
tui/
tui-core/
tui-termion/
tui-widgets/
benches/
tree.rs
4.6 KiB
src/
tests/
Cargo.toml
408 B
README.md
294 B
scripts/
src/
.gitignore
30 B
.gitsigners
198 B
AGENTS.md
1.9 KiB
CONTRIBUTING
1.7 KiB
Cargo.lock
5.4 KiB
Cargo.toml
1.9 KiB
LICENSE
1.1 KiB
README.md
245 B
STYLE
3.7 KiB
clippy.toml
213 B
crates/tui-widgets/benches/tree.rs
raw
| 1 | //! Render benchmark for the generic tree widget. |
| 2 | //! |
| 3 | //! The harness builds a deterministic tree, expands every branch, and renders a |
| 4 | //! moving visible window. |
| 5 | |
| 6 | use std::env; |
| 7 | use std::time::Instant; |
| 8 | |
| 9 | use rustik_tui_core::{Buffer, Rect, Widget}; |
| 10 | use rustik_tui_widgets::{ScrollView, Tree, TreeModel, TreeNodeKind, TreeRow, TreeState}; |
| 11 | |
| 12 | /// Benchmark frame area. |
| 13 | const AREA: Rect = Rect::new(0, 0, 120, 40); |
| 14 | /// Default number of frames to render. |
| 15 | const DEFAULT_FRAMES: usize = 2_000; |
| 16 | /// Number of root nodes in the synthetic tree. |
| 17 | const ROOTS: usize = 64; |
| 18 | /// Number of directories below each root. |
| 19 | const DIRS_PER_ROOT: usize = 12; |
| 20 | /// Number of files below each directory. |
| 21 | const FILES_PER_DIR: usize = 16; |
| 22 | |
| 23 | /// Benchmark tree node. |
| 24 | #[derive(Debug, Clone)] |
| 25 | struct Node { |
| 26 | /// Rendered node name. |
| 27 | name: String, |
| 28 | /// Optional row detail. |
| 29 | detail: String, |
| 30 | /// Child node ids. |
| 31 | children: Vec<usize>, |
| 32 | } |
| 33 | |
| 34 | /// Benchmark tree model. |
| 35 | #[derive(Debug, Clone)] |
| 36 | struct Model { |
| 37 | /// Root node ids. |
| 38 | roots: Vec<usize>, |
| 39 | /// Tree nodes indexed by id. |
| 40 | nodes: Vec<Node>, |
| 41 | } |
| 42 | |
| 43 | impl Model { |
| 44 | /// Creates the deterministic benchmark tree. |
| 45 | fn new() -> Self { |
| 46 | let mut roots = Vec::with_capacity(ROOTS); |
| 47 | let mut nodes = Vec::with_capacity(ROOTS * (1 + DIRS_PER_ROOT * (1 + FILES_PER_DIR))); |
| 48 | |
| 49 | for root in 0..ROOTS { |
| 50 | let root_id = nodes.len(); |
| 51 | roots.push(root_id); |
| 52 | nodes.push(Node { |
| 53 | name: format!("crate-{root:03}"), |
| 54 | detail: format!("{} files", DIRS_PER_ROOT * FILES_PER_DIR), |
| 55 | children: Vec::with_capacity(DIRS_PER_ROOT), |
| 56 | }); |
| 57 | |
| 58 | for dir in 0..DIRS_PER_ROOT { |
| 59 | let dir_id = nodes.len(); |
| 60 | nodes[root_id].children.push(dir_id); |
| 61 | nodes.push(Node { |
| 62 | name: format!("module-{dir:02}"), |
| 63 | detail: String::new(), |
| 64 | children: Vec::with_capacity(FILES_PER_DIR), |
| 65 | }); |
| 66 | |
| 67 | for file in 0..FILES_PER_DIR { |
| 68 | let file_id = nodes.len(); |
| 69 | nodes[dir_id].children.push(file_id); |
| 70 | nodes.push(Node { |
| 71 | name: format!("file_{root:03}_{dir:02}_{file:02}.rs"), |
| 72 | detail: format!("+{} -{}", file % 17, (root + dir + file) % 9), |
| 73 | children: Vec::new(), |
| 74 | }); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | Self { roots, nodes } |
| 79 | } |
| 80 | |
| 81 | /// Expands every branch in the model. |
| 82 | fn expanded_state(&self) -> TreeState<usize> { |
| 83 | let mut state = TreeState::new(); |
| 84 | |
| 85 | for (id, node) in self.nodes.iter().enumerate() { |
| 86 | if !node.children.is_empty() { |
| 87 | state.expand(id); |
| 88 | } |
| 89 | } |
| 90 | state |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | impl TreeModel for Model { |
| 95 | type Id = usize; |
| 96 | |
| 97 | fn roots(&self) -> &[Self::Id] { |
| 98 | &self.roots |
| 99 | } |
| 100 | |
| 101 | fn children(&self, id: Self::Id) -> &[Self::Id] { |
| 102 | &self.nodes[id].children |
| 103 | } |
| 104 | |
| 105 | fn kind(&self, id: Self::Id) -> TreeNodeKind { |
| 106 | if self.nodes[id].children.is_empty() { |
| 107 | TreeNodeKind::Leaf |
| 108 | } else { |
| 109 | TreeNodeKind::Branch |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | fn row(&self, id: Self::Id) -> TreeRow { |
| 114 | TreeRow::new(self.nodes[id].name.clone()).detail(self.nodes[id].detail.clone()) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /// Parses benchmark arguments. |
| 119 | fn args() -> usize { |
| 120 | let mut frames = DEFAULT_FRAMES; |
| 121 | let mut raw = env::args().skip(1); |
| 122 | |
| 123 | while let Some(arg) = raw.next() { |
| 124 | if arg == "--frames" |
| 125 | && let Some(value) = raw.next() |
| 126 | && let Ok(parsed) = value.parse() |
| 127 | { |
| 128 | frames = parsed; |
| 129 | } |
| 130 | } |
| 131 | frames |
| 132 | } |
| 133 | |
| 134 | /// Runs the tree render benchmark. |
| 135 | fn main() { |
| 136 | let frames = args(); |
| 137 | let model = Model::new(); |
| 138 | let state = model.expanded_state(); |
| 139 | let tree = Tree::new(&model, &state); |
| 140 | let rows = tree.visible_rows(); |
| 141 | let scroll_limit = rows.len().saturating_sub(AREA.rows).max(1); |
| 142 | let mut buffer = Buffer::empty(AREA); |
| 143 | let start = Instant::now(); |
| 144 | |
| 145 | for frame in 0..frames { |
| 146 | let offset = frame.wrapping_mul(7) % scroll_limit; |
| 147 | let mut view = ScrollView::virtualized(rows.len(), offset, |row, cols| { |
| 148 | tree.render_row(rows[row], cols) |
| 149 | }); |
| 150 | |
| 151 | view.render(AREA, &mut buffer); |
| 152 | } |
| 153 | let elapsed = start.elapsed(); |
| 154 | let fps = frames as f64 / elapsed.as_secs_f64(); |
| 155 | |
| 156 | println!("tree benchmark"); |
| 157 | println!("frames: {frames}"); |
| 158 | println!("rows: {}", rows.len()); |
| 159 | println!("elapsed: {:.3}s", elapsed.as_secs_f64()); |
| 160 | println!("fps: {fps:.1}"); |
| 161 | } |