incremental.rs.old 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #![allow(unused)]
  2. use std::time::Duration;
  3. use dioxus::prelude::*;
  4. use dioxus_router::prelude::*;
  5. use criterion::{black_box, criterion_group, criterion_main, Criterion};
  6. use dioxus_ssr::Renderer;
  7. pub fn criterion_benchmark(c: &mut Criterion) {
  8. c.bench_function("build 1000 routes", |b| {
  9. let mut renderer = IncrementalRenderer::builder()
  10. .static_dir("./static")
  11. .invalidate_after(Duration::from_secs(10))
  12. .build();
  13. let wrapper = DefaultRenderer {
  14. before_body: r#"<!DOCTYPE html>
  15. <html lang="en">
  16. <head>
  17. <meta charset="UTF-8">
  18. <meta name="viewport" content="width=device-width,
  19. initial-scale=1.0">
  20. <title>Dioxus Application</title>
  21. </head>
  22. <body>"#
  23. .to_string(),
  24. after_body: r#"</body>
  25. </html>"#
  26. .to_string(),
  27. };
  28. b.iter(|| {
  29. tokio::runtime::Runtime::new().unwrap().block_on(async {
  30. for id in 0..1000 {
  31. render_route(
  32. &mut renderer,
  33. Route::Post { id },
  34. &mut tokio::io::sink(),
  35. |_| Box::pin(async move {}),
  36. &wrapper,
  37. )
  38. .await
  39. .unwrap();
  40. }
  41. })
  42. })
  43. });
  44. c.bench_function("build 1000 routes no memory cache", |b| {
  45. let wrapper = DefaultRenderer {
  46. before_body: r#"<!DOCTYPE html>
  47. <html lang="en">
  48. <head>
  49. <meta charset="UTF-8">
  50. <meta name="viewport" content="width=device-width,
  51. initial-scale=1.0">
  52. <title>Dioxus Application</title>
  53. </head>
  54. <body>"#
  55. .to_string(),
  56. after_body: r#"</body>
  57. </html>"#
  58. .to_string(),
  59. };
  60. b.to_async(tokio::runtime::Runtime::new().unwrap())
  61. .iter(|| async {
  62. let mut renderer = IncrementalRenderer::builder()
  63. .static_dir("./static")
  64. .memory_cache_limit(0)
  65. .invalidate_after(Duration::from_secs(10))
  66. .build();
  67. for id in 0..1000 {
  68. render_route(
  69. &mut renderer,
  70. Route::Post { id },
  71. &mut tokio::io::sink(),
  72. |_| Box::pin(async move {}),
  73. &wrapper,
  74. )
  75. .await
  76. .unwrap();
  77. }
  78. })
  79. });
  80. c.bench_function("build 1000 routes no cache", |b| {
  81. let mut renderer = Renderer::default();
  82. b.iter(|| {
  83. for id in 0..1000 {
  84. let mut vdom = VirtualDom::new_with_props(
  85. RenderPath,
  86. RenderPathProps::builder().path(Route::Post { id }).build(),
  87. );
  88. vdom.rebuild_in_place();
  89. struct Ignore;
  90. impl std::fmt::Write for Ignore {
  91. fn write_str(&mut self, s: &str) -> std::fmt::Result {
  92. Ok(())
  93. }
  94. }
  95. renderer.render_to(&mut Ignore, &vdom).unwrap();
  96. }
  97. })
  98. });
  99. c.bench_function("cache static", |b| {
  100. let wrapper = DefaultRenderer {
  101. before_body: r#"<!DOCTYPE html>
  102. <html lang="en">
  103. <head>
  104. <meta charset="UTF-8">
  105. <meta name="viewport" content="width=device-width,
  106. initial-scale=1.0">
  107. <title>Dioxus Application</title>
  108. </head>
  109. <body>"#
  110. .to_string(),
  111. after_body: r#"</body>
  112. </html>"#
  113. .to_string(),
  114. };
  115. b.to_async(tokio::runtime::Runtime::new().unwrap())
  116. .iter(|| async {
  117. let mut renderer = IncrementalRenderer::builder()
  118. .static_dir("./static")
  119. .build();
  120. pre_cache_static_routes::<Route, _>(&mut renderer, &wrapper)
  121. .await
  122. .unwrap();
  123. })
  124. });
  125. }
  126. criterion_group!(benches, criterion_benchmark);
  127. criterion_main!(benches);
  128. #[component]
  129. fn Blog() -> Element {
  130. rsx! {
  131. div {
  132. "Blog"
  133. }
  134. }
  135. }
  136. #[component]
  137. fn Post(id: usize) -> Element {
  138. rsx! {
  139. for _ in 0..id {
  140. div {
  141. "PostId: {id}"
  142. }
  143. }
  144. }
  145. }
  146. #[component]
  147. fn PostHome() -> Element {
  148. rsx! {
  149. div {
  150. "Post"
  151. }
  152. }
  153. }
  154. #[component]
  155. fn Home() -> Element {
  156. rsx! {
  157. div {
  158. "Home"
  159. }
  160. }
  161. }
  162. #[rustfmt::skip]
  163. #[derive(Clone, Debug, PartialEq, Routable)]
  164. enum Route {
  165. #[nest("/blog")]
  166. #[route("/")]
  167. Blog {},
  168. #[route("/post/index")]
  169. PostHome {},
  170. #[route("/post/:id")]
  171. Post {
  172. id: usize,
  173. },
  174. #[end_nest]
  175. #[route("/")]
  176. Home {},
  177. }
  178. #[component]
  179. fn RenderPath(path: Route) -> Element {
  180. let path = path.clone();
  181. rsx! {
  182. Router::<Route> {
  183. config: || RouterConfig::default().history(MemoryHistory::with_initial_path(path))
  184. }
  185. }
  186. }