From 8dcdc1dd433feb862ddbab06c065f720ff7d1d9a Mon Sep 17 00:00:00 2001 From: Neil Fuller Date: Mon, 17 Jun 2019 18:57:49 +0100 Subject: [PATCH] Add a basic benchmark for android.text.format.Time Add a benchmark to demonstrate performance tradeoffs between alternatives for android.text.format.Time. This can be extended later as needed for other Time usecases. Executable with: vogar --mode app_process --benchmark core/tests/benchmarks/src/android/text/format/AndroidTimeVsOthersBenchmark.java Results from an aosp/master taimen: Experiment selection: Benchmark Methods: [toMillis_androidIucUtil, toMillis_androidTime, toMillis_javaTime, toMillis_javaUtil] Instruments: [runtime] User parameters: {} Virtual machines: [app_process] Selection type: Full cartesian product This selection yields 4 experiments. Trial Report (1 of 4): Experiment {instrument=runtime, benchmarkMethod=toMillis_androidIucUtil, vm=app_process, parameters={}} Results: runtime(ns): min=6341.29, 1st qu.=6341.29, median=6341.29, mean=6341.29, 3rd qu.=6341.29, max=6341.29 Trial Report (2 of 4): Experiment {instrument=runtime, benchmarkMethod=toMillis_androidTime, vm=app_process, parameters={}} Results: runtime(ns): min=12310.21, 1st qu.=12310.21, median=12310.21, mean=12310.21, 3rd qu.=12310.21, max=12310.21 Trial Report (3 of 4): Experiment {instrument=runtime, benchmarkMethod=toMillis_javaTime, vm=app_process, parameters={}} Results: runtime(ns): min=1029.36, 1st qu.=1029.36, median=1029.36, mean=1029.36, 3rd qu.=1029.36, max=1029.36 Trial Report (4 of 4): Experiment {instrument=runtime, benchmarkMethod=toMillis_javaUtil, vm=app_process, parameters={}} Results: runtime(ns): min=10142.66, 1st qu.=10142.66, median=10142.66, mean=10142.66, 3rd qu.=10142.66, max=10142.66 Collected 4 measurements from: 1 instrument(s) 1 virtual machine(s) 4 benchmark(s) Note: When not cycling the time zone ID the results are different, but java.time still comes out ahead. Trial Report (1 of 4): Experiment {instrument=runtime, benchmarkMethod=toMillis_androidIucUtil, vm=app_process, parameters={}} Results: runtime(ns): min=6583.57, 1st qu.=6583.57, median=6583.57, mean=6583.57, 3rd qu.=6583.57, max=6583.57 Trial Report (2 of 4): Experiment {instrument=runtime, benchmarkMethod=toMillis_androidTime, vm=app_process, parameters={}} Results: runtime(ns): min=4653.01, 1st qu.=4653.01, median=4653.01, mean=4653.01, 3rd qu.=4653.01, max=4653.01 Trial Report (3 of 4): Experiment {instrument=runtime, benchmarkMethod=toMillis_javaTime, vm=app_process, parameters={}} Results: runtime(ns): min=1095.20, 1st qu.=1095.20, median=1095.20, mean=1095.20, 3rd qu.=1095.20, max=1095.20 Trial Report (4 of 4): Experiment {instrument=runtime, benchmarkMethod=toMillis_javaUtil, vm=app_process, parameters={}} Results: runtime(ns): min=2825.56, 1st qu.=2825.56, median=2825.56, mean=2825.56, 3rd qu.=2825.56, max=2825.56 Bug: 16550209 Test: see above Change-Id: I8f1dffb083db00c2ea4cef40fd5cc2f4acab7a89 --- .../format/AndroidTimeVsOthersBenchmark.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 core/tests/benchmarks/src/android/text/format/AndroidTimeVsOthersBenchmark.java diff --git a/core/tests/benchmarks/src/android/text/format/AndroidTimeVsOthersBenchmark.java b/core/tests/benchmarks/src/android/text/format/AndroidTimeVsOthersBenchmark.java new file mode 100644 index 0000000000000..ea244001aae4c --- /dev/null +++ b/core/tests/benchmarks/src/android/text/format/AndroidTimeVsOthersBenchmark.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.text.format; + +import com.google.caliper.Benchmark; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; + +public class AndroidTimeVsOthersBenchmark { + + private static final String[] TIMEZONE_IDS = { + "Europe/London", + "America/Los_Angeles", + "Asia/Shanghai", + }; + + @Benchmark + public void toMillis_androidTime(int reps) { + long answer = 0; + for (int i = 0; i < reps; i++) { + String timezoneId = TIMEZONE_IDS[i % TIMEZONE_IDS.length]; + Time time = new Time(timezoneId); + time.set(1, 2, 3, 4, 5, 2010); + answer = time.toMillis(false); + } + // System.out.println(answer); + } + + @Benchmark + public void toMillis_javaTime(int reps) { + long answer = 0; + for (int i = 0; i < reps; i++) { + String timezoneId = TIMEZONE_IDS[i % TIMEZONE_IDS.length]; + LocalDateTime time = LocalDateTime.of(2010, 5 + 1, 4, 3, 2, 1); + ZoneOffset offset = ZoneId.of(timezoneId).getRules().getOffset(time); + answer = time.toInstant(offset).toEpochMilli(); + } + // System.out.println(answer); + } + + @Benchmark + public void toMillis_javaUtil(int reps) { + long answer = 0; + for (int i = 0; i < reps; i++) { + String timezoneId = TIMEZONE_IDS[i % TIMEZONE_IDS.length]; + java.util.TimeZone timeZone = java.util.TimeZone.getTimeZone(timezoneId); + java.util.Calendar calendar = new java.util.GregorianCalendar(timeZone); + calendar.set(2010, 5, 4, 3, 2, 1); + calendar.set(java.util.Calendar.MILLISECOND, 0); + answer = calendar.getTimeInMillis(); + } + // System.out.println(answer); + } + + @Benchmark + public void toMillis_androidIucUtil(int reps) { + long answer = 0; + for (int i = 0; i < reps; i++) { + String timezoneId = TIMEZONE_IDS[i % TIMEZONE_IDS.length]; + android.icu.util.TimeZone timeZone = + android.icu.util.TimeZone.getTimeZone(timezoneId); + android.icu.util.Calendar calendar = new android.icu.util.GregorianCalendar(timeZone); + calendar.set(2010, 5, 4, 3, 2, 1); + calendar.set(android.icu.util.Calendar.MILLISECOND, 0); + answer = calendar.getTimeInMillis(); + } + // System.out.println(answer); + } +}