tui_update.rs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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..=6 {
  11. let parameter_string = format!("{}", 5 * size);
  12. group.bench_with_input(
  13. BenchmarkId::new("size", parameter_string),
  14. &size,
  15. |b, size| {
  16. b.iter(|| match size {
  17. 1 => dioxus::tui::launch_cfg(
  18. app5,
  19. Config {
  20. headless: true,
  21. ..Default::default()
  22. },
  23. ),
  24. 2 => dioxus::tui::launch_cfg(
  25. app10,
  26. Config {
  27. headless: true,
  28. ..Default::default()
  29. },
  30. ),
  31. 3 => dioxus::tui::launch_cfg(
  32. app15,
  33. Config {
  34. headless: true,
  35. ..Default::default()
  36. },
  37. ),
  38. 4 => dioxus::tui::launch_cfg(
  39. app20,
  40. Config {
  41. headless: true,
  42. ..Default::default()
  43. },
  44. ),
  45. 5 => dioxus::tui::launch_cfg(
  46. app25,
  47. Config {
  48. headless: true,
  49. ..Default::default()
  50. },
  51. ),
  52. 6 => dioxus::tui::launch_cfg(
  53. app30,
  54. Config {
  55. headless: true,
  56. ..Default::default()
  57. },
  58. ),
  59. _ => (),
  60. })
  61. },
  62. );
  63. }
  64. }
  65. #[derive(Props, PartialEq)]
  66. struct BoxProps {
  67. x: usize,
  68. y: usize,
  69. hue: f32,
  70. alpha: f32,
  71. }
  72. #[allow(non_snake_case)]
  73. fn Box(cx: Scope<BoxProps>) -> Element {
  74. let count = use_state(&cx, || 0);
  75. let x = cx.props.x * 2;
  76. let y = cx.props.y * 2;
  77. let hue = cx.props.hue;
  78. let display_hue = cx.props.hue as u32 / 10;
  79. let count = count.get();
  80. let alpha = cx.props.alpha + (count % 100) as f32;
  81. cx.render(rsx! {
  82. div {
  83. left: "{x}%",
  84. top: "{y}%",
  85. width: "100%",
  86. height: "100%",
  87. background_color: "hsl({hue}, 100%, 50%, {alpha}%)",
  88. align_items: "center",
  89. p{"{display_hue:03}"}
  90. }
  91. })
  92. }
  93. #[derive(Props, PartialEq)]
  94. struct GridProps {
  95. size: usize,
  96. }
  97. #[allow(non_snake_case)]
  98. fn Grid(cx: Scope<GridProps>) -> Element {
  99. let size = cx.props.size;
  100. let count = use_state(&cx, || 0);
  101. let counts = use_ref(&cx, || vec![0; size * size]);
  102. let ctx: TuiContext = cx.consume_context().unwrap();
  103. if *count.get() + 1 >= (size * size) {
  104. ctx.quit();
  105. } else {
  106. counts.with_mut(|c| {
  107. let i = *count.current();
  108. c[i] += 1;
  109. c[i] = c[i] % 360;
  110. });
  111. count.with_mut(|i| {
  112. *i += 1;
  113. *i = *i % (size * size);
  114. });
  115. }
  116. cx.render(rsx! {
  117. div{
  118. width: "100%",
  119. height: "100%",
  120. flex_direction: "column",
  121. (0..size).map(|x|
  122. {
  123. cx.render(rsx! {
  124. div{
  125. width: "100%",
  126. height: "100%",
  127. flex_direction: "row",
  128. (0..size).map(|y|
  129. {
  130. let alpha = y as f32*100.0/size as f32 + counts.read()[x*size + y] as f32;
  131. let key = format!("{}-{}", x, y);
  132. cx.render(rsx! {
  133. Box{
  134. x: x,
  135. y: y,
  136. alpha: 100.0,
  137. hue: alpha,
  138. key: "{key}",
  139. }
  140. })
  141. }
  142. )
  143. }
  144. })
  145. }
  146. )
  147. }
  148. })
  149. }
  150. fn app5(cx: Scope) -> Element {
  151. cx.render(rsx! {
  152. div{
  153. width: "100%",
  154. height: "100%",
  155. Grid{
  156. size: 5,
  157. }
  158. }
  159. })
  160. }
  161. fn app10(cx: Scope) -> Element {
  162. cx.render(rsx! {
  163. div{
  164. width: "100%",
  165. height: "100%",
  166. Grid{
  167. size: 10,
  168. }
  169. }
  170. })
  171. }
  172. fn app15(cx: Scope) -> Element {
  173. cx.render(rsx! {
  174. div{
  175. width: "100%",
  176. height: "100%",
  177. Grid{
  178. size: 15,
  179. }
  180. }
  181. })
  182. }
  183. fn app20(cx: Scope) -> Element {
  184. cx.render(rsx! {
  185. div{
  186. width: "100%",
  187. height: "100%",
  188. Grid{
  189. size: 20,
  190. }
  191. }
  192. })
  193. }
  194. fn app25(cx: Scope) -> Element {
  195. cx.render(rsx! {
  196. div{
  197. width: "100%",
  198. height: "100%",
  199. Grid{
  200. size: 25,
  201. }
  202. }
  203. })
  204. }
  205. fn app30(cx: Scope) -> Element {
  206. cx.render(rsx! {
  207. div{
  208. width: "100%",
  209. height: "100%",
  210. Grid{
  211. size: 30,
  212. }
  213. }
  214. })
  215. }