Debian Codegraph @

debian/rust-vendor/fluent-syntax/benches/parser.rs

raw
1 use criterion::criterion_group;
2 use criterion::criterion_main;
3 use criterion::Criterion;
4 use std::collections::HashMap;
5 use std::fs;
6 use std::io;
7
8 use fluent_syntax::parser::parse_runtime;
9
10 fn read_file(path: &str) -> Result<String, io::Error> {
11 fs::read_to_string(path)
12 }
13
14 #[cfg(feature = "all-benchmarks")]
15 fn get_resources(tests: &[&'static str]) -> HashMap<&'static str, String> {
16 let mut ftl_strings = HashMap::new();
17 for test in tests {
18 let path = format!("./benches/{}", test);
19 ftl_strings.insert(*test, read_file(&path).expect("Couldn't load file"));
20 }
21 ftl_strings
22 }
23
24 fn get_ctxs(tests: &[&'static str]) -> HashMap<&'static str, Vec<String>> {
25 let mut ftl_strings = HashMap::new();
26 for test in tests {
27 let paths = fs::read_dir(format!("./benches/contexts/{}", test)).unwrap();
28 let strings = paths
29 .into_iter()
30 .map(|p| {
31 let p = p.unwrap().path();
32 let path = p.to_str().unwrap();
33 read_file(path).unwrap()
34 })
35 .collect::<Vec<_>>();
36 ftl_strings.insert(*test, strings);
37 }
38 ftl_strings
39 }
40
41 fn parse_bench(c: &mut Criterion) {
42 #[cfg(feature = "all-benchmarks")]
43 {
44 let tests = &["simple.ftl", "preferences.ftl", "menubar.ftl"];
45
46 let mut group = c.benchmark_group("parse_resource");
47
48 for (name, resource) in get_resources(tests) {
49 group.bench_with_input(name, &resource, |b, source| {
50 b.iter(|| parse_runtime(source.as_str()).expect("Parsing of the FTL failed."))
51 });
52 }
53
54 group.finish();
55 }
56
57 let ctx_names = &["browser", "preferences"];
58
59 #[cfg(feature = "all-benchmarks")]
60 {
61 use fluent_syntax::parser::parse;
62
63 let mut group = c.benchmark_group("parse_ctx");
64
65 for (name, ctx) in get_ctxs(ctx_names) {
66 group.bench_with_input(name, &ctx, |b, ctx| {
67 b.iter(|| {
68 for source in ctx {
69 parse(source.as_str()).expect("Parsing of the FTL failed.");
70 }
71 })
72 });
73 }
74
75 group.finish();
76 }
77
78 {
79 let mut group = c.benchmark_group("parse_ctx_runtime");
80
81 for (name, ctx) in get_ctxs(ctx_names) {
82 group.bench_with_input(name, &ctx, |b, ctx| {
83 b.iter(|| {
84 for source in ctx {
85 parse_runtime(source.as_str()).expect("Parsing of the FTL failed.");
86 }
87 });
88 });
89 }
90
91 group.finish();
92 }
93
94 #[cfg(feature = "all-benchmarks")]
95 {
96 use fluent_syntax::unicode::{unescape_unicode, unescape_unicode_to_string};
97
98 let strings = &[
99 "foo",
100 "This is an example value",
101 "Hello \\u00e3\\u00e9 World",
102 "\\u004c\\u006f\\u0072\\u0065\\u006d \\u0069\\u0070\\u0073\\u0075\\u006d \\u0064\\u006f\\u006c\\u006f\\u0072 \\u0073\\u0069\\u0074 \\u0061\\u006d\\u0065\\u0074",
103 "Let me introduce \\\"The\\\" Fluent",
104 "And here's an example of \\\\ a character to be escaped",
105 "But this message is completely unescape free",
106 "And so is this one",
107 "Maybe this one is as well completely escape free",
108 "Welcome to Mozilla Firefox",
109 "\\u0054\\u0068\\u0065\\u0073\\u0065 \\u0073\\u0065\\u0074\\u0074\\u0069\\u006e\\u0067\\u0073 \\u0061\\u0072\\u0065 \\u0074\\u0061\\u0069\\u006c\\u006f\\u0072\\u0065\\u0064 \\u0074\\u006f \\u0079\\u006f\\u0075\\u0072 \\u0063\\u006f\\u006d\\u0070\\u0075\\u0074\\u0065\\u0072\\u2019\\u0073 \\u0068\\u0061\\u0072\\u0064\\u0077\\u0061\\u0072\\u0065 \\u0061\\u006e\\u0064 \\u006f\\u0070\\u0065\\u0072\\u0061\\u0074\\u0069\\u006e\\u0067 \\u0073\\u0079\\u0073\\u0074\\u0065\\u006d\\u002e",
110 "These settings are tailored to your computer’s hardware and operating system",
111 "Use recommended performance settings",
112 "\\u0041\\u0064\\u0064\\u0069\\u0074\\u0069\\u006f\\u006e\\u0061\\u006c \\u0063\\u006f\\u006e\\u0074\\u0065\\u006e\\u0074 \\u0070\\u0072\\u006f\\u0063\\u0065\\u0073\\u0073\\u0065\\u0073 \\u0063\\u0061\\u006e \\u0069\\u006d\\u0070\\u0072\\u006f\\u0076\\u0065 \\u0070\\u0065\\u0072\\u0066\\u006f\\u0072\\u006d\\u0061\\u006e\\u0063\\u0065 \\u0077\\u0068\\u0065\\u006e \\u0075\\u0073\\u0069\\u006e\\u0067 \\u006d\\u0075\\u006c\\u0074\\u0069\\u0070\\u006c\\u0065 \\u0074\\u0061\\u0062\\u0073\\u002c \\u0062\\u0075\\u0074 \\u0077\\u0069\\u006c\\u006c \\u0061\\u006c\\u0073\\u006f \\u0075\\u0073\\u0065 \\u006d\\u006f\\u0072\\u0065 \\u006d\\u0065\\u006d\\u006f\\u0072\\u0079\\u002e",
113 "Additional content processes can improve performance when using multiple tabs, but will also use more memory.",
114 ];
115
116 let mut group = c.benchmark_group("unicode");
117
118 group.bench_function("writer", |b| {
119 b.iter(|| {
120 let mut result = String::new();
121 for s in strings {
122 unescape_unicode(&mut result, s).unwrap();
123 result.clear();
124 }
125 })
126 });
127
128 group.bench_function("to_string", |b| {
129 b.iter(|| {
130 for s in strings {
131 let _ = unescape_unicode_to_string(s);
132 }
133 })
134 });
135
136 group.finish();
137 }
138 }
139
140 criterion_group!(benches, parse_bench,);
141 criterion_main!(benches);