update.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
  2. use dioxus::prelude::*;
  3. use dioxus_tui::{Config, TuiContext};
  4. criterion_group!(mbenches, tui_update);
  5. criterion_main!(mbenches);
  6. /// This benchmarks the cache performance of the TUI for small edits by changing one box at a time.
  7. fn tui_update(c: &mut Criterion) {
  8. {
  9. let mut group = c.benchmark_group("Update boxes");
  10. for size in 1..=20usize {
  11. let parameter_string = format!("{}", (size).pow(2));
  12. group.bench_with_input(
  13. BenchmarkId::new("size", parameter_string),
  14. &size,
  15. |b, size| {
  16. b.iter(|| {
  17. dioxus_tui::launch_cfg_with_props(
  18. app,
  19. GridProps {
  20. size: *size,
  21. update_count: 1,
  22. },
  23. Config::default().with_headless(),
  24. )
  25. })
  26. },
  27. );
  28. }
  29. }
  30. {
  31. let mut group = c.benchmark_group("Update many boxes");
  32. for update_count in 1..=20usize {
  33. let update_count = update_count * 20;
  34. let parameter_string = update_count.to_string();
  35. group.bench_with_input(
  36. BenchmarkId::new("update count", parameter_string),
  37. &update_count,
  38. |b, update_count| {
  39. b.iter(|| {
  40. dioxus_tui::launch_cfg_with_props(
  41. app,
  42. GridProps {
  43. size: 20,
  44. update_count: *update_count,
  45. },
  46. Config::default().with_headless(),
  47. )
  48. })
  49. },
  50. );
  51. }
  52. }
  53. }
  54. #[derive(Props, PartialEq)]
  55. struct BoxProps {
  56. x: usize,
  57. y: usize,
  58. hue: f32,
  59. alpha: f32,
  60. }
  61. #[allow(non_snake_case)]
  62. fn Box(cx: Scope<BoxProps>) -> Element {
  63. let count = use_state(cx, || 0);
  64. let x = cx.props.x * 2;
  65. let y = cx.props.y * 2;
  66. let hue = cx.props.hue;
  67. let display_hue = cx.props.hue as u32 / 10;
  68. let count = count.get();
  69. let alpha = cx.props.alpha + (count % 100) as f32;
  70. cx.render(rsx! {
  71. div {
  72. left: "{x}%",
  73. top: "{y}%",
  74. width: "100%",
  75. height: "100%",
  76. background_color: "hsl({hue}, 100%, 50%, {alpha}%)",
  77. align_items: "center",
  78. p{"{display_hue:03}"}
  79. }
  80. })
  81. }
  82. #[derive(Props, PartialEq)]
  83. struct GridProps {
  84. size: usize,
  85. update_count: usize,
  86. }
  87. #[allow(non_snake_case)]
  88. fn Grid(cx: Scope<GridProps>) -> Element {
  89. let size = cx.props.size;
  90. let count = use_state(cx, || 0);
  91. let counts = use_ref(cx, || vec![0; size * size]);
  92. let ctx: TuiContext = cx.consume_context().unwrap();
  93. if *count.get() + cx.props.update_count >= (size * size) {
  94. ctx.quit();
  95. } else {
  96. for _ in 0..cx.props.update_count {
  97. counts.with_mut(|c| {
  98. let i = *count.current();
  99. c[i] += 1;
  100. c[i] %= 360;
  101. });
  102. count.with_mut(|i| {
  103. *i += 1;
  104. *i %= size * size;
  105. });
  106. }
  107. }
  108. render! {
  109. div{
  110. width: "100%",
  111. height: "100%",
  112. flex_direction: "column",
  113. (0..size).map(|x|
  114. {
  115. rsx! {
  116. div{
  117. width: "100%",
  118. height: "100%",
  119. flex_direction: "row",
  120. (0..size).map(|y|
  121. {
  122. let alpha = y as f32*100.0/size as f32 + counts.read()[x*size + y] as f32;
  123. let key = format!("{}-{}", x, y);
  124. rsx! {
  125. Box{
  126. x: x,
  127. y: y,
  128. alpha: 100.0,
  129. hue: alpha,
  130. key: "{key}",
  131. }
  132. }
  133. }
  134. )
  135. }
  136. }
  137. }
  138. )
  139. }
  140. }
  141. }
  142. fn app(cx: Scope<GridProps>) -> Element {
  143. cx.render(rsx! {
  144. div{
  145. width: "100%",
  146. height: "100%",
  147. Grid{
  148. size: cx.props.size,
  149. update_count: cx.props.update_count,
  150. }
  151. }
  152. })
  153. }