Skip to content

Commit

Permalink
fzf: add benches for non-fuzzy matching
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Dec 1, 2023
1 parent b73564f commit 93a5b8f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ required-features = ["tests"]
name = "fzf_v2"
required-features = ["tests"]

[[bench]]
name = "fzf_common"
harness = false
required-features = ["bench"]

[[bench]]
name = "fzf_v1"
harness = false
Expand Down
6 changes: 4 additions & 2 deletions benches/common.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dead_code)]

use core::ops::Range;

use criterion::{
Expand Down Expand Up @@ -128,12 +130,12 @@ fn bench<'a, M, C>(
}
}

const MEDIUM_TEXT: &str =
pub const MEDIUM_TEXT: &str =
"Far far away, behind the word mountains, far from the countries Vokalia \
and Consonantia, there live the blind texts. Separated they live in \
Bookmarksgrove right at the coast of the Semantics, a large.";

const LONG_TEXT: &str =
pub const LONG_TEXT: &str =
"Far far away, behind the word mountains, far from the countries Vokalia \
and Consonantia, there live the blind texts. Separated they live in \
Bookmarksgrove right at the coast of the Semantics, a large language \
Expand Down
95 changes: 95 additions & 0 deletions benches/fzf_common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
mod common;

use criterion::{
criterion_group,
criterion_main,
measurement::WallTime,
BenchmarkGroup,
Criterion,
Throughput,
};
use norm::{
fzf::{FzfParser, FzfV1},
Metric,
};

/// The char length of the queries.
const QUERY_LEN: usize = 16;

fn bench(
group: &mut BenchmarkGroup<WallTime>,
query: &str,
candidate: &str,
bench_name: &str,
) {
// Using V1 or V2 doesn't matter because we're not doing fuzzy matching
// here.
let mut fzf = FzfV1::new();

let mut parser = FzfParser::new();

let query = parser.parse(query);

group.throughput(Throughput::Elements(1));

group.bench_function(bench_name, |b| {
b.iter(|| {
let _ = fzf.distance(query, candidate);
})
});
}

fn group(c: &mut Criterion) -> BenchmarkGroup<WallTime> {
c.benchmark_group("fzf")
}

fn exact(c: &mut Criterion) {
let mut group = group(c);

let candidate = common::MEDIUM_TEXT;

let query = {
let midpoint = candidate.len() / 2;
let start = midpoint - QUERY_LEN / 2;
let end = start + QUERY_LEN;
&candidate[start..end]
};

let query = format!("'{query}");

bench(&mut group, &query, candidate, "exact");
}

fn prefix(c: &mut Criterion) {
let mut group = group(c);

let candidate = common::MEDIUM_TEXT;

let query = format!("^{query}", query = &candidate[..QUERY_LEN]);

bench(&mut group, &query, candidate, "prefix");
}

fn suffix(c: &mut Criterion) {
let mut group = group(c);

let candidate = common::MEDIUM_TEXT;

let query =
format!("{query}$", query = &candidate[candidate.len() - QUERY_LEN..]);

bench(&mut group, &query, candidate, "suffix");
}

fn equal(c: &mut Criterion) {
let mut group = group(c);

let candidate = &common::MEDIUM_TEXT[..QUERY_LEN];

let query = format!("^{candidate}$");

bench(&mut group, &query, candidate, "equal");
}

criterion_group!(benches, exact, prefix, suffix, equal);
criterion_main!(benches);

0 comments on commit 93a5b8f

Please sign in to comment.