Skip to content

Commit

Permalink
Merge pull request #397 from YuukiToriyama/feature/refactor-orthograp…
Browse files Browse the repository at this point in the history
…hical-variant-adapter/add-benchmark

表記ゆれアダプタのリファクタ: `OrthographicVariantAdapter`に対するベンチマークテストを作成
  • Loading branch information
YuukiToriyama authored Sep 1, 2024
2 parents 4c7ae80 + 9ef65fb commit c19678d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
5 changes: 5 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ blocking = ["reqwest/blocking"]
city-name-correction = []
format-house-number = []

[[bench]]
name = "core_benchmark"
harness = false

[dependencies]
itertools = "0.13.0"
rapidfuzz = "0.5.0"
Expand All @@ -29,6 +33,7 @@ reqwest = { version = "0.12.5", default-features = false, features = ["json", "r
js-sys = "0.3.67"

[dev-dependencies]
criterion = { version = "0.5.1", default-features = false, features = ["html_reports"] }
tokio.workspace = true
wasm-bindgen-test = { workspace = true }

Expand Down
7 changes: 7 additions & 0 deletions core/benches/core_benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod orthographical_variant_adapter;

use crate::orthographical_variant_adapter::bench_orthographical_variant_adapter;
use criterion::{criterion_group, criterion_main};

criterion_group!(benches, bench_orthographical_variant_adapter);
criterion_main!(benches);
48 changes: 48 additions & 0 deletions core/benches/orthographical_variant_adapter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use criterion::measurement::WallTime;
use criterion::{BatchSize, BenchmarkGroup, BenchmarkId, Criterion};
use japanese_address_parser::parser::adapter::orthographical_variant_adapter::{
OrthographicalVariantAdapter, OrthographicalVariants, Variant,
};

pub fn bench_orthographical_variant_adapter(c: &mut Criterion) {
let mut group = c.benchmark_group("orthographical_variant_adapter");
add_tests(
&mut group,
TestSuite {
expected: "松ケ崎東池ノ内町",
inputs: vec![
"松が崎東池ノ内町",
"松ヶ崎東池ノ内町",
"松ケ﨑東池ノ内町",
"松ケ﨑東池の内町",
"松ガ﨑東池の内町",
],
variants_to_be_used: vec![Variant::ケ, Variant::崎, Variant::の],
},
);
group.finish();
}

fn add_tests(group: &mut BenchmarkGroup<WallTime>, test_suite: TestSuite) {
for input in test_suite.inputs {
let benchmark_id = BenchmarkId::new(test_suite.expected, input);
group.bench_with_input(benchmark_id, input, |b, input| {
b.iter_batched(
|| OrthographicalVariantAdapter {
variant_list: test_suite.variants_to_be_used.clone(),
},
|adapter| {
let (region_name, _) = adapter.apply(input, test_suite.expected).unwrap();
assert_eq!(region_name, test_suite.expected);
},
BatchSize::SmallInput,
)
});
}
}

struct TestSuite {
expected: &'static str,
inputs: Vec<&'static str>,
variants_to_be_used: Vec<Variant>,
}
2 changes: 1 addition & 1 deletion core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::domain::geolonia::error::{Error, ParseErrorKind};
use crate::tokenizer::Tokenizer;
use serde::Serialize;

pub(crate) mod adapter;
pub mod adapter;

impl<T> From<Tokenizer<T>> for Address {
fn from(value: Tokenizer<T>) -> Self {
Expand Down

0 comments on commit c19678d

Please sign in to comment.