summaryrefslogtreecommitdiffstats
path: root/third_party/rust/mapped_hyph/benches/bench.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/mapped_hyph/benches/bench.rs
parentInitial commit. (diff)
downloadfirefox-esr-upstream.tar.xz
firefox-esr-upstream.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/mapped_hyph/benches/bench.rs')
-rw-r--r--third_party/rust/mapped_hyph/benches/bench.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/third_party/rust/mapped_hyph/benches/bench.rs b/third_party/rust/mapped_hyph/benches/bench.rs
new file mode 100644
index 0000000000..cf4ad6cb2f
--- /dev/null
+++ b/third_party/rust/mapped_hyph/benches/bench.rs
@@ -0,0 +1,50 @@
+// Any copyright to the test code below is dedicated to the Public Domain.
+// http://creativecommons.org/publicdomain/zero/1.0/
+
+use criterion::black_box;
+use criterion::criterion_group;
+use criterion::criterion_main;
+use criterion::BenchmarkId;
+use criterion::Criterion;
+
+use mapped_hyph::Hyphenator;
+use std::fs;
+
+const SAMPLE_SIZE: usize = 300;
+const DIC_PATH: &str = "hyph_en_US.hyf";
+
+fn bench_construct(c: &mut Criterion) {
+ c.bench_function("construct", |b| {
+ b.iter(|| {
+ let dic = unsafe { mapped_hyph::load_file(DIC_PATH) }
+ .expect(&format!("failed to load dictionary {}", DIC_PATH));
+ let _ = Hyphenator::new(black_box(&*dic));
+ })
+ });
+}
+
+fn bench_find_hyphen_values(c: &mut Criterion) {
+ // XXX: Should we copy this file to the crate to ensure reproducability?
+ let data = fs::read_to_string("/usr/share/dict/words").expect("File reading failed.");
+ let words: Vec<&str> = data.lines().take(SAMPLE_SIZE).collect();
+
+ let dic = unsafe { mapped_hyph::load_file(DIC_PATH) }
+ .expect(&format!("failed to load dictionary {}", DIC_PATH));
+ let hyph = Hyphenator::new(&*dic);
+
+ c.bench_with_input(
+ BenchmarkId::new("bench_word", SAMPLE_SIZE),
+ &words,
+ |b, words| {
+ b.iter(|| {
+ let mut values: Vec<u8> = vec![0; 1000];
+ for w in words {
+ hyph.find_hyphen_values(&w, &mut values);
+ }
+ });
+ },
+ );
+}
+
+criterion_group!(benches, bench_construct, bench_find_hyphen_values,);
+criterion_main!(benches);