many_small_edit_stress.rs 2.5 KB

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