| 1 |
use criterion::criterion_group; |
| 2 |
use criterion::criterion_main; |
| 3 |
use criterion::BenchmarkId; |
| 4 |
use criterion::Criterion; |
| 5 |
use std::collections::HashMap; |
| 6 |
use std::fs::File; |
| 7 |
use std::io; |
| 8 |
use std::io::Read; |
| 9 |
use std::rc::Rc; |
| 10 |
|
| 11 |
use fluent_bundle::{FluentArgs, FluentBundle, FluentResource, FluentValue}; |
| 12 |
use fluent_syntax::ast; |
| 13 |
use unic_langid::langid; |
| 14 |
|
| 15 |
fn read_file(path: &str) -> Result<String, io::Error> { |
| 16 |
let mut f = File::open(path)?; |
| 17 |
let mut s = String::new(); |
| 18 |
f.read_to_string(&mut s)?; |
| 19 |
Ok(s) |
| 20 |
} |
| 21 |
|
| 22 |
fn get_strings(tests: &[&'static str]) -> HashMap<&'static str, String> { |
| 23 |
let mut ftl_strings = HashMap::new(); |
| 24 |
for test in tests { |
| 25 |
let path = format!("./benches/{}.ftl", test); |
| 26 |
ftl_strings.insert(*test, read_file(&path).expect("Couldn't load file")); |
| 27 |
} |
| 28 |
ftl_strings |
| 29 |
} |
| 30 |
|
| 31 |
fn get_ids(res: &FluentResource) -> Vec<String> { |
| 32 |
res.entries() |
| 33 |
.filter_map(|entry| match entry { |
| 34 |
ast::Entry::Message(ast::Message { id, .. }) => Some(id.name.to_owned()), |
| 35 |
_ => None, |
| 36 |
}) |
| 37 |
.collect() |
| 38 |
} |
| 39 |
|
| 40 |
fn get_args(name: &str) -> Option<FluentArgs> { |
| 41 |
match name { |
| 42 |
"preferences" => { |
| 43 |
let mut prefs_args = FluentArgs::new(); |
| 44 |
prefs_args.set("name", FluentValue::from("John")); |
| 45 |
prefs_args.set("tabCount", FluentValue::from(5)); |
| 46 |
prefs_args.set("count", FluentValue::from(3)); |
| 47 |
prefs_args.set("version", FluentValue::from("65.0")); |
| 48 |
prefs_args.set("path", FluentValue::from("/tmp")); |
| 49 |
prefs_args.set("num", FluentValue::from(4)); |
| 50 |
prefs_args.set("email", FluentValue::from("[email protected]")); |
| 51 |
prefs_args.set("value", FluentValue::from(4.5)); |
| 52 |
prefs_args.set("unit", FluentValue::from("mb")); |
| 53 |
prefs_args.set("service-name", FluentValue::from("Mozilla Disk")); |
| 54 |
Some(prefs_args) |
| 55 |
} |
| 56 |
_ => None, |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
fn add_functions<R>(name: &'static str, bundle: &mut FluentBundle<R>) { |
| 61 |
if name == "preferences" { |
| 62 |
bundle |
| 63 |
.add_function("PLATFORM", |_args, _named_args| "linux".into()) |
| 64 |
.expect("Failed to add a function to the bundle."); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
fn get_bundle(name: &'static str, source: &str) -> (FluentBundle<FluentResource>, Vec<String>) { |
| 69 |
let res = FluentResource::try_new(source.to_owned()).expect("Couldn't parse an FTL source"); |
| 70 |
let ids = get_ids(&res); |
| 71 |
let lids = vec![langid!("en")]; |
| 72 |
let mut bundle = FluentBundle::new(lids); |
| 73 |
bundle |
| 74 |
.add_resource(res) |
| 75 |
.expect("Couldn't add FluentResource to the FluentBundle"); |
| 76 |
add_functions(name, &mut bundle); |
| 77 |
(bundle, ids) |
| 78 |
} |
| 79 |
|
| 80 |
fn resolver_bench(c: &mut Criterion) { |
| 81 |
let tests = &[ |
| 82 |
#[cfg(feature = "all-benchmarks")] |
| 83 |
"simple", |
| 84 |
"preferences", |
| 85 |
#[cfg(feature = "all-benchmarks")] |
| 86 |
"menubar", |
| 87 |
#[cfg(feature = "all-benchmarks")] |
| 88 |
"unescape", |
| 89 |
]; |
| 90 |
let ftl_strings = get_strings(tests); |
| 91 |
|
| 92 |
let mut group = c.benchmark_group("construct"); |
| 93 |
for name in tests { |
| 94 |
let source = ftl_strings.get(name).expect("Failed to find the source."); |
| 95 |
group.bench_with_input(BenchmarkId::from_parameter(name), &source, |b, source| { |
| 96 |
let res = Rc::new( |
| 97 |
FluentResource::try_new(source.to_string()).expect("Couldn't parse an FTL source"), |
| 98 |
); |
| 99 |
b.iter(|| { |
| 100 |
let lids = vec![langid!("en")]; |
| 101 |
let mut bundle = FluentBundle::new(lids); |
| 102 |
bundle |
| 103 |
.add_resource(res.clone()) |
| 104 |
.expect("Couldn't add FluentResource to the FluentBundle"); |
| 105 |
add_functions(name, &mut bundle); |
| 106 |
}); |
| 107 |
}); |
| 108 |
} |
| 109 |
group.finish(); |
| 110 |
|
| 111 |
let mut group = c.benchmark_group("resolve"); |
| 112 |
for name in tests { |
| 113 |
let source = ftl_strings.get(name).expect("Failed to find the source."); |
| 114 |
group.bench_with_input(BenchmarkId::from_parameter(name), &source, |b, source| { |
| 115 |
let (bundle, ids) = get_bundle(name, source); |
| 116 |
let args = get_args(name); |
| 117 |
b.iter(|| { |
| 118 |
let mut s = String::new(); |
| 119 |
for id in &ids { |
| 120 |
let msg = bundle.get_message(id).expect("Message found"); |
| 121 |
let mut errors = vec![]; |
| 122 |
if let Some(value) = msg.value() { |
| 123 |
let _ = bundle.write_pattern(&mut s, value, args.as_ref(), &mut errors); |
| 124 |
s.clear(); |
| 125 |
} |
| 126 |
for attr in msg.attributes() { |
| 127 |
let _ = |
| 128 |
bundle.write_pattern(&mut s, attr.value(), args.as_ref(), &mut errors); |
| 129 |
s.clear(); |
| 130 |
} |
| 131 |
assert!(errors.is_empty(), "Resolver errors: {:#?}", errors); |
| 132 |
} |
| 133 |
}); |
| 134 |
}); |
| 135 |
} |
| 136 |
group.finish(); |
| 137 |
|
| 138 |
let mut group = c.benchmark_group("resolve_to_str"); |
| 139 |
for name in tests { |
| 140 |
let source = ftl_strings.get(name).expect("Failed to find the source."); |
| 141 |
group.bench_with_input(BenchmarkId::from_parameter(name), &source, |b, source| { |
| 142 |
let (bundle, ids) = get_bundle(name, source); |
| 143 |
let args = get_args(name); |
| 144 |
b.iter(|| { |
| 145 |
for id in &ids { |
| 146 |
let msg = bundle.get_message(id).expect("Message found"); |
| 147 |
let mut errors = vec![]; |
| 148 |
if let Some(value) = msg.value() { |
| 149 |
let _ = bundle.format_pattern(value, args.as_ref(), &mut errors); |
| 150 |
} |
| 151 |
for attr in msg.attributes() { |
| 152 |
let _ = bundle.format_pattern(attr.value(), args.as_ref(), &mut errors); |
| 153 |
} |
| 154 |
assert!(errors.is_empty(), "Resolver errors: {:#?}", errors); |
| 155 |
} |
| 156 |
}); |
| 157 |
}); |
| 158 |
} |
| 159 |
group.finish(); |
| 160 |
} |
| 161 |
|
| 162 |
criterion_group!(benches, resolver_bench); |
| 163 |
criterion_main!(benches); |