fix router examples

This commit is contained in:
Evan Almloff 2023-06-24 13:44:22 -07:00
parent 7e3c45eb7e
commit e83866b986
6 changed files with 77 additions and 52 deletions

View file

@ -1 +1,2 @@
dist/
static

View file

@ -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"

View file

@ -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#"<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Dioxus Application</title>
</head>
<body>"#
.to_string(),
after_body: r#"</body>
</html>"#
.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#"<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Dioxus Application</title>
</head>
<body>"#
.to_string(),
after_body: r#"</body>
</html>"#
.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#"<!DOCTYPE html>
b.to_async(tokio::runtime::Runtime::new().unwrap())
.iter(|| async {
let mut renderer = IncrementalRenderer::builder(DefaultRenderer {
before_body: r#"<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
@ -105,16 +116,18 @@ pub fn criterion_benchmark(c: &mut Criterion) {
<title>Dioxus Application</title>
</head>
<body>"#
.to_string(),
after_body: r#"</body>
.to_string(),
after_body: r#"</body>
</html>"#
.to_string(),
})
.static_dir("./static")
.build();
.to_string(),
})
.static_dir("./static")
.build();
renderer.pre_cache_static_routes::<Route>().unwrap();
})
pre_cache_static_routes::<Route, _>(&mut renderer)
.await
.unwrap();
})
});
}

View file

@ -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#"<!DOCTYPE html>
<html lang="en">
@ -27,13 +28,20 @@ fn main() {
.invalidate_after(Duration::from_secs(10))
.build();
renderer.pre_cache_static_routes::<Route>().unwrap();
pre_cache_static_routes::<Route, _>(&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();
}
}
}

View file

@ -9,7 +9,7 @@ use dioxus_ssr::incremental::{
use crate::prelude::*;
/// Pre-cache all static routes.
pub async fn pre_cache_static_routes<R: RenderHTML + Send, Rt>(
pub async fn pre_cache_static_routes<Rt, R: RenderHTML + Send>(
renderer: &mut IncrementalRenderer<R>,
) -> Result<(), IncrementalRendererError>
where

View file

@ -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