many_small_edit_stress.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. dioxus_tui::launch_cfg_with_props(app, size, Config::default().with_headless())
  8. }
  9. }
  10. }
  11. #[derive(Props, PartialEq)]
  12. struct BoxProps {
  13. x: usize,
  14. y: usize,
  15. hue: f32,
  16. alpha: f32,
  17. }
  18. #[allow(non_snake_case)]
  19. fn Box(cx: Scope<BoxProps>) -> Element {
  20. let count = use_state(cx, || 0);
  21. let x = cx.props.x * 2;
  22. let y = cx.props.y * 2;
  23. let hue = cx.props.hue;
  24. let display_hue = cx.props.hue as u32 / 10;
  25. let count = count.get();
  26. let alpha = cx.props.alpha + (count % 100) as f32;
  27. cx.render(rsx! {
  28. div {
  29. left: "{x}%",
  30. top: "{y}%",
  31. width: "100%",
  32. height: "100%",
  33. background_color: "hsl({hue}, 100%, 50%, {alpha}%)",
  34. align_items: "center",
  35. p{"{display_hue:03}"}
  36. }
  37. })
  38. }
  39. #[derive(Props, PartialEq)]
  40. struct GridProps {
  41. size: usize,
  42. }
  43. #[allow(non_snake_case)]
  44. fn Grid(cx: Scope<GridProps>) -> Element {
  45. let size = cx.props.size;
  46. let count = use_state(cx, || 0);
  47. let counts = use_ref(cx, || vec![0; size * size]);
  48. let ctx: TuiContext = cx.consume_context().unwrap();
  49. if *count.get() + 1 >= (size * size) {
  50. ctx.quit();
  51. } else {
  52. counts.with_mut(|c| {
  53. let i = *count.current();
  54. c[i] += 1;
  55. c[i] %= 360;
  56. });
  57. count.with_mut(|i| {
  58. *i += 1;
  59. *i %= size * size;
  60. });
  61. }
  62. render! {
  63. div{
  64. width: "100%",
  65. height: "100%",
  66. flex_direction: "column",
  67. for x in 0..size {
  68. div{
  69. width: "100%",
  70. height: "100%",
  71. flex_direction: "row",
  72. for y in 0..size {
  73. {
  74. let alpha = y as f32*100.0/size as f32 + counts.read()[x*size + y] as f32;
  75. let key = format!("{}-{}", x, y);
  76. rsx! {
  77. Box {
  78. x: x,
  79. y: y,
  80. alpha: 100.0,
  81. hue: alpha,
  82. key: "{key}",
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }
  92. fn app(cx: Scope<usize>) -> Element {
  93. cx.render(rsx! {
  94. div{
  95. width: "100%",
  96. height: "100%",
  97. Grid{
  98. size: *cx.props,
  99. }
  100. }
  101. })
  102. }