From 73572c0c28e805daaa5d63cefcbdb147de38265f Mon Sep 17 00:00:00 2001 From: Taj Pereira Date: Wed, 27 Nov 2024 07:02:27 +0900 Subject: [PATCH 1/6] Add prepare benchmarks --- Cargo.toml | 9 +++ benches/prepare.rs | 120 ++++++++++++++++++++++++++++ benches/state.rs | 35 ++++++++ samples/arabic.txt | 3 + samples/moby_dick.txt | 181 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 348 insertions(+) create mode 100644 benches/prepare.rs create mode 100644 benches/state.rs create mode 100644 samples/arabic.txt create mode 100644 samples/moby_dick.txt diff --git a/Cargo.toml b/Cargo.toml index fa70fca..3f75710 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,3 +19,12 @@ winit = "0.30.3" wgpu = "23" resvg = { version = "0.44", default-features = false } pollster = "0.4.0" +criterion = { version = "0.5", features = ["html_reports"] } + +[profile.profiling] +inherits = "release" +debug = true + +[[bench]] +name = "prepare" +harness = false diff --git a/benches/prepare.rs b/benches/prepare.rs new file mode 100644 index 0000000..487bcbb --- /dev/null +++ b/benches/prepare.rs @@ -0,0 +1,120 @@ +use cosmic_text::{Attrs, Buffer, Color, Family, FontSystem, Metrics, Shaping, SwashCache}; +use criterion::{criterion_group, criterion_main, Criterion}; +use glyphon::{ + Cache, ColorMode, Resolution, TextArea, TextAtlas, TextBounds, TextRenderer, Viewport, Weight, +}; +use wgpu::{MultisampleState, TextureFormat}; + +mod state; + +fn run_bench(ctx: &mut Criterion) { + let mut group = ctx.benchmark_group("Prepare"); + + let state = state::State::new(); + + // Set up text renderer + let mut font_system = FontSystem::new(); + let mut swash_cache = SwashCache::new(); + let cache = Cache::new(&state.device); + let mut viewport = Viewport::new(&state.device, &cache); + let mut atlas = TextAtlas::with_color_mode( + &state.device, + &state.queue, + &cache, + TextureFormat::Bgra8Unorm, + ColorMode::Web, + ); + let mut text_renderer = + TextRenderer::new(&mut atlas, &state.device, MultisampleState::default(), None); + + let attrs = Attrs::new() + .family(Family::SansSerif) + .weight(Weight::NORMAL); + let shaping = Shaping::Advanced; + viewport.update( + &state.queue, + Resolution { + width: 1000, + height: 1000, + }, + ); + + for (test_name, text_areas) in &[ + ( + "Moby Dick Chapter 1 - Single Text Area", + vec![include_str!("../samples/moby_dick.txt")], + ), + ( + "Arabic - Single Text Area", + vec![include_str!("../samples/arabic.txt")], + ), + ( + "Moby Dick Chapter 1 - Many Text Areas", + include_str!("../samples/moby_dick.txt") + .repeat(100) + .split('\n') + .collect(), + ), + ( + "Arabic - Many Text Areas", + include_str!("../samples/arabic.txt") + .repeat(1000) + .split('\n') + .collect(), + ), + ] { + let buffers: Vec = text_areas + .iter() + .copied() + .map(|s| { + let mut text_buffer = Buffer::new(&mut font_system, Metrics::relative(1.0, 10.0)); + text_buffer.set_size(&mut font_system, Some(20.0), None); + text_buffer.set_text(&mut font_system, s, attrs, shaping); + text_buffer.shape_until_scroll(&mut font_system, false); + text_buffer + }) + .collect(); + + group.bench_function(*test_name, |b| { + b.iter(|| { + let text_areas: Vec