many_small_edit_stress.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. (0..size).map(|x|
  68. {
  69. rsx! {
  70. div{
  71. width: "100%",
  72. height: "100%",
  73. flex_direction: "row",
  74. (0..size).map(|y|
  75. {
  76. let alpha = y as f32*100.0/size as f32 + counts.read()[x*size + y] as f32;
  77. let key = format!("{}-{}", x, y);
  78. rsx! {
  79. Box{
  80. x: x,
  81. y: y,
  82. alpha: 100.0,
  83. hue: alpha,
  84. key: "{key}",
  85. }
  86. }
  87. }
  88. )
  89. }
  90. }
  91. }
  92. )
  93. }
  94. }
  95. }
  96. fn app(cx: Scope<usize>) -> Element {
  97. cx.render(rsx! {
  98. div{
  99. width: "100%",
  100. height: "100%",
  101. Grid{
  102. size: *cx.props,
  103. }
  104. }
  105. })
  106. }