Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions crates/egui_demo_lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,16 @@ The demo library is a separate crate for three reasons:
* To remove the amount of code in `egui` proper.
* To make it easy for 3rd party egui integrations to use it for tests.
- See for instance https://github.com/not-fl3/egui-miniquad/blob/master/examples/demo.rs

This crate also contains benchmarks for egui.
Run them with
```bash
# Run all benchmarks
cargo bench -p egui_demo_lib

# Run a single benchmark
cargo bench -p egui_demo_lib "benchmark name"

# Profile benchmarks with cargo-flamegraph (--root flag is necessary for MacOS)
CARGO_PROFILE_BENCH_DEBUG=true cargo flamegraph --bench benchmark --root -p egui_demo_lib -- --bench "benchmark name"
```
65 changes: 58 additions & 7 deletions crates/egui_demo_lib/benches/benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::fmt::Write as _;

use criterion::{criterion_group, criterion_main, Criterion};
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};

use egui::epaint::TextShape;
use egui::load::SizedTexture;
use egui::{Button, TextureId, UiBuilder, Vec2};
use egui_demo_lib::LOREM_IPSUM_LONG;
use rand::Rng as _;

Expand Down Expand Up @@ -57,14 +59,63 @@ pub fn criterion_benchmark(c: &mut Criterion) {
let _ = ctx.run(RawInput::default(), |ctx| {
egui::CentralPanel::default().show(ctx, |ui| {
c.bench_function("label &str", |b| {
b.iter(|| {
ui.label("the quick brown fox jumps over the lazy dog");
});
b.iter_batched_ref(
|| ui.new_child(UiBuilder::new()),
|ui| {
ui.label("the quick brown fox jumps over the lazy dog");
},
BatchSize::LargeInput,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to explain why we create a child ui. The best way to explain it would be to test it (i.e. make sure there are more shapes after the call to ui.label then before it

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and below

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I explain it in the create_benchmark_ui doc comment

)
});
c.bench_function("label format!", |b| {
b.iter(|| {
ui.label("the quick brown fox jumps over the lazy dog".to_owned());
});
b.iter_batched_ref(
|| ui.new_child(UiBuilder::new()),
|ui| {
ui.label("the quick brown fox jumps over the lazy dog".to_owned());
},
BatchSize::LargeInput,
)
});
});
});
}

{
let ctx = egui::Context::default();
let _ = ctx.run(RawInput::default(), |ctx| {
egui::CentralPanel::default().show(ctx, |ui| {
let mut group = c.benchmark_group("button");

// To ensure we have a valid image, let's use the font texture. The size
// shouldn't be important for this benchmark.
let image = SizedTexture::new(TextureId::default(), Vec2::splat(16.0));

group.bench_function("button", |b| {
b.iter_batched_ref(
|| ui.new_child(UiBuilder::new()),
|ui| {
ui.add(Button::new("Hello World"));
},
BatchSize::LargeInput,
);
});
group.bench_function("button image", |b| {
b.iter_batched_ref(
|| ui.new_child(UiBuilder::new()),
|ui| {
ui.add(Button::image_and_text(image, "Hello World"));
},
BatchSize::LargeInput,
)
});
group.bench_function("button image right text", |b| {
b.iter_batched_ref(
|| ui.new_child(UiBuilder::new()),
|ui| {
ui.add(Button::image_and_text(image, "Hello World").right_text("⏵"));
},
BatchSize::LargeInput,
)
});
});
});
Expand Down