framework_benchmark.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. use dioxus::prelude::*;
  2. use rand::prelude::*;
  3. fn main() {
  4. dioxus::desktop::launch(app);
  5. }
  6. #[derive(Clone, PartialEq)]
  7. struct Label {
  8. key: usize,
  9. labels: [&'static str; 3],
  10. }
  11. impl Label {
  12. fn new_list(num: usize) -> Vec<Self> {
  13. let mut rng = SmallRng::from_entropy();
  14. let mut labels = Vec::with_capacity(num);
  15. for _ in 0..num {
  16. labels.push(Label {
  17. key: 0,
  18. labels: [
  19. ADJECTIVES.choose(&mut rng).unwrap(),
  20. COLOURS.choose(&mut rng).unwrap(),
  21. NOUNS.choose(&mut rng).unwrap(),
  22. ],
  23. });
  24. }
  25. labels
  26. }
  27. }
  28. fn app(cx: Scope) -> Element {
  29. let items = use_ref(&cx, || vec![]);
  30. let selected = use_state(&cx, || None);
  31. cx.render(rsx! {
  32. div { class: "container",
  33. div { class: "jumbotron",
  34. div { class: "row",
  35. div { class: "col-md-6", h1 { "Dioxus" } }
  36. div { class: "col-md-6",
  37. div { class: "row",
  38. ActionButton { name: "Create 1,000 rows", id: "run",
  39. onclick: move |_| items.set(Label::new_list(1_000)),
  40. }
  41. ActionButton { name: "Create 10,000 rows", id: "runlots",
  42. onclick: move |_| items.set(Label::new_list(10_000)),
  43. }
  44. ActionButton { name: "Append 1,000 rows", id: "add",
  45. onclick: move |_| items.write().extend(Label::new_list(1_000)),
  46. }
  47. ActionButton { name: "Update every 10th row", id: "update",
  48. onclick: move |_| items.write().iter_mut().step_by(10).for_each(|item| item.labels[2] = "!!!"),
  49. }
  50. ActionButton { name: "Clear", id: "clear",
  51. onclick: move |_| items.write().clear(),
  52. }
  53. ActionButton { name: "Swap rows", id: "swaprows",
  54. onclick: move |_| items.write().swap(0, 998),
  55. }
  56. }
  57. }
  58. }
  59. }
  60. table {
  61. tbody {
  62. items.read().iter().enumerate().map(|(id, item)| {
  63. let is_in_danger = if (*selected).map(|s| s == id).unwrap_or(false) {"danger"} else {""};
  64. rsx!(tr { class: "{is_in_danger}",
  65. td { class:"col-md-1" }
  66. td { class:"col-md-1", "{item.key}" }
  67. td { class:"col-md-1", onclick: move |_| selected.set(Some(id)),
  68. a { class: "lbl", item.labels }
  69. }
  70. td { class: "col-md-1",
  71. a { class: "remove", onclick: move |_| { items.write().remove(id); },
  72. span { class: "glyphicon glyphicon-remove remove", aria_hidden: "true" }
  73. }
  74. }
  75. td { class: "col-md-6" }
  76. })
  77. })
  78. }
  79. }
  80. span { class: "preloadicon glyphicon glyphicon-remove", aria_hidden: "true" }
  81. }
  82. })
  83. }
  84. #[derive(Props)]
  85. struct ActionButtonProps<'a> {
  86. name: &'a str,
  87. id: &'a str,
  88. onclick: EventHandler<'a, ()>,
  89. }
  90. fn ActionButton<'a>(cx: Scope<'a, ActionButtonProps<'a>>) -> Element {
  91. cx.render(rsx! {
  92. div {
  93. class: "col-sm-6 smallpad",
  94. button {
  95. class:"btn btn-primary btn-block",
  96. r#type: "button",
  97. id: "{cx.props.id}",
  98. onclick: move |_| cx.props.onclick.call(()),
  99. "{cx.props.name}"
  100. }
  101. }
  102. })
  103. }
  104. static ADJECTIVES: &[&str] = &[
  105. "pretty",
  106. "large",
  107. "big",
  108. "small",
  109. "tall",
  110. "short",
  111. "long",
  112. "handsome",
  113. "plain",
  114. "quaint",
  115. "clean",
  116. "elegant",
  117. "easy",
  118. "angry",
  119. "crazy",
  120. "helpful",
  121. "mushy",
  122. "odd",
  123. "unsightly",
  124. "adorable",
  125. "important",
  126. "inexpensive",
  127. "cheap",
  128. "expensive",
  129. "fancy",
  130. ];
  131. static COLOURS: &[&str] = &[
  132. "red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black",
  133. "orange",
  134. ];
  135. static NOUNS: &[&str] = &[
  136. "table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger",
  137. "pizza", "mouse", "keyboard",
  138. ];