dioxus/packages/dioxus-tui/examples/many_small_edit_stress.rs

111 lines
2.8 KiB
Rust
Raw Normal View History

2023-02-08 02:55:01 +00:00
use dioxus::prelude::*;
use dioxus_tui::{Config, TuiContext};
/// This benchmarks the cache performance of the TUI for small edits by changing one box at a time.
fn main() {
2023-02-08 13:57:00 +00:00
for size in 1..=20usize {
2023-02-08 02:55:01 +00:00
for _ in 0..10 {
dioxus_tui::launch_cfg_with_props(app, size, Config::default().with_headless())
}
}
}
#[derive(Props, PartialEq)]
struct BoxProps {
x: usize,
y: usize,
hue: f32,
alpha: f32,
}
#[allow(non_snake_case)]
fn Box(cx: Scope<BoxProps>) -> Element {
2024-01-14 05:12:21 +00:00
let count = use_signal(|| 0);
2023-02-08 02:55:01 +00:00
let x = cx.props.x * 2;
let y = cx.props.y * 2;
let hue = cx.props.hue;
let display_hue = cx.props.hue as u32 / 10;
let count = count.get();
let alpha = cx.props.alpha + (count % 100) as f32;
2024-01-14 05:12:21 +00:00
rsx! {
2023-02-08 02:55:01 +00:00
div {
left: "{x}%",
top: "{y}%",
width: "100%",
height: "100%",
background_color: "hsl({hue}, 100%, 50%, {alpha}%)",
align_items: "center",
p{"{display_hue:03}"}
}
2024-01-14 05:12:21 +00:00
}
2023-02-08 02:55:01 +00:00
}
#[derive(Props, PartialEq)]
struct GridProps {
size: usize,
}
#[allow(non_snake_case)]
fn Grid(cx: Scope<GridProps>) -> Element {
let size = cx.props.size;
2024-01-14 05:12:21 +00:00
let count = use_signal(|| 0);
let counts = use_signal(|| vec![0; size * size]);
2023-02-08 02:55:01 +00:00
let ctx: TuiContext = cx.consume_context().unwrap();
if *count.get() + 1 >= (size * size) {
ctx.quit();
} else {
counts.with_mut(|c| {
let i = *count.current();
c[i] += 1;
c[i] %= 360;
});
count.with_mut(|i| {
*i += 1;
*i %= size * size;
});
}
2024-01-16 19:18:46 +00:00
rsx! {
2023-02-08 02:55:01 +00:00
div{
width: "100%",
height: "100%",
flex_direction: "column",
for x in 0..size {
div{
width: "100%",
height: "100%",
flex_direction: "row",
for y in 0..size {
{
let alpha = y as f32*100.0/size as f32 + counts.read()[x*size + y] as f32;
let key = format!("{}-{}", x, y);
rsx! {
Box {
x: x,
y: y,
alpha: 100.0,
hue: alpha,
key: "{key}",
2023-02-08 02:55:01 +00:00
}
}
2023-02-08 02:55:01 +00:00
}
}
}
}
2023-02-08 02:55:01 +00:00
}
}
}
fn app(cx: Scope<usize>) -> Element {
2024-01-14 05:12:21 +00:00
rsx! {
2023-02-08 02:55:01 +00:00
div{
width: "100%",
height: "100%",
Grid{
size: *cx.props,
}
}
2024-01-14 05:12:21 +00:00
}
2023-02-08 02:55:01 +00:00
}