Debian Codegraph @

debian/rust-vendor/icu_datetime/benches/datetime.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 use criterion::{Criterion, black_box, criterion_group, criterion_main};
6 use icu_datetime::{DateTimeFormatter, FixedCalendarDateTimeFormatter, fieldsets};
7
8 use icu_calendar::Gregorian;
9 use icu_locale_core::{Locale, locale};
10 use icu_time::zone::{IanaParser, ZoneNameTimestamp};
11 use icu_time::{DateTime, TimeZoneInfo, ZonedDateTime};
12 use writeable::Writeable;
13
14 use icu_datetime::{fieldsets::builder::FieldSetBuilder, provider::fields::components};
15 use serde::{Deserialize, Serialize};
16
17 #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
18 pub(crate) struct Fixture {
19 pub(crate) setups: Vec<TestInput>,
20 pub(crate) values: Vec<String>,
21 }
22
23 #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
24 pub(crate) struct TestInput {
25 pub(crate) locale: String,
26 pub(crate) options: TestOptions,
27 }
28
29 #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
30 pub(crate) struct TestOptions {
31 pub(crate) length: Option<TestOptionsLength>,
32 pub(crate) components: Option<TestComponentsBag>,
33 pub(crate) semantic: Option<FieldSetBuilder>,
34 }
35
36 #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
37 pub(crate) struct TestOptionsLength {
38 pub(crate) date: Option<TestLength>,
39 pub(crate) time: Option<TestLength>,
40 }
41
42 #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
43 pub(crate) enum TestLength {
44 #[serde(rename = "short")]
45 Short,
46 #[serde(rename = "medium")]
47 Medium,
48 #[serde(rename = "long")]
49 Long,
50 #[serde(rename = "full")]
51 Full,
52 }
53
54 #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
55 pub(crate) struct TestComponentsBag {
56 pub(crate) era: Option<components::Text>,
57 pub(crate) year: Option<components::Year>,
58 pub(crate) month: Option<components::Month>,
59 pub(crate) week: Option<components::Week>,
60 pub(crate) day: Option<components::Day>,
61 pub(crate) weekday: Option<components::Text>,
62
63 pub(crate) hour: Option<components::Numeric>,
64 pub(crate) minute: Option<components::Numeric>,
65 pub(crate) second: Option<components::Numeric>,
66 pub(crate) subsecond: Option<u8>,
67
68 pub(crate) time_zone_name: Option<components::TimeZoneName>,
69 }
70
71 fn datetime_benches(c: &mut Criterion) {
72 let mut group = c.benchmark_group("datetime");
73
74 let mut bench_neoneo_datetime_with_fixture = |name, file, has_zones| {
75 let fx = serde_json::from_str::<Fixture>(file).unwrap();
76
77 let datetimes = fx
78 .values
79 .iter()
80 .map(|s| {
81 if has_zones {
82 ZonedDateTime::try_lenient_from_str(s, Gregorian, IanaParser::new()).expect(s)
83 } else {
84 let DateTime { date, time } = DateTime::try_from_str(s, Gregorian).unwrap();
85 ZonedDateTime {
86 date,
87 time,
88 // zone is unused but we need to make the types match
89 zone: TimeZoneInfo::unknown()
90 .with_zone_name_timestamp(ZoneNameTimestamp::far_in_future()),
91 }
92 }
93 })
94 .collect::<Vec<_>>();
95
96 let setups = fx
97 .setups
98 .iter()
99 .map(|s| {
100 (
101 s.locale.parse::<Locale>().unwrap(),
102 s.options.semantic.as_ref().unwrap(),
103 )
104 })
105 .collect::<Vec<_>>();
106
107 let mut result = String::with_capacity(1000);
108
109 group.bench_function(format!("semantic/{name}"), |b| {
110 b.iter(|| {
111 for &(ref locale, field_set_builder) in &setups {
112 let dtf = {
113 FixedCalendarDateTimeFormatter::<Gregorian, _>::try_new(
114 locale.into(),
115 field_set_builder.clone().build_composite().unwrap(),
116 )
117 .expect("Failed to create FixedCalendarDateTimeFormatter.")
118 };
119
120 for dt in &datetimes {
121 let fdt = dtf.format(dt);
122 fdt.write_to(&mut result).unwrap();
123 result.clear();
124 }
125 }
126 })
127 });
128 };
129
130 bench_neoneo_datetime_with_fixture("lengths", include_str!("fixtures/lengths.json"), false);
131
132 bench_neoneo_datetime_with_fixture(
133 "components",
134 include_str!("fixtures/components.json"),
135 false,
136 );
137
138 bench_neoneo_datetime_with_fixture(
139 "lengths_with_zones",
140 include_str!("fixtures/lengths_with_zones.json"),
141 true,
142 );
143
144 let ten_cases = [
145 "2001-09-08T18:46:40.000[u-ca=gregory]",
146 "2017-07-13T19:40:00.000[u-ca=gregory]",
147 "2020-09-13T05:26:40.000[u-ca=gregory]",
148 "2021-01-06T22:13:20.000[u-ca=gregory]",
149 "2021-05-02T17:00:00.000[u-ca=gregory]",
150 "2021-08-26T10:46:40.000[u-ca=gregory]",
151 "2021-11-20T03:33:20.000[u-ca=gregory]",
152 "2022-04-14T22:20:00.000[u-ca=gregory]",
153 "2022-08-08T16:06:40.000[u-ca=gregory]",
154 "2033-05-17T20:33:20.000[u-ca=gregory]",
155 ]
156 .map(|s| DateTime::try_from_str(s, Gregorian).unwrap());
157
158 #[inline]
159 fn construct_any_ymd_short() -> DateTimeFormatter<fieldsets::YMD> {
160 DateTimeFormatter::try_new(locale!("fr").into(), fieldsets::YMD::short()).unwrap()
161 }
162
163 #[inline]
164 fn construct_fixed_ymd_short() -> FixedCalendarDateTimeFormatter<Gregorian, fieldsets::YMD> {
165 FixedCalendarDateTimeFormatter::try_new(locale!("fr").into(), fieldsets::YMD::short())
166 .unwrap()
167 }
168
169 #[inline]
170 fn construct_fixed_ymd_long() -> FixedCalendarDateTimeFormatter<Gregorian, fieldsets::YMD> {
171 FixedCalendarDateTimeFormatter::try_new(locale!("fr").into(), fieldsets::YMD::long())
172 .unwrap()
173 }
174
175 group.bench_function("ymd_short/any/construct_and_format/10_cases", |b| {
176 let mut buffer = String::with_capacity(1000);
177 b.iter(|| {
178 for datetime in black_box(&ten_cases).iter() {
179 buffer.clear();
180 let formatter = construct_any_ymd_short();
181 formatter.format(datetime).write_to(&mut buffer).unwrap();
182 }
183 });
184 });
185
186 group.bench_function("ymd_short/any/format_only/10_cases", |b| {
187 let formatter = construct_any_ymd_short();
188 let mut buffer = String::with_capacity(1000);
189 b.iter(|| {
190 for datetime in black_box(&ten_cases).iter() {
191 buffer.clear();
192 black_box(&formatter)
193 .format(datetime)
194 .write_to(&mut buffer)
195 .unwrap();
196 }
197 });
198 });
199
200 group.bench_function("ymd_short/any/format_to_string/10_cases", |b| {
201 let formatter = construct_any_ymd_short();
202 b.iter(|| {
203 let mut counter = 0usize; // make sure the loop is not DCE'd
204 for datetime in black_box(&ten_cases).iter() {
205 let n = black_box(&formatter)
206 .format(datetime)
207 .write_to_string()
208 .len();
209 counter = counter.wrapping_add(n);
210 }
211 counter
212 });
213 });
214
215 group.bench_function("ymd_short/fixed/construct_and_format/10_cases", |b| {
216 let mut buffer = String::with_capacity(1000);
217 b.iter(|| {
218 for datetime in black_box(&ten_cases).iter() {
219 buffer.clear();
220 let formatter = construct_fixed_ymd_short();
221 formatter.format(datetime).write_to(&mut buffer).unwrap();
222 }
223 });
224 });
225
226 group.bench_function("ymd_short/fixed/format_only/10_cases", |b| {
227 let formatter = construct_fixed_ymd_short();
228 let mut buffer = String::with_capacity(1000);
229 b.iter(|| {
230 for datetime in black_box(&ten_cases).iter() {
231 buffer.clear();
232 black_box(&formatter)
233 .format(datetime)
234 .write_to(&mut buffer)
235 .unwrap();
236 }
237 });
238 });
239
240 group.bench_function("ymd_short/fixed/format_to_string/10_cases", |b| {
241 let formatter = construct_fixed_ymd_short();
242 b.iter(|| {
243 let mut counter = 0usize; // make sure the loop is not DCE'd
244 for datetime in black_box(&ten_cases).iter() {
245 let n = black_box(&formatter)
246 .format(datetime)
247 .write_to_string()
248 .len();
249 counter = counter.wrapping_add(n);
250 }
251 counter
252 });
253 });
254
255 group.bench_function("ymd_long/fixed/construct_and_format/10_cases", |b| {
256 let mut buffer = String::with_capacity(1000);
257 b.iter(|| {
258 for datetime in black_box(&ten_cases).iter() {
259 buffer.clear();
260 let formatter = construct_fixed_ymd_long();
261 formatter.format(datetime).write_to(&mut buffer).unwrap();
262 }
263 });
264 });
265
266 group.bench_function("ymd_long/fixed/format_only/10_cases", |b| {
267 let formatter = construct_fixed_ymd_long();
268 let mut buffer = String::with_capacity(1000);
269 b.iter(|| {
270 for datetime in black_box(&ten_cases).iter() {
271 buffer.clear();
272 black_box(&formatter)
273 .format(datetime)
274 .write_to(&mut buffer)
275 .unwrap();
276 }
277 });
278 });
279
280 group.bench_function("ymd_long/fixed/format_to_string/10_cases", |b| {
281 let formatter = construct_fixed_ymd_long();
282 b.iter(|| {
283 let mut counter = 0usize; // make sure the loop is not DCE'd
284 for datetime in black_box(&ten_cases).iter() {
285 let n = black_box(&formatter)
286 .format(datetime)
287 .write_to_string()
288 .len();
289 counter = counter.wrapping_add(n);
290 }
291 counter
292 });
293 });
294
295 group.finish();
296 }
297
298 criterion_group!(benches, datetime_benches);
299 criterion_main!(benches);