Skip to content

Commit

Permalink
Unify benchmarks behind overview and bench flag
Browse files Browse the repository at this point in the history
  • Loading branch information
zbraniecki committed Oct 10, 2020
1 parent e3cfe00 commit 31386d2
Show file tree
Hide file tree
Showing 16 changed files with 310 additions and 151 deletions.
6 changes: 6 additions & 0 deletions components/datetime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ icu-testdata = { path = "../../resources/testdata" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

[features]
default = []

bench = []

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

[[bench]]
name = "pattern"
harness = false
required-features = ["bench"]
29 changes: 29 additions & 0 deletions components/datetime/benches/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,35 @@ fn datetime_benches(c: &mut Criterion) {

let provider = icu_testdata::get_provider();

let mut group = c.benchmark_group("datetime");

group.bench_function("overview", |b| {
b.iter(|| {
for fx in &fxs.0 {
let datetimes: Vec<MockDateTime> = fx
.values
.iter()
.map(|value| value.parse().unwrap())
.collect();
for setup in &fx.setups {
let langid = setup.locale.parse().unwrap();
let options = fixtures::get_options(&setup.options);
let dtf = DateTimeFormat::try_new(langid, &provider, &options).unwrap();

let mut result = String::new();

for dt in &datetimes {
let fdt = dtf.format(dt);
write!(result, "{}", fdt).unwrap();
result.clear();
}
}
}
})
});
group.finish();

#[cfg(feature = "bench")]
{
let mut group = c.benchmark_group("datetime");

Expand Down
7 changes: 5 additions & 2 deletions components/locale/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ criterion = "0.3.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

[lib]
bench = false
[features]
default = []

bench = []

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

[[bench]]
name = "langid"
Expand Down
21 changes: 21 additions & 0 deletions components/locale/benches/helpers/macros.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
#[macro_export]
macro_rules! overview {
($c:expr, $struct:ident, $data_str:expr, $compare:expr) => {
$c.bench_function("overview", |b| {
b.iter(|| {
let mut values = vec![];
for s in $data_str {
let value: Result<$struct, _> = black_box(s).parse();
values.push(value.expect("Parsing failed"));
}
let _ = values.iter().filter(|v| *v == $compare).count();

let mut strings = vec![];
for value in &values {
strings.push(value.to_string());
}
})
});
};
}

#[macro_export]
macro_rules! construct {
($c:expr, $struct:ident, $struct_name:expr, $data_str:expr) => {
Expand Down
133 changes: 74 additions & 59 deletions components/locale/benches/langid.rs
Original file line number Diff line number Diff line change
@@ -1,78 +1,93 @@
mod fixtures;
mod helpers;

use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use criterion::{black_box, criterion_group, criterion_main, Criterion};

use icu_locale::{LanguageIdentifier, Locale};
use icu_locale::LanguageIdentifier;

fn langid_benches(c: &mut Criterion) {
let path = "./benches/fixtures/langid.json";
let data: fixtures::LocaleList = helpers::read_fixture(path).expect("Failed to read a fixture");

// Construct
// Overview
{
let mut group = c.benchmark_group("langid/construct");
let mut group = c.benchmark_group("langid");

construct!(group, LanguageIdentifier, "langid", &data.canonicalized);
construct!(group, Locale, "locale", &data.canonicalized);
overview!(group, LanguageIdentifier, &data.canonicalized, "en-US");

group.finish();
}

// Stringify
#[cfg(feature = "bench")]
{
let mut group = c.benchmark_group("langid/to_string");

let langids: Vec<LanguageIdentifier> = data
.canonicalized
.iter()
.map(|s| s.parse().unwrap())
.collect();

to_string!(group, LanguageIdentifier, "langid", &langids);
to_string!(group, Locale, "locale", &langids);

group.finish();
}

// Compare
{
let mut group = c.benchmark_group("langid/compare");

let langids: Vec<LanguageIdentifier> = data
.canonicalized
.iter()
.map(|s| s.parse().unwrap())
.collect();
let langids2: Vec<LanguageIdentifier> = data
.canonicalized
.iter()
.map(|s| s.parse().unwrap())
.collect();

compare_struct!(group, LanguageIdentifier, "langid", &langids, &langids2);
compare_struct!(group, Locale, "locale", &langids, &langids2);

compare_str!(
group,
LanguageIdentifier,
"langid",
&langids,
&data.canonicalized
);
compare_str!(group, Locale, "locale", &langids, &data.canonicalized);

group.finish();
}

// Canonicalize
{
let mut group = c.benchmark_group("langid/canonicalize");

canonicalize!(group, LanguageIdentifier, "langid", &data.casing);
canonicalize!(group, Locale, "locale", &data.casing);

group.finish();
use criterion::BenchmarkId;
use icu_locale::Locale;

// Construct
{
let mut group = c.benchmark_group("langid/construct");

construct!(group, LanguageIdentifier, "langid", &data.canonicalized);
construct!(group, Locale, "locale", &data.canonicalized);

group.finish();
}

// Stringify
{
let mut group = c.benchmark_group("langid/to_string");

let langids: Vec<LanguageIdentifier> = data
.canonicalized
.iter()
.map(|s| s.parse().unwrap())
.collect();

to_string!(group, LanguageIdentifier, "langid", &langids);
to_string!(group, Locale, "locale", &langids);

group.finish();
}

// Compare
{
let mut group = c.benchmark_group("langid/compare");

let langids: Vec<LanguageIdentifier> = data
.canonicalized
.iter()
.map(|s| s.parse().unwrap())
.collect();
let langids2: Vec<LanguageIdentifier> = data
.canonicalized
.iter()
.map(|s| s.parse().unwrap())
.collect();

compare_struct!(group, LanguageIdentifier, "langid", &langids, &langids2);
compare_struct!(group, Locale, "locale", &langids, &langids2);

compare_str!(
group,
LanguageIdentifier,
"langid",
&langids,
&data.canonicalized
);
compare_str!(group, Locale, "locale", &langids, &data.canonicalized);

group.finish();
}

// Canonicalize
{
let mut group = c.benchmark_group("langid/canonicalize");

canonicalize!(group, LanguageIdentifier, "langid", &data.casing);
canonicalize!(group, Locale, "locale", &data.casing);

group.finish();
}
}
}

Expand Down
86 changes: 50 additions & 36 deletions components/locale/benches/locale.rs
Original file line number Diff line number Diff line change
@@ -1,67 +1,81 @@
mod fixtures;
mod helpers;

use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use criterion::{black_box, criterion_group, criterion_main, Criterion};

use icu_locale::Locale;

fn locale_benches(c: &mut Criterion) {
let path = "./benches/fixtures/locale.json";
let data: fixtures::LocaleList = helpers::read_fixture(path).expect("Failed to read a fixture");

// Construct
// Overview
{
let mut group = c.benchmark_group("locale/construct");
let mut group = c.benchmark_group("locale");

construct!(group, Locale, "locale", &data.canonicalized);
overview!(group, Locale, &data.canonicalized, "en-US");

group.finish();
}

// Stringify
#[cfg(feature = "bench")]
{
let mut group = c.benchmark_group("locale/to_string");
use criterion::BenchmarkId;

let locales: Vec<Locale> = data
.canonicalized
.iter()
.map(|s| s.parse().unwrap())
.collect();
// Construct
{
let mut group = c.benchmark_group("locale/construct");

to_string!(group, Locale, "locale", &locales);
construct!(group, Locale, "locale", &data.canonicalized);

group.finish();
}
group.finish();
}

// Compare
{
let mut group = c.benchmark_group("locale/compare");
// Stringify
{
let mut group = c.benchmark_group("locale/to_string");

let locales: Vec<Locale> = data
.canonicalized
.iter()
.map(|s| s.parse().unwrap())
.collect();
let locales2: Vec<Locale> = data
.canonicalized
.iter()
.map(|s| s.parse().unwrap())
.collect();
let locales: Vec<Locale> = data
.canonicalized
.iter()
.map(|s| s.parse().unwrap())
.collect();

compare_struct!(group, Locale, "locale", &locales, &locales2);
to_string!(group, Locale, "locale", &locales);

compare_str!(group, Locale, "locale", &locales, &data.canonicalized);
group.finish();
}

group.finish();
}
// Compare
{
let mut group = c.benchmark_group("locale/compare");

// Canonicalize
{
let mut group = c.benchmark_group("locale/canonicalize");
let locales: Vec<Locale> = data
.canonicalized
.iter()
.map(|s| s.parse().unwrap())
.collect();
let locales2: Vec<Locale> = data
.canonicalized
.iter()
.map(|s| s.parse().unwrap())
.collect();

canonicalize!(group, Locale, "locale", &data.casing);
compare_struct!(group, Locale, "locale", &locales, &locales2);

group.finish();
compare_str!(group, Locale, "locale", &locales, &data.canonicalized);

group.finish();
}

// Canonicalize
{
let mut group = c.benchmark_group("locale/canonicalize");

canonicalize!(group, Locale, "locale", &data.casing);

group.finish();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion components/locale/benches/subtags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use icu_locale::ParserError;

macro_rules! subtag_bench {
($c:expr, $name:expr, $subtag:ident, $data:expr) => {
$c.bench_function(&format!("{}_subtag_parse", $name), |b| {
$c.bench_function(&format!("subtags/{}/parse", $name), |b| {
b.iter(|| {
for s in &$data.valid {
let _: $subtag = black_box(s).parse().unwrap();
Expand Down
6 changes: 6 additions & 0 deletions components/locale/src/langid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,9 @@ impl PartialEq<&str> for LanguageIdentifier {
self.to_string().eq(*other)
}
}

impl PartialEq<str> for LanguageIdentifier {
fn eq(&self, other: &str) -> bool {
self.to_string().eq(other)
}
}
6 changes: 6 additions & 0 deletions components/locale/src/locale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,9 @@ impl PartialEq<&str> for Locale {
self.to_string().eq(*other)
}
}

impl PartialEq<str> for Locale {
fn eq(&self, other: &str) -> bool {
self.to_string().eq(other)
}
}
Loading

0 comments on commit 31386d2

Please sign in to comment.