|
| 1 | +//! Paired headless measurements. Run with the perf profile; no GPU upload or presentation. |
| 2 | +use rune::{Color, Cx, El, Env, Live, Runtime, VirtualItems, col, el, text}; |
| 3 | +use std::{cell::Cell, rc::Rc, time::Instant}; |
| 4 | + |
| 5 | +struct Source { |
| 6 | + catalog: Vec<String>, |
| 7 | + offset: Cell<usize>, |
| 8 | + revision: Cell<u64>, |
| 9 | + builds: Cell<usize>, |
| 10 | + complex: bool, |
| 11 | + opacity: Live<f32>, |
| 12 | +} |
| 13 | +impl VirtualItems for Source { |
| 14 | + fn version(&self) -> u64 { |
| 15 | + self.revision.get() |
| 16 | + } |
| 17 | + fn keys(&self) -> Vec<String> { |
| 18 | + (0..50).map(|i| self.catalog[(self.offset.get() + i) % self.catalog.len()].clone()).collect() |
| 19 | + } |
| 20 | + fn build(&self, key: &str) -> El { |
| 21 | + self.builds.set(self.builds.get() + 1); |
| 22 | + let i = key[1..].parse::<usize>().unwrap(); |
| 23 | + let item = col().absolute((i % 10) as f32 * 60.0, (i % 5) as f32 * 60.0).size(54.0, 54.0); |
| 24 | + if !self.complex { |
| 25 | + return item.fill(Color::WHITE); |
| 26 | + } |
| 27 | + item.live_opacity(&self.opacity).children((0..19).map(|j| { |
| 28 | + let p = |
| 29 | + if j % 4 == 0 { text("Canvas node").h(5.0) } else { el().size(8.0, 2.0).fill(Color::WHITE) }; |
| 30 | + if j % 3 == 0 { p.live_opacity(&self.opacity) } else { p } |
| 31 | + })) |
| 32 | + } |
| 33 | +} |
| 34 | +struct App { |
| 35 | + source: Rc<Source>, |
| 36 | + views: Cell<usize>, |
| 37 | +} |
| 38 | +fn view(s: &App, _: &mut Cx<App>) -> El { |
| 39 | + s.views.set(s.views.get() + 1); |
| 40 | + el().size(640.0, 480.0).virtual_items(s.source.clone()) |
| 41 | +} |
| 42 | + |
| 43 | +fn main() { |
| 44 | + assert!(!cfg!(debug_assertions), "benchmarks require --profile perf"); |
| 45 | + println!("BENCH,kind,total,enter,full,round,mean_ms,p95_ms,max_ms,views,builds"); |
| 46 | + for complex in [false, true] { |
| 47 | + for total in [100, 10_000, 50_000] { |
| 48 | + for enter in [1, 4, 16] { |
| 49 | + for round in 0..5 { |
| 50 | + for full in if round % 2 == 0 { [false, true] } else { [true, false] } { |
| 51 | + let source = Rc::new(Source { |
| 52 | + catalog: (0..total).map(|i| format!("n{i}")).collect(), |
| 53 | + offset: Cell::new(0), |
| 54 | + revision: Cell::new(0), |
| 55 | + builds: Cell::new(0), |
| 56 | + complex, |
| 57 | + opacity: Live::new(0.8), |
| 58 | + }); |
| 59 | + let mut rt = Runtime::new( |
| 60 | + App { source: source.clone(), views: Cell::new(0) }, |
| 61 | + view, |
| 62 | + (640.0, 480.0), |
| 63 | + Env::new(true, 2.0), |
| 64 | + ); |
| 65 | + rt.frame(0.0); |
| 66 | + let mut samples = Vec::new(); |
| 67 | + for frame in 0..420 { |
| 68 | + let start = Instant::now(); |
| 69 | + source.offset.set((source.offset.get() + enter) % total); |
| 70 | + source.revision.set(source.revision.get() + 1); |
| 71 | + let views = rt.state.views.get(); |
| 72 | + let builds = source.builds.get(); |
| 73 | + if full { |
| 74 | + rt.invalidate(); |
| 75 | + } |
| 76 | + rt.frame(1.0 / 120.0); |
| 77 | + let ms = start.elapsed().as_secs_f64() * 1000.0; |
| 78 | + assert_eq!(rt.state.views.get() - views, usize::from(full)); |
| 79 | + assert_eq!(source.builds.get() - builds, if full { 50 } else { enter }); |
| 80 | + assert_eq!( |
| 81 | + rt.elements().iter().filter(|n| n.path.matches('/').count() == 1).count(), |
| 82 | + 50 |
| 83 | + ); |
| 84 | + if frame >= 120 { |
| 85 | + samples.push(ms); |
| 86 | + } |
| 87 | + } |
| 88 | + samples.sort_by(f64::total_cmp); |
| 89 | + let mean = samples.iter().sum::<f64>() / samples.len() as f64; |
| 90 | + println!( |
| 91 | + "BENCH,{},{total},{enter},{full},{round},{mean:.6},{:.6},{:.6},{},{}", |
| 92 | + if complex { "complex" } else { "simple" }, |
| 93 | + samples[284], |
| 94 | + samples[299], |
| 95 | + if full { 300 } else { 0 }, |
| 96 | + if full { 15_000 } else { enter * 300 } |
| 97 | + ); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments