Skip to content

Commit 7cc583f

Browse files
committed
test(bench): Switch to Divan
1 parent 443e650 commit 7cc583f

File tree

8 files changed

+411
-515
lines changed

8 files changed

+411
-515
lines changed

Cargo.lock

Lines changed: 49 additions & 240 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/typos-cli/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,16 @@ encoding_rs = "0.8.33"
7979

8080
[dev-dependencies]
8181
assert_fs = "1.1"
82-
trycmd = "0.14.20"
83-
criterion = "0.5"
82+
divan = "0.1.11"
8483
snapbox = "0.4.16"
84+
trycmd = "0.14.20"
8585

8686
[[bench]]
87-
name = "checks"
87+
name = "check_file"
8888
harness = false
8989

9090
[[bench]]
91-
name = "corrections"
91+
name = "correct_word"
9292
harness = false
9393

9494
[[bench]]
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
mod data;
2+
3+
use assert_fs::prelude::*;
4+
use typos_cli::file::FileChecker;
5+
6+
#[divan::bench(args = data::DATA)]
7+
fn found_files(bencher: divan::Bencher, sample: &data::Data) {
8+
let dict = typos_cli::dict::BuiltIn::new(Default::default());
9+
let tokenizer = typos::tokens::Tokenizer::new();
10+
let policy = typos_cli::policy::Policy::new()
11+
.dict(&dict)
12+
.tokenizer(&tokenizer);
13+
14+
let temp = assert_fs::TempDir::new().unwrap();
15+
let sample_path = temp.child(sample.name());
16+
sample_path.write_str(sample.content()).unwrap();
17+
18+
bencher
19+
.counter(divan::counter::BytesCount::of_str(sample.content()))
20+
.bench_local(|| {
21+
typos_cli::file::FoundFiles.check_file(sample_path.path(), true, &policy, &PrintSilent)
22+
})
23+
}
24+
25+
#[divan::bench(args = data::DATA)]
26+
fn identifiers(bencher: divan::Bencher, sample: &data::Data) {
27+
let dict = typos_cli::dict::BuiltIn::new(Default::default());
28+
let tokenizer = typos::tokens::Tokenizer::new();
29+
let policy = typos_cli::policy::Policy::new()
30+
.dict(&dict)
31+
.tokenizer(&tokenizer);
32+
33+
let temp = assert_fs::TempDir::new().unwrap();
34+
let sample_path = temp.child(sample.name());
35+
sample_path.write_str(sample.content()).unwrap();
36+
37+
bencher
38+
.counter(divan::counter::BytesCount::of_str(sample.content()))
39+
.bench_local(|| {
40+
typos_cli::file::Identifiers.check_file(sample_path.path(), true, &policy, &PrintSilent)
41+
})
42+
}
43+
44+
#[divan::bench(args = data::DATA)]
45+
fn words(bencher: divan::Bencher, sample: &data::Data) {
46+
let dict = typos_cli::dict::BuiltIn::new(Default::default());
47+
let tokenizer = typos::tokens::Tokenizer::new();
48+
let policy = typos_cli::policy::Policy::new()
49+
.dict(&dict)
50+
.tokenizer(&tokenizer);
51+
52+
let temp = assert_fs::TempDir::new().unwrap();
53+
let sample_path = temp.child(sample.name());
54+
sample_path.write_str(sample.content()).unwrap();
55+
56+
bencher
57+
.counter(divan::counter::BytesCount::of_str(sample.content()))
58+
.bench_local(|| {
59+
typos_cli::file::Words.check_file(sample_path.path(), true, &policy, &PrintSilent)
60+
})
61+
}
62+
63+
#[divan::bench(args = data::DATA)]
64+
fn typos(bencher: divan::Bencher, sample: &data::Data) {
65+
let dict = typos_cli::dict::BuiltIn::new(Default::default());
66+
let tokenizer = typos::tokens::Tokenizer::new();
67+
let policy = typos_cli::policy::Policy::new()
68+
.dict(&dict)
69+
.tokenizer(&tokenizer);
70+
71+
let temp = assert_fs::TempDir::new().unwrap();
72+
let sample_path = temp.child(sample.name());
73+
sample_path.write_str(sample.content()).unwrap();
74+
75+
bencher
76+
.counter(divan::counter::BytesCount::of_str(sample.content()))
77+
.bench_local(|| {
78+
typos_cli::file::Typos.check_file(sample_path.path(), true, &policy, &PrintSilent)
79+
})
80+
}
81+
82+
#[derive(Debug, Default)]
83+
pub struct PrintSilent;
84+
85+
impl typos_cli::report::Report for PrintSilent {
86+
fn report(&self, _msg: typos_cli::report::Message) -> Result<(), std::io::Error> {
87+
Ok(())
88+
}
89+
}
90+
91+
fn main() {
92+
divan::main();
93+
}

crates/typos-cli/benches/checks.rs

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
mod regular {
2+
mod ok {
3+
#[divan::bench]
4+
fn en(bencher: divan::Bencher) {
5+
let input = "finalizes";
6+
let locale = typos_cli::config::Locale::En;
7+
let corrections = typos_cli::dict::BuiltIn::new(locale);
8+
let input = typos::tokens::Word::new(input, 0).unwrap();
9+
#[cfg(feature = "vars")]
10+
assert_eq!(corrections.correct_word(input), None);
11+
bencher
12+
.with_inputs(|| input)
13+
.bench_local_values(|input| corrections.correct_word(input));
14+
}
15+
16+
#[divan::bench]
17+
#[cfg(feature = "vars")]
18+
fn en_us(bencher: divan::Bencher) {
19+
let input = "finalizes";
20+
let locale = typos_cli::config::Locale::EnUs;
21+
let corrections = typos_cli::dict::BuiltIn::new(locale);
22+
let input = typos::tokens::Word::new(input, 0).unwrap();
23+
#[cfg(feature = "vars")]
24+
assert_eq!(corrections.correct_word(input), Some(typos::Status::Valid));
25+
bencher
26+
.with_inputs(|| input)
27+
.bench_local_values(|input| corrections.correct_word(input));
28+
}
29+
}
30+
31+
mod misspell {
32+
#[divan::bench]
33+
fn en(bencher: divan::Bencher) {
34+
let input = "finallizes";
35+
let output = "finalizes";
36+
let locale = typos_cli::config::Locale::En;
37+
let corrections = typos_cli::dict::BuiltIn::new(locale);
38+
let input = typos::tokens::Word::new(input, 0).unwrap();
39+
assert_eq!(
40+
corrections.correct_word(input),
41+
Some(typos::Status::Corrections(vec![
42+
std::borrow::Cow::Borrowed(output)
43+
]))
44+
);
45+
bencher
46+
.with_inputs(|| input)
47+
.bench_local_values(|input| corrections.correct_word(input));
48+
}
49+
50+
#[divan::bench]
51+
#[cfg(feature = "vars")]
52+
fn en_us(bencher: divan::Bencher) {
53+
let input = "finallizes";
54+
let output = "finalizes";
55+
let locale = typos_cli::config::Locale::EnUs;
56+
let corrections = typos_cli::dict::BuiltIn::new(locale);
57+
let input = typos::tokens::Word::new(input, 0).unwrap();
58+
assert_eq!(
59+
corrections.correct_word(input),
60+
Some(typos::Status::Corrections(vec![
61+
std::borrow::Cow::Borrowed(output)
62+
]))
63+
);
64+
bencher
65+
.with_inputs(|| input)
66+
.bench_local_values(|input| corrections.correct_word(input));
67+
}
68+
}
69+
70+
mod misspell_case {
71+
#[divan::bench]
72+
fn en(bencher: divan::Bencher) {
73+
let input = "FINALLIZES";
74+
let output = "FINALIZES";
75+
let locale = typos_cli::config::Locale::En;
76+
let corrections = typos_cli::dict::BuiltIn::new(locale);
77+
let input = typos::tokens::Word::new(input, 0).unwrap();
78+
assert_eq!(
79+
corrections.correct_word(input),
80+
Some(typos::Status::Corrections(vec![
81+
std::borrow::Cow::Borrowed(output)
82+
]))
83+
);
84+
bencher
85+
.with_inputs(|| input)
86+
.bench_local_values(|input| corrections.correct_word(input));
87+
}
88+
89+
#[divan::bench]
90+
#[cfg(feature = "vars")]
91+
fn en_us(bencher: divan::Bencher) {
92+
let input = "FINALLIZES";
93+
let output = "FINALIZES";
94+
let locale = typos_cli::config::Locale::EnUs;
95+
let corrections = typos_cli::dict::BuiltIn::new(locale);
96+
let input = typos::tokens::Word::new(input, 0).unwrap();
97+
assert_eq!(
98+
corrections.correct_word(input),
99+
Some(typos::Status::Corrections(vec![
100+
std::borrow::Cow::Borrowed(output)
101+
]))
102+
);
103+
bencher
104+
.with_inputs(|| input)
105+
.bench_local_values(|input| corrections.correct_word(input));
106+
}
107+
}
108+
}
109+
110+
#[cfg(feature = "vars")]
111+
mod varcon {
112+
mod ok {
113+
#[divan::bench]
114+
fn en_gb(bencher: divan::Bencher) {
115+
let input = "finalizes";
116+
let output = "finalises";
117+
let locale = typos_cli::config::Locale::EnGb;
118+
let corrections = typos_cli::dict::BuiltIn::new(locale);
119+
let input = typos::tokens::Word::new(input, 0).unwrap();
120+
assert_eq!(
121+
corrections.correct_word(input),
122+
Some(typos::Status::Corrections(vec![
123+
std::borrow::Cow::Borrowed(output)
124+
]))
125+
);
126+
bencher
127+
.with_inputs(|| input)
128+
.bench_local_values(|input| corrections.correct_word(input));
129+
}
130+
}
131+
132+
mod misspell {
133+
#[divan::bench]
134+
fn en_gb(bencher: divan::Bencher) {
135+
let input = "finallizes";
136+
let output = "finalises";
137+
let locale = typos_cli::config::Locale::EnGb;
138+
let corrections = typos_cli::dict::BuiltIn::new(locale);
139+
let input = typos::tokens::Word::new(input, 0).unwrap();
140+
assert_eq!(
141+
corrections.correct_word(input),
142+
Some(typos::Status::Corrections(vec![
143+
std::borrow::Cow::Borrowed(output)
144+
]))
145+
);
146+
bencher
147+
.with_inputs(|| input)
148+
.bench_local_values(|input| corrections.correct_word(input));
149+
}
150+
}
151+
}
152+
153+
fn main() {
154+
divan::main();
155+
}

0 commit comments

Comments
 (0)