tui_update.rs 6.9 KB

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