Improve benchmarks to also test for updating many elements at once

This commit is contained in:
Evan Almloff committed 2023-03-12 16:56:02 -05:00
1 parent c4845d5c8d
commit 0f5833d4c0
1 file changed
+42 -5
+42 -5
View File
@@ -7,9 +7,9 @@ criterion_main!(mbenches);
/// This benchmarks the cache performance of the TUI for small edits by changing one box at a time. /// This benchmarks the cache performance of the TUI for small edits by changing one box at a time.
fn tui_update(c: &mut Criterion) { fn tui_update(c: &mut Criterion) {
{
let mut group = c.benchmark_group("Update boxes"); let mut group = c.benchmark_group("Update boxes");
// We can also use loops to define multiple benchmarks, even over multiple dimensions.
for size in 1..=20usize { for size in 1..=20usize {
let parameter_string = format!("{}", (size).pow(2)); let parameter_string = format!("{}", (size).pow(2));
group.bench_with_input( group.bench_with_input(
@@ -17,11 +17,44 @@ fn tui_update(c: &mut Criterion) {
&size, &size,
|b, size| { |b, size| {
b.iter(|| { b.iter(|| {
dioxus_tui::launch_cfg_with_props(app, *size, Config::default().with_headless()) dioxus_tui::launch_cfg_with_props(
app,
GridProps {
size: *size,
update_count: 1,
},
Config::default().with_headless(),
)
}) })
}, },
); );
} }
}
{
let mut group = c.benchmark_group("Update many boxes");
for update_count in 1..=20usize {
let update_count = update_count * 20;
let parameter_string = update_count.to_string();
group.bench_with_input(
BenchmarkId::new("update count", parameter_string),
&update_count,
|b, update_count| {
b.iter(|| {
dioxus_tui::launch_cfg_with_props(
app,
GridProps {
size: 20,
update_count: *update_count,
},
Config::default().with_headless(),
)
})
},
);
}
}
} }
#[derive(Props, PartialEq)] #[derive(Props, PartialEq)]
@@ -58,6 +91,7 @@ fn Box(cx: Scope<BoxProps>) -> Element {
#[derive(Props, PartialEq)] #[derive(Props, PartialEq)]
struct GridProps { struct GridProps {
size: usize, size: usize,
update_count: usize,
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
fn Grid(cx: Scope<GridProps>) -> Element { fn Grid(cx: Scope<GridProps>) -> Element {
@@ -66,9 +100,10 @@ fn Grid(cx: Scope<GridProps>) -> Element {
let counts = use_ref(cx, || vec![0; size * size]); let counts = use_ref(cx, || vec![0; size * size]);
let ctx: TuiContext = cx.consume_context().unwrap(); let ctx: TuiContext = cx.consume_context().unwrap();
if *count.get() + 1 >= (size * size) { if *count.get() + cx.props.update_count >= (size * size) {
ctx.quit(); ctx.quit();
} else { } else {
for _ in 0..cx.props.update_count {
counts.with_mut(|c| { counts.with_mut(|c| {
let i = *count.current(); let i = *count.current();
c[i] += 1; c[i] += 1;
@@ -79,6 +114,7 @@ fn Grid(cx: Scope<GridProps>) -> Element {
*i %= size * size; *i %= size * size;
}); });
} }
}
render! { render! {
div{ div{
@@ -115,13 +151,14 @@ fn Grid(cx: Scope<GridProps>) -> Element {
} }
} }
fn app(cx: Scope<usize>) -> Element { fn app(cx: Scope<GridProps>) -> Element {
cx.render(rsx! { cx.render(rsx! {
div{ div{
width: "100%", width: "100%",
height: "100%", height: "100%",
Grid{ Grid{
size: *cx.props, size: cx.props.size,
update_count: cx.props.update_count,
} }
} }
}) })