Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(bench): Switch to Divan #928

Merged
merged 1 commit into from Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
289 changes: 49 additions & 240 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions crates/typos-cli/Cargo.toml
Expand Up @@ -79,16 +79,16 @@ encoding_rs = "0.8.33"

[dev-dependencies]
assert_fs = "1.1"
trycmd = "0.14.20"
criterion = "0.5"
divan = "0.1.11"
snapbox = "0.4.16"
trycmd = "0.14.20"

[[bench]]
name = "checks"
name = "check_file"
harness = false

[[bench]]
name = "corrections"
name = "correct_word"
harness = false

[[bench]]
Expand Down
93 changes: 93 additions & 0 deletions crates/typos-cli/benches/check_file.rs
@@ -0,0 +1,93 @@
mod data;

use assert_fs::prelude::*;
use typos_cli::file::FileChecker;

#[divan::bench(args = data::DATA)]
fn found_files(bencher: divan::Bencher, sample: &data::Data) {
let dict = typos_cli::dict::BuiltIn::new(Default::default());
let tokenizer = typos::tokens::Tokenizer::new();
let policy = typos_cli::policy::Policy::new()
.dict(&dict)
.tokenizer(&tokenizer);

let temp = assert_fs::TempDir::new().unwrap();
let sample_path = temp.child(sample.name());
sample_path.write_str(sample.content()).unwrap();

bencher
.counter(divan::counter::BytesCount::of_str(sample.content()))
.bench_local(|| {
typos_cli::file::FoundFiles.check_file(sample_path.path(), true, &policy, &PrintSilent)
})
}

#[divan::bench(args = data::DATA)]
fn identifiers(bencher: divan::Bencher, sample: &data::Data) {
let dict = typos_cli::dict::BuiltIn::new(Default::default());
let tokenizer = typos::tokens::Tokenizer::new();
let policy = typos_cli::policy::Policy::new()
.dict(&dict)
.tokenizer(&tokenizer);

let temp = assert_fs::TempDir::new().unwrap();
let sample_path = temp.child(sample.name());
sample_path.write_str(sample.content()).unwrap();

bencher
.counter(divan::counter::BytesCount::of_str(sample.content()))
.bench_local(|| {
typos_cli::file::Identifiers.check_file(sample_path.path(), true, &policy, &PrintSilent)
})
}

#[divan::bench(args = data::DATA)]
fn words(bencher: divan::Bencher, sample: &data::Data) {
let dict = typos_cli::dict::BuiltIn::new(Default::default());
let tokenizer = typos::tokens::Tokenizer::new();
let policy = typos_cli::policy::Policy::new()
.dict(&dict)
.tokenizer(&tokenizer);

let temp = assert_fs::TempDir::new().unwrap();
let sample_path = temp.child(sample.name());
sample_path.write_str(sample.content()).unwrap();

bencher
.counter(divan::counter::BytesCount::of_str(sample.content()))
.bench_local(|| {
typos_cli::file::Words.check_file(sample_path.path(), true, &policy, &PrintSilent)
})
}

#[divan::bench(args = data::DATA)]
fn typos(bencher: divan::Bencher, sample: &data::Data) {
let dict = typos_cli::dict::BuiltIn::new(Default::default());
let tokenizer = typos::tokens::Tokenizer::new();
let policy = typos_cli::policy::Policy::new()
.dict(&dict)
.tokenizer(&tokenizer);

let temp = assert_fs::TempDir::new().unwrap();
let sample_path = temp.child(sample.name());
sample_path.write_str(sample.content()).unwrap();

bencher
.counter(divan::counter::BytesCount::of_str(sample.content()))
.bench_local(|| {
typos_cli::file::Typos.check_file(sample_path.path(), true, &policy, &PrintSilent)
})
}

#[derive(Debug, Default)]
pub struct PrintSilent;

impl typos_cli::report::Report for PrintSilent {
fn report(&self, _msg: typos_cli::report::Message) -> Result<(), std::io::Error> {
Ok(())
}
}

fn main() {
divan::main();
}
69 changes: 0 additions & 69 deletions crates/typos-cli/benches/checks.rs

This file was deleted.

155 changes: 155 additions & 0 deletions crates/typos-cli/benches/correct_word.rs
@@ -0,0 +1,155 @@
mod regular {
mod ok {
#[divan::bench]
fn en(bencher: divan::Bencher) {
let input = "finalizes";
let locale = typos_cli::config::Locale::En;
let corrections = typos_cli::dict::BuiltIn::new(locale);
let input = typos::tokens::Word::new(input, 0).unwrap();
#[cfg(feature = "vars")]
assert_eq!(corrections.correct_word(input), None);
bencher
.with_inputs(|| input)
.bench_local_values(|input| corrections.correct_word(input));
}

#[divan::bench]
#[cfg(feature = "vars")]
fn en_us(bencher: divan::Bencher) {
let input = "finalizes";
let locale = typos_cli::config::Locale::EnUs;
let corrections = typos_cli::dict::BuiltIn::new(locale);
let input = typos::tokens::Word::new(input, 0).unwrap();
#[cfg(feature = "vars")]
assert_eq!(corrections.correct_word(input), Some(typos::Status::Valid));
bencher
.with_inputs(|| input)
.bench_local_values(|input| corrections.correct_word(input));
}
}

mod misspell {
#[divan::bench]
fn en(bencher: divan::Bencher) {
let input = "finallizes";
let output = "finalizes";
let locale = typos_cli::config::Locale::En;
let corrections = typos_cli::dict::BuiltIn::new(locale);
let input = typos::tokens::Word::new(input, 0).unwrap();
assert_eq!(
corrections.correct_word(input),
Some(typos::Status::Corrections(vec![
std::borrow::Cow::Borrowed(output)
]))
);
bencher
.with_inputs(|| input)
.bench_local_values(|input| corrections.correct_word(input));
}

#[divan::bench]
#[cfg(feature = "vars")]
fn en_us(bencher: divan::Bencher) {
let input = "finallizes";
let output = "finalizes";
let locale = typos_cli::config::Locale::EnUs;
let corrections = typos_cli::dict::BuiltIn::new(locale);
let input = typos::tokens::Word::new(input, 0).unwrap();
assert_eq!(
corrections.correct_word(input),
Some(typos::Status::Corrections(vec![
std::borrow::Cow::Borrowed(output)
]))
);
bencher
.with_inputs(|| input)
.bench_local_values(|input| corrections.correct_word(input));
}
}

mod misspell_case {
#[divan::bench]
fn en(bencher: divan::Bencher) {
let input = "FINALLIZES";
let output = "FINALIZES";
let locale = typos_cli::config::Locale::En;
let corrections = typos_cli::dict::BuiltIn::new(locale);
let input = typos::tokens::Word::new(input, 0).unwrap();
assert_eq!(
corrections.correct_word(input),
Some(typos::Status::Corrections(vec![
std::borrow::Cow::Borrowed(output)
]))
);
bencher
.with_inputs(|| input)
.bench_local_values(|input| corrections.correct_word(input));
}

#[divan::bench]
#[cfg(feature = "vars")]
fn en_us(bencher: divan::Bencher) {
let input = "FINALLIZES";
let output = "FINALIZES";
let locale = typos_cli::config::Locale::EnUs;
let corrections = typos_cli::dict::BuiltIn::new(locale);
let input = typos::tokens::Word::new(input, 0).unwrap();
assert_eq!(
corrections.correct_word(input),
Some(typos::Status::Corrections(vec![
std::borrow::Cow::Borrowed(output)
]))
);
bencher
.with_inputs(|| input)
.bench_local_values(|input| corrections.correct_word(input));
}
}
}

#[cfg(feature = "vars")]
mod varcon {
mod ok {
#[divan::bench]
fn en_gb(bencher: divan::Bencher) {
let input = "finalizes";
let output = "finalises";
let locale = typos_cli::config::Locale::EnGb;
let corrections = typos_cli::dict::BuiltIn::new(locale);
let input = typos::tokens::Word::new(input, 0).unwrap();
assert_eq!(
corrections.correct_word(input),
Some(typos::Status::Corrections(vec![
std::borrow::Cow::Borrowed(output)
]))
);
bencher
.with_inputs(|| input)
.bench_local_values(|input| corrections.correct_word(input));
}
}

mod misspell {
#[divan::bench]
fn en_gb(bencher: divan::Bencher) {
let input = "finallizes";
let output = "finalises";
let locale = typos_cli::config::Locale::EnGb;
let corrections = typos_cli::dict::BuiltIn::new(locale);
let input = typos::tokens::Word::new(input, 0).unwrap();
assert_eq!(
corrections.correct_word(input),
Some(typos::Status::Corrections(vec![
std::borrow::Cow::Borrowed(output)
]))
);
bencher
.with_inputs(|| input)
.bench_local_values(|input| corrections.correct_word(input));
}
}
}

fn main() {
divan::main();
}