update.rs 3.6 KB

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