From db83c4d14a352e816aa1bc13d1fe08a7ed745b6b Mon Sep 17 00:00:00 2001 From: rhysd Date: Sun, 10 Jul 2022 21:54:17 +0900 Subject: [PATCH] add benchmark suites for text search --- .cargo/config.toml | 2 +- bench/Cargo.toml | 6 +++- bench/benches/insert.rs | 36 ++++-------------------- bench/benches/search.rs | 62 +++++++++++++++++++++++++++++++++++++++++ bench/src/lib.rs | 26 +++++++++++++++++ 5 files changed, 99 insertions(+), 33 deletions(-) create mode 100644 bench/benches/search.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index c3046d7..4d06f1c 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,3 +1,3 @@ [alias] -watch-check = ["watch", "-x", "check --examples --all-features"] +watch-check = ["watch", "-x", "check --examples --all-features --all --benches"] watch-test = ["watch", "-x", "test --all-features -- --skip=src/lib.rs", "-w", "src"] diff --git a/bench/Cargo.toml b/bench/Cargo.toml index 60b5158..4b095f6 100644 --- a/bench/Cargo.toml +++ b/bench/Cargo.toml @@ -9,7 +9,7 @@ license = "MIT" bench = false [dependencies] -tui-textarea = { path = "..", default-features = false } +tui-textarea = { path = "..", default-features = false, features = ["search"] } tui = { version = "0.18", default-features = false } [dev-dependencies] @@ -19,3 +19,7 @@ rand = { version = "0.8.5", features = ["small_rng"] } [[bench]] name = "insert" harness = false + +[[bench]] +name = "search" +harness = false diff --git a/bench/benches/insert.rs b/bench/benches/insert.rs index 9a79011..e40b416 100644 --- a/bench/benches/insert.rs +++ b/bench/benches/insert.rs @@ -2,21 +2,7 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; use rand::rngs::SmallRng; use rand::{Rng, SeedableRng}; use tui_textarea::{CursorMove, Input, Key, TextArea}; -use tui_textarea_bench::dummy_terminal; - -const LOREM: &[&str] = &[ - "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do", - "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim", - "ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut", - "aliquip ex ea commodo consequat. Duis aute irure dolor in", - "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla", - "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in", - "culpa qui officia deserunt mollit anim id est laborum.", -]; -const SEED: [u8; 32] = [ - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, - 27, 28, 29, 30, 31, 32, -]; +use tui_textarea_bench::{dummy_terminal, TerminalExt, LOREM, SEED}; #[inline] fn append_lorem(repeat: usize) -> usize { @@ -30,10 +16,7 @@ fn append_lorem(repeat: usize) -> usize { ctrl: false, alt: false, }); - term.draw(|f| { - f.render_widget(textarea.widget(), f.size()); - }) - .unwrap(); + term.draw_textarea(&textarea); } } textarea.input(Input { @@ -41,10 +24,7 @@ fn append_lorem(repeat: usize) -> usize { ctrl: false, alt: false, }); - term.draw(|f| { - f.render_widget(textarea.widget(), f.size()); - }) - .unwrap(); + term.draw_textarea(&textarea); } textarea.lines().len() } @@ -66,10 +46,7 @@ fn random_lorem(repeat: usize) -> usize { ctrl: false, alt: false, }); - term.draw(|f| { - f.render_widget(textarea.widget(), f.size()); - }) - .unwrap(); + term.draw_textarea(&textarea); for c in line.chars() { textarea.input(Input { @@ -77,10 +54,7 @@ fn random_lorem(repeat: usize) -> usize { ctrl: false, alt: false, }); - term.draw(|f| { - f.render_widget(textarea.widget(), f.size()); - }) - .unwrap(); + term.draw_textarea(&textarea); } } } diff --git a/bench/benches/search.rs b/bench/benches/search.rs new file mode 100644 index 0000000..ac92490 --- /dev/null +++ b/bench/benches/search.rs @@ -0,0 +1,62 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use tui_textarea::TextArea; +use tui_textarea_bench::{dummy_terminal, TerminalExt, LOREM}; + +fn forward(c: &mut Criterion) { + #[inline] + fn run(pat: &str, lines: Vec) { + let mut term = dummy_terminal(); + let mut textarea: TextArea = TextArea::new(lines); + textarea.set_search_pattern(pat).unwrap(); + term.draw_textarea(&textarea); + for _ in 0..100 { + textarea.search_forward(false); + term.draw_textarea(&textarea); + } + textarea.set_search_pattern(r"").unwrap(); + term.draw_textarea(&textarea); + } + + c.bench_function("search::forward_short", |b| { + let lines: Vec<_> = LOREM.iter().map(|s| s.to_string()).collect(); + b.iter(move || run(r"\w*i\w*", lines.clone())) + }); + c.bench_function("search::forward_long", |b| { + let mut lines = vec![]; + for _ in 0..10 { + lines.extend(LOREM.iter().map(|s| s.to_string())); + } + b.iter(move || run(r"[A-Z]\w*", lines.clone())) + }); +} + +fn back(c: &mut Criterion) { + #[inline] + fn run(pat: &str, lines: Vec) { + let mut term = dummy_terminal(); + let mut textarea: TextArea = TextArea::new(lines); + textarea.set_search_pattern(pat).unwrap(); + term.draw_textarea(&textarea); + for _ in 0..100 { + textarea.search_back(false); + term.draw_textarea(&textarea); + } + textarea.set_search_pattern(r"").unwrap(); + term.draw_textarea(&textarea); + } + + c.bench_function("search::back_short", |b| { + let lines: Vec<_> = LOREM.iter().map(|s| s.to_string()).collect(); + b.iter(move || run(r"\w*i\w*", lines.clone())) + }); + c.bench_function("search::back_long", |b| { + let mut lines = vec![]; + for _ in 0..10 { + lines.extend(LOREM.iter().map(|s| s.to_string())); + } + b.iter(move || run(r"[A-Z]\w*", lines.clone())) + }); +} + +criterion_group!(search, forward, back); +criterion_main!(search); diff --git a/bench/src/lib.rs b/bench/src/lib.rs index 3be47a8..dba3a67 100644 --- a/bench/src/lib.rs +++ b/bench/src/lib.rs @@ -6,6 +6,21 @@ use tui::backend::Backend; use tui::buffer::Cell; use tui::layout::Rect; use tui::Terminal; +use tui_textarea::TextArea; + +pub const LOREM: &[&str] = &[ + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do", + "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim", + "ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut", + "aliquip ex ea commodo consequat. Duis aute irure dolor in", + "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla", + "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in", + "culpa qui officia deserunt mollit anim id est laborum.", +]; +pub const SEED: [u8; 32] = [ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, + 27, 28, 29, 30, 31, 32, +]; pub struct DummyBackend { width: u16, @@ -69,3 +84,14 @@ impl Backend for DummyBackend { pub fn dummy_terminal() -> Terminal { Terminal::new(DummyBackend::default()).unwrap() } + +pub trait TerminalExt { + fn draw_textarea(&mut self, textarea: &TextArea<'_>); +} + +impl TerminalExt for Terminal { + fn draw_textarea(&mut self, textarea: &TextArea<'_>) { + self.draw(|f| f.render_widget(textarea.widget(), f.size())) + .unwrap(); + } +}