From e83866b98686c0792b1b60af21ec17793a8da956 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Sat, 24 Jun 2023 13:44:22 -0700 Subject: [PATCH] fix router examples --- packages/router/.gitignore | 1 + packages/router/Cargo.toml | 3 +- packages/router/benches/incremental.rs | 101 ++++++++++-------- packages/router/examples/static_generation.rs | 18 +++- packages/router/src/incremental.rs | 2 +- packages/router/src/lib.rs | 4 +- 6 files changed, 77 insertions(+), 52 deletions(-) diff --git a/packages/router/.gitignore b/packages/router/.gitignore index 849ddff3b..8cf5c8dd1 100644 --- a/packages/router/.gitignore +++ b/packages/router/.gitignore @@ -1 +1,2 @@ dist/ +static \ No newline at end of file diff --git a/packages/router/Cargo.toml b/packages/router/Cargo.toml index 740867fed..1941c6e67 100644 --- a/packages/router/Cargo.toml +++ b/packages/router/Cargo.toml @@ -36,7 +36,8 @@ web = ["gloo", "web-sys", "wasm-bindgen", "gloo-utils", "js-sys"] [dev-dependencies] dioxus = { path = "../dioxus" } dioxus-ssr = { path = "../ssr" } -criterion = "0.3" +dioxus-router = { path = ".", features = ["ssr"] } +criterion = { verison = "0.5", features = ["async_tokio", "html_reports"] } [[bench]] name = "incremental" diff --git a/packages/router/benches/incremental.rs b/packages/router/benches/incremental.rs index 5706bfd3a..0694e3636 100644 --- a/packages/router/benches/incremental.rs +++ b/packages/router/benches/incremental.rs @@ -6,7 +6,7 @@ use dioxus::prelude::*; use dioxus_router::prelude::*; use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use dioxus_router::incremental::{DefaultRenderer, IncrementalRenderer}; + use dioxus_ssr::Renderer; pub fn criterion_benchmark(c: &mut Criterion) { @@ -31,43 +31,53 @@ pub fn criterion_benchmark(c: &mut Criterion) { .build(); b.iter(|| { - for id in 0..100 { - for id in 0..10 { - renderer - .render(Route::Post { id }, &mut std::io::sink()) - .unwrap(); + tokio::runtime::Runtime::new().unwrap().block_on(async { + for id in 0..1000 { + render_route( + &mut renderer, + Route::Post { id }, + &mut tokio::io::sink(), + |_| {}, + ) + .await + .unwrap(); } - } + }) }) }); c.bench_function("build 1000 routes no memory cache", |b| { - let mut renderer = IncrementalRenderer::builder(DefaultRenderer { - before_body: r#" - - - - - Dioxus Application - - "# - .to_string(), - after_body: r#" - "# - .to_string(), - }) - .static_dir("./static") - .memory_cache_limit(0) - .invalidate_after(Duration::from_secs(10)) - .build(); - - b.iter(|| { - for id in 0..1000 { - renderer - .render(Route::Post { id }, &mut std::io::sink()) + b.to_async(tokio::runtime::Runtime::new().unwrap()) + .iter(|| async { + let mut renderer = IncrementalRenderer::builder(DefaultRenderer { + before_body: r#" + + + + + Dioxus Application + + "# + .to_string(), + after_body: r#" + "# + .to_string(), + }) + .static_dir("./static") + .memory_cache_limit(0) + .invalidate_after(Duration::from_secs(10)) + .build(); + for id in 0..1000 { + render_route( + &mut renderer, + Route::Post { id }, + &mut tokio::io::sink(), + |_| {}, + ) + .await .unwrap(); - } - }) + } + }) }); c.bench_function("build 1000 routes no cache", |b| { let mut renderer = Renderer::default(); @@ -94,9 +104,10 @@ pub fn criterion_benchmark(c: &mut Criterion) { }) }); c.bench_function("cache static", |b| { - b.iter(|| { - let mut renderer = IncrementalRenderer::builder(DefaultRenderer { - before_body: r#" + b.to_async(tokio::runtime::Runtime::new().unwrap()) + .iter(|| async { + let mut renderer = IncrementalRenderer::builder(DefaultRenderer { + before_body: r#" @@ -105,16 +116,18 @@ pub fn criterion_benchmark(c: &mut Criterion) { Dioxus Application "# - .to_string(), - after_body: r#" + .to_string(), + after_body: r#" "# - .to_string(), - }) - .static_dir("./static") - .build(); + .to_string(), + }) + .static_dir("./static") + .build(); - renderer.pre_cache_static_routes::().unwrap(); - }) + pre_cache_static_routes::(&mut renderer) + .await + .unwrap(); + }) }); } diff --git a/packages/router/examples/static_generation.rs b/packages/router/examples/static_generation.rs index 97a88f1e7..30d503f7f 100644 --- a/packages/router/examples/static_generation.rs +++ b/packages/router/examples/static_generation.rs @@ -7,7 +7,8 @@ use dioxus_router::prelude::*; use dioxus_ssr::incremental::{DefaultRenderer, IncrementalRendererConfig}; -fn main() { +#[tokio::main] +async fn main() { let mut renderer = IncrementalRendererConfig::new(DefaultRenderer { before_body: r#" @@ -27,13 +28,20 @@ fn main() { .invalidate_after(Duration::from_secs(10)) .build(); - renderer.pre_cache_static_routes::().unwrap(); + pre_cache_static_routes::(&mut renderer) + .await + .unwrap(); for _ in 0..1_000_000 { for id in 0..10 { - renderer - .render(Route::Post { id }, &mut std::io::sink()) - .unwrap(); + render_route( + &mut renderer, + Route::Post { id }, + &mut tokio::io::sink(), + |_| {}, + ) + .await + .unwrap(); } } } diff --git a/packages/router/src/incremental.rs b/packages/router/src/incremental.rs index 6e2adf567..e4f867bbd 100644 --- a/packages/router/src/incremental.rs +++ b/packages/router/src/incremental.rs @@ -9,7 +9,7 @@ use dioxus_ssr::incremental::{ use crate::prelude::*; /// Pre-cache all static routes. -pub async fn pre_cache_static_routes( +pub async fn pre_cache_static_routes( renderer: &mut IncrementalRenderer, ) -> Result<(), IncrementalRendererError> where diff --git a/packages/router/src/lib.rs b/packages/router/src/lib.rs index 1327f9a93..1310f64bd 100644 --- a/packages/router/src/lib.rs +++ b/packages/router/src/lib.rs @@ -63,7 +63,9 @@ pub mod prelude { pub use dioxus_router_macro::Routable; #[cfg(feature = "ssr")] - pub use dioxus_ssr::incremental::IncrementalRendererConfig; + pub use crate::incremental::*; + #[cfg(feature = "ssr")] + pub use dioxus_ssr::incremental::*; #[doc(hidden)] /// A component with props used in the macro