123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
- use dioxus::prelude::*;
- use dioxus_tui::{Config, TuiContext};
- criterion_group!(mbenches, tui_update);
- criterion_main!(mbenches);
- /// This benchmarks the cache performance of the TUI for small edits by changing one box at a time.
- fn tui_update(c: &mut Criterion) {
- let mut group = c.benchmark_group("Update boxes");
- // We can also use loops to define multiple benchmarks, even over multiple dimensions.
- for size in 1..=8u32 {
- let parameter_string = format!("{}", (3 * size).pow(2));
- group.bench_with_input(
- BenchmarkId::new("size", parameter_string),
- &size,
- |b, size| {
- b.iter(|| match size {
- 1 => dioxus::tui::launch_cfg(
- app3,
- Config {
- ..Default::default()
- },
- ),
- 2 => dioxus::tui::launch_cfg(
- app6,
- Config {
- ..Default::default()
- },
- ),
- 3 => dioxus::tui::launch_cfg(
- app9,
- Config {
- ..Default::default()
- },
- ),
- 4 => dioxus::tui::launch_cfg(
- app12,
- Config {
- ..Default::default()
- },
- ),
- 5 => dioxus::tui::launch_cfg(
- app15,
- Config {
- ..Default::default()
- },
- ),
- 6 => dioxus::tui::launch_cfg(
- app18,
- Config {
- ..Default::default()
- },
- ),
- 7 => dioxus::tui::launch_cfg(
- app21,
- Config {
- ..Default::default()
- },
- ),
- 8 => dioxus::tui::launch_cfg(
- app24,
- Config {
- ..Default::default()
- },
- ),
- _ => (),
- })
- },
- );
- }
- }
- #[derive(Props, PartialEq)]
- struct BoxProps {
- x: usize,
- y: usize,
- hue: f32,
- alpha: f32,
- }
- #[allow(non_snake_case)]
- fn Box(cx: Scope<BoxProps>) -> Element {
- let count = use_state(&cx, || 0);
- 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;
- cx.render(rsx! {
- div {
- left: "{x}%",
- top: "{y}%",
- width: "100%",
- height: "100%",
- background_color: "hsl({hue}, 100%, 50%, {alpha}%)",
- align_items: "center",
- p{"{display_hue:03}"}
- }
- })
- }
- #[derive(Props, PartialEq)]
- struct GridProps {
- size: usize,
- }
- #[allow(non_snake_case)]
- fn Grid(cx: Scope<GridProps>) -> Element {
- let size = cx.props.size;
- let count = use_state(&cx, || 0);
- let counts = use_ref(&cx, || vec![0; size * size]);
- 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] = c[i] % 360;
- });
- count.with_mut(|i| {
- *i += 1;
- *i = *i % (size * size);
- });
- }
- cx.render(rsx! {
- div{
- width: "100%",
- height: "100%",
- flex_direction: "column",
- (0..size).map(|x|
- {
- cx.render(rsx! {
- div{
- width: "100%",
- height: "100%",
- flex_direction: "row",
- (0..size).map(|y|
- {
- let alpha = y as f32*100.0/size as f32 + counts.read()[x*size + y] as f32;
- let key = format!("{}-{}", x, y);
- cx.render(rsx! {
- Box{
- x: x,
- y: y,
- alpha: 100.0,
- hue: alpha,
- key: "{key}",
- }
- })
- }
- )
- }
- })
- }
- )
- }
- })
- }
- fn app3(cx: Scope) -> Element {
- cx.render(rsx! {
- div{
- width: "100%",
- height: "100%",
- Grid{
- size: 3,
- }
- }
- })
- }
- fn app6(cx: Scope) -> Element {
- cx.render(rsx! {
- div{
- width: "100%",
- height: "100%",
- Grid{
- size: 6,
- }
- }
- })
- }
- fn app9(cx: Scope) -> Element {
- cx.render(rsx! {
- div{
- width: "100%",
- height: "100%",
- Grid{
- size: 9,
- }
- }
- })
- }
- fn app12(cx: Scope) -> Element {
- cx.render(rsx! {
- div{
- width: "100%",
- height: "100%",
- Grid{
- size: 12,
- }
- }
- })
- }
- fn app15(cx: Scope) -> Element {
- cx.render(rsx! {
- div{
- width: "100%",
- height: "100%",
- Grid{
- size: 15,
- }
- }
- })
- }
- fn app18(cx: Scope) -> Element {
- cx.render(rsx! {
- div{
- width: "100%",
- height: "100%",
- Grid{
- size: 18,
- }
- }
- })
- }
- fn app21(cx: Scope) -> Element {
- cx.render(rsx! {
- div{
- width: "100%",
- height: "100%",
- Grid{
- size: 21,
- }
- }
- })
- }
- fn app24(cx: Scope) -> Element {
- cx.render(rsx! {
- div{
- width: "100%",
- height: "100%",
- Grid{
- size: 24,
- }
- }
- })
- }
|