]> git.proxmox.com Git - rustc.git/blob - vendor/fluent-bundle/benches/resolver.rs
New upstream version 1.62.1+dfsg1
[rustc.git] / vendor / fluent-bundle / benches / resolver.rs
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 return 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("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"));
54 Some(prefs_args)
55 }
56 _ => None,
57 }
58 }
59
60 fn add_functions<R>(name: &'static str, bundle: &mut FluentBundle<R>) {
61 match name {
62 "preferences" => {
63 bundle
64 .add_function("PLATFORM", |_args, _named_args| {
65 return "linux".into();
66 })
67 .expect("Failed to add a function to the bundle.");
68 }
69 _ => {}
70 }
71 }
72
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);
78 bundle
79 .add_resource(res)
80 .expect("Couldn't add FluentResource to the FluentBundle");
81 add_functions(name, &mut bundle);
82 (bundle, ids)
83 }
84
85 fn resolver_bench(c: &mut Criterion) {
86 let tests = &[
87 #[cfg(feature = "all-benchmarks")]
88 "simple",
89 "preferences",
90 #[cfg(feature = "all-benchmarks")]
91 "menubar",
92 #[cfg(feature = "all-benchmarks")]
93 "unescape",
94 ];
95 let ftl_strings = get_strings(tests);
96
97 let mut group = c.benchmark_group("construct");
98 for name in tests {
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| {
101 let res = Rc::new(
102 FluentResource::try_new(source.to_string()).expect("Couldn't parse an FTL source"),
103 );
104 b.iter(|| {
105 let lids = vec![langid!("en")];
106 let mut bundle = FluentBundle::new(lids);
107 bundle
108 .add_resource(res.clone())
109 .expect("Couldn't add FluentResource to the FluentBundle");
110 add_functions(name, &mut bundle);
111 })
112 });
113 }
114 group.finish();
115
116 let mut group = c.benchmark_group("resolve");
117 for name in tests {
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);
122 b.iter(|| {
123 let mut s = String::new();
124 for id in &ids {
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);
129 s.clear();
130 }
131 for attr in msg.attributes() {
132 let _ =
133 bundle.write_pattern(&mut s, attr.value(), args.as_ref(), &mut errors);
134 s.clear();
135 }
136 assert!(errors.len() == 0, "Resolver errors: {:#?}", errors);
137 }
138 })
139 });
140 }
141 group.finish();
142
143 let mut group = c.benchmark_group("resolve_to_str");
144 for name in tests {
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);
149 b.iter(|| {
150 for id in &ids {
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);
155 }
156 for attr in msg.attributes() {
157 let _ = bundle.format_pattern(attr.value(), args.as_ref(), &mut errors);
158 }
159 assert!(errors.len() == 0, "Resolver errors: {:#?}", errors);
160 }
161 })
162 });
163 }
164 group.finish();
165 }
166
167 criterion_group!(benches, resolver_bench);
168 criterion_main!(benches);