1 use criterion
::criterion_group
;
2 use criterion
::criterion_main
;
3 use criterion
::BenchmarkId
;
4 use criterion
::Criterion
;
5 use std
::collections
::HashMap
;
11 use fluent_bundle
::{FluentArgs, FluentBundle, FluentResource, FluentValue}
;
12 use fluent_syntax
::ast
;
13 use unic_langid
::langid
;
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
)?
;
22 fn get_strings(tests
: &[&'
static str]) -> HashMap
<&'
static str, String
> {
23 let mut ftl_strings
= HashMap
::new();
25 let path
= format
!("./benches/{}.ftl", test
);
26 ftl_strings
.insert(*test
, read_file(&path
).expect("Couldn't load file"));
31 fn get_ids(res
: &FluentResource
) -> Vec
<String
> {
33 .filter_map(|entry
| match entry
{
34 ast
::Entry
::Message(ast
::Message { id, .. }
) => Some(id
.name
.to_owned()),
40 fn get_args(name
: &str) -> Option
<FluentArgs
> {
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("john@doe.com"));
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"));
60 fn add_functions
<R
>(name
: &'
static str, bundle
: &mut FluentBundle
<R
>) {
64 .add_function("PLATFORM", |_args
, _named_args
| {
65 return "linux".into();
67 .expect("Failed to add a function to the bundle.");
73 fn get_bundle(name
: &'
static str, source
: &str) -> (FluentBundle
<FluentResource
>, Vec
<String
>) {
74 let res
= FluentResource
::try_new(source
.to_owned()).expect("Couldn't parse an FTL source");
75 let ids
= get_ids(&res
);
76 let lids
= vec
![langid
!("en")];
77 let mut bundle
= FluentBundle
::new(lids
);
80 .expect("Couldn't add FluentResource to the FluentBundle");
81 add_functions(name
, &mut bundle
);
85 fn resolver_bench(c
: &mut Criterion
) {
87 #[cfg(feature = "all-benchmarks")]
90 #[cfg(feature = "all-benchmarks")]
92 #[cfg(feature = "all-benchmarks")]
95 let ftl_strings
= get_strings(tests
);
97 let mut group
= c
.benchmark_group("construct");
99 let source
= ftl_strings
.get(name
).expect("Failed to find the source.");
100 group
.bench_with_input(BenchmarkId
::from_parameter(name
), &source
, |b
, source
| {
102 FluentResource
::try_new(source
.to_string()).expect("Couldn't parse an FTL source"),
105 let lids
= vec
![langid
!("en")];
106 let mut bundle
= FluentBundle
::new(lids
);
108 .add_resource(res
.clone())
109 .expect("Couldn't add FluentResource to the FluentBundle");
110 add_functions(name
, &mut bundle
);
116 let mut group
= c
.benchmark_group("resolve");
118 let source
= ftl_strings
.get(name
).expect("Failed to find the source.");
119 group
.bench_with_input(BenchmarkId
::from_parameter(name
), &source
, |b
, source
| {
120 let (bundle
, ids
) = get_bundle(name
, source
);
121 let args
= get_args(name
);
123 let mut s
= String
::new();
125 let msg
= bundle
.get_message(id
).expect("Message found");
126 let mut errors
= vec
![];
127 if let Some(value
) = msg
.value() {
128 let _
= bundle
.write_pattern(&mut s
, value
, args
.as_ref(), &mut errors
);
131 for attr
in msg
.attributes() {
133 bundle
.write_pattern(&mut s
, attr
.value(), args
.as_ref(), &mut errors
);
136 assert
!(errors
.len() == 0, "Resolver errors: {:#?}", errors
);
143 let mut group
= c
.benchmark_group("resolve_to_str");
145 let source
= ftl_strings
.get(name
).expect("Failed to find the source.");
146 group
.bench_with_input(BenchmarkId
::from_parameter(name
), &source
, |b
, source
| {
147 let (bundle
, ids
) = get_bundle(name
, source
);
148 let args
= get_args(name
);
151 let msg
= bundle
.get_message(id
).expect("Message found");
152 let mut errors
= vec
![];
153 if let Some(value
) = msg
.value() {
154 let _
= bundle
.format_pattern(value
, args
.as_ref(), &mut errors
);
156 for attr
in msg
.attributes() {
157 let _
= bundle
.format_pattern(attr
.value(), args
.as_ref(), &mut errors
);
159 assert
!(errors
.len() == 0, "Resolver errors: {:#?}", errors
);
167 criterion_group
!(benches
, resolver_bench
);
168 criterion_main
!(benches
);