many_small_edit_stress.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. use dioxus::prelude::*;
  2. use dioxus_tui::{Config, TuiContext};
  3. /// This benchmarks the cache performance of the TUI for small edits by changing one box at a time.
  4. fn main() {
  5. for size in 1..=20usize {
  6. for _ in 0..10 {
  7. let dom = VirtualDom::new(app).with_root_context(size);
  8. dioxus_tui::launch_vdom_cfg(dom, Config::default().with_headless())
  9. }
  10. }
  11. }
  12. fn app() -> Element {
  13. let size = use_context::<usize>();
  14. rsx! {
  15. div { width: "100%", height: "100%", Grid { size } }
  16. }
  17. }
  18. #[component]
  19. fn Box(x: usize, y: usize, hue: f32, alpha: f32) -> Element {
  20. let count = use_signal(|| 0);
  21. let x = x * 2;
  22. let y = y * 2;
  23. let hue = hue;
  24. let display_hue = hue as u32 / 10;
  25. let alpha = alpha + (count() % 100) as f32;
  26. rsx! {
  27. div {
  28. left: "{x}%",
  29. top: "{y}%",
  30. width: "100%",
  31. height: "100%",
  32. background_color: "hsl({hue}, 100%, 50%, {alpha}%)",
  33. align_items: "center",
  34. p{"{display_hue:03}"}
  35. }
  36. }
  37. }
  38. #[component]
  39. fn Grid(size: usize) -> Element {
  40. let size = size;
  41. let mut count = use_signal(|| 0);
  42. let mut counts = use_signal(|| vec![0; size * size]);
  43. let ctx: TuiContext = consume_context();
  44. if count() + 1 >= (size * size) {
  45. ctx.quit();
  46. } else {
  47. counts.with_mut(|c| {
  48. let i = count();
  49. c[i] += 1;
  50. c[i] %= 360;
  51. });
  52. count.with_mut(|i| {
  53. *i += 1;
  54. *i %= size * size;
  55. });
  56. }
  57. rsx! {
  58. div{
  59. width: "100%",
  60. height: "100%",
  61. flex_direction: "column",
  62. for x in 0..size {
  63. div{
  64. width: "100%",
  65. height: "100%",
  66. flex_direction: "row",
  67. for y in 0..size {
  68. {
  69. let alpha = y as f32*100.0/size as f32 + counts.read()[x*size + y] as f32;
  70. let key = format!("{}-{}", x, y);
  71. rsx! {
  72. Box {
  73. x: x,
  74. y: y,
  75. alpha: 100.0,
  76. hue: alpha,
  77. key: "{key}",
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }