framework_benchmark.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. static App: Component = |cx| {
  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: &'static str,
  87. id: &'static str,
  88. onclick: &'a dyn Fn(),
  89. }
  90. fn ActionButton<'a>(cx: Scope<'a, ActionButtonProps<'a>>) -> Element {
  91. rsx!(cx, div { class: "col-sm-6 smallpad",
  92. button { class:"btn btn-primary btn-block", r#type: "button", id: "{cx.props.id}", onclick: move |_| (cx.props.onclick)(),
  93. "{cx.props.name}"
  94. }
  95. })
  96. }
  97. static ADJECTIVES: &[&str] = &[
  98. "pretty",
  99. "large",
  100. "big",
  101. "small",
  102. "tall",
  103. "short",
  104. "long",
  105. "handsome",
  106. "plain",
  107. "quaint",
  108. "clean",
  109. "elegant",
  110. "easy",
  111. "angry",
  112. "crazy",
  113. "helpful",
  114. "mushy",
  115. "odd",
  116. "unsightly",
  117. "adorable",
  118. "important",
  119. "inexpensive",
  120. "cheap",
  121. "expensive",
  122. "fancy",
  123. ];
  124. static COLOURS: &[&str] = &[
  125. "red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black",
  126. "orange",
  127. ];
  128. static NOUNS: &[&str] = &[
  129. "table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger",
  130. "pizza", "mouse", "keyboard",
  131. ];