Skip to content

Commit

Permalink
Add benchmarks with brunch
Browse files Browse the repository at this point in the history
  • Loading branch information
the-mikedavis committed Aug 26, 2023
1 parent 0a97845 commit 7e90ed2
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
/result
/.direnv/
/bench/target
2 changes: 1 addition & 1 deletion .helix/config.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
editor.workspace-lsp-roots = ["examples"]
editor.workspace-lsp-roots = ["examples", "bench"]
62 changes: 62 additions & 0 deletions bench/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions bench/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "benches"
version = "0.1.0"
edition = "2021"
# Needed for `std::hint::black_box`.
rust-version = "1.66"

[dependencies]
spellbook = { path = "../" }
brunch = "0.5"
xdg = "2.5"
3 changes: 3 additions & 0 deletions bench/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Benchmarks for the spellbook API. Requires Rust 1.66 or higher.

Run with `cargo run --release` in this `bench` directory.
60 changes: 60 additions & 0 deletions bench/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::hint::black_box;

use brunch::Bench;
use spellbook::Dictionary;

const SAMPLES: u32 = 500_000;

fn main() {
let base = xdg::BaseDirectories::new().expect("Could not determine XDG directories");
let (dic_path, aff_path) = match base.get_data_dirs().iter().find_map(|dir| {
let subdir = dir.join("hunspell");
if !subdir.is_dir() {
return None;
}

let dic = subdir.join("en_US.dic");
let aff = subdir.join("en_US.aff");
if dic.is_file() && aff.is_file() {
Some((dic, aff))
} else {
None
}
}) {
Some((dic, aff)) => (dic, aff),
None => {
eprintln!("Could not find the en_US dictionary");
std::process::exit(1);
}
};
let dic_text = std::fs::read_to_string(dic_path).unwrap();
let aff_text = std::fs::read_to_string(aff_path).unwrap();
let dict = Dictionary::compile(&aff_text, &dic_text).unwrap();

eprintln!("Starting benchmarks...");
eprintln!();
let now = std::time::Instant::now();
brunch::benches!(
inline:

// Compilation
Bench::new("Compile en_US")
.run(|| Dictionary::compile(black_box(&aff_text), black_box(&dic_text))),
Bench::spacer(),

// Checking
Bench::new("In-dictionary word (\"drink\")")
.with_samples(SAMPLES)
.run(|| dict.check(black_box("drink"))),
Bench::new("Word with a suffix (\"drinkable\")")
.with_samples(SAMPLES)
.run(|| dict.check(black_box("drinkable"))),
Bench::new("Multi-affix (\"undrinkable\")")
.with_samples(SAMPLES)
.run(|| dict.check(black_box("undrinkable"))),
Bench::new("Incorrect prefix (\"undrink\")")
.with_samples(SAMPLES)
.run(|| dict.check(black_box("undrink"))),
);
eprintln!("Finished in {:.1}s", now.elapsed().as_secs_f64());
}

0 comments on commit 7e90ed2

Please sign in to comment.