Debian Codegraph @

apct-tests/perftests/core/src/android/text/TextUtilsPerfTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package android.text;
18
19 import android.perftests.utils.BenchmarkState;
20 import android.perftests.utils.PerfStatusReporter;
21
22 import androidx.test.filters.LargeTest;
23 import androidx.test.runner.AndroidJUnit4;
24
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28
29 import java.util.function.Supplier;
30
31 @RunWith(AndroidJUnit4.class)
32 @LargeTest
33 public class TextUtilsPerfTest {
34 @Rule
35 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
36
37 public static final String TEMPLATE = "Template that combines %s and %d together";
38
39 public String mVar1 = "example";
40 public int mVar2 = 42;
41
42 /**
43 * Measure overhead of formatting a string via {@link String#format}.
44 */
45 @Test
46 public void timeFormatUpstream() {
47 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
48 while (state.keepRunning()) {
49 String res = String.format(TEMPLATE, mVar1, mVar2);
50 }
51 }
52
53 /**
54 * Measure overhead of formatting a string via
55 * {@link TextUtils#formatSimple}.
56 */
57 @Test
58 public void timeFormatLocal() {
59 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
60 while (state.keepRunning()) {
61 String res = TextUtils.formatSimple(TEMPLATE, mVar1, mVar2);
62 }
63 }
64
65 /**
66 * Measure overhead of formatting a string inline.
67 */
68 @Test
69 public void timeFormatInline() {
70 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
71 while (state.keepRunning()) {
72 String res = "Template that combines " + mVar1 + " and " + mVar2 + " together";
73 }
74 }
75
76 /**
77 * Measure overhead of a passing null-check that uses a lambda to
78 * communicate a custom error message.
79 */
80 @Test
81 public void timeFormat_Skip_Lambda() {
82 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
83 while (state.keepRunning()) {
84 requireNonNull(this, () -> {
85 return String.format(TEMPLATE, mVar1, mVar2);
86 });
87 }
88 }
89
90 /**
91 * Measure overhead of a passing null-check that uses varargs to communicate
92 * a custom error message.
93 */
94 @Test
95 public void timeFormat_Skip_Varargs() {
96 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
97 while (state.keepRunning()) {
98 requireNonNull(this, TEMPLATE, mVar1, mVar2);
99 }
100 }
101
102 private static <T> T requireNonNull(T obj, Supplier<String> messageSupplier) {
103 return obj;
104 }
105
106 private static <T> T requireNonNull(T obj, String format, Object... args) {
107 return obj;
108 }
109 }