Debian Codegraph @

apct-tests/perftests/core/src/android/util/CharsetUtilsPerfTest.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.util;
18
19 import android.perftests.utils.BenchmarkState;
20 import android.perftests.utils.PerfStatusReporter;
21
22 import androidx.test.filters.LargeTest;
23
24 import dalvik.system.VMRuntime;
25
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.Parameterized;
30
31 import java.nio.charset.StandardCharsets;
32 import java.util.Arrays;
33 import java.util.Collection;
34
35 @LargeTest
36 @RunWith(Parameterized.class)
37 public class CharsetUtilsPerfTest {
38 @Rule
39 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
40
41 @Parameterized.Parameter(0)
42 public String mName;
43 @Parameterized.Parameter(1)
44 public String mValue;
45
46 @Parameterized.Parameters(name = "{0}")
47 public static Collection<Object[]> getParameters() {
48 return Arrays.asList(new Object[][] {
49 { "simple", "com.example.typical_package_name" },
50 { "complex", "從不喜歡孤單一個 - 蘇永康/吳雨霏" },
51 });
52 }
53
54 @Test
55 public void timeUpstream() {
56 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
57 while (state.keepRunning()) {
58 mValue.getBytes(StandardCharsets.UTF_8);
59 }
60 }
61
62 /**
63 * Measure performance of writing into a small buffer where bounds checking
64 * requires careful measurement of encoded size.
65 */
66 @Test
67 public void timeLocal_SmallBuffer() {
68 final byte[] dest = (byte[]) VMRuntime.getRuntime().newNonMovableArray(byte.class, 64);
69 final long destPtr = VMRuntime.getRuntime().addressOf(dest);
70
71 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
72 while (state.keepRunning()) {
73 CharsetUtils.toModifiedUtf8Bytes(mValue, destPtr, 0, dest.length);
74 }
75 }
76
77 /**
78 * Measure performance of writing into a large buffer where bounds checking
79 * only needs a simple worst-case 4-bytes-per-char check.
80 */
81 @Test
82 public void timeLocal_LargeBuffer() {
83 final byte[] dest = (byte[]) VMRuntime.getRuntime().newNonMovableArray(byte.class, 1024);
84 final long destPtr = VMRuntime.getRuntime().addressOf(dest);
85
86 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
87 while (state.keepRunning()) {
88 CharsetUtils.toModifiedUtf8Bytes(mValue, destPtr, 0, dest.length);
89 }
90 }
91 }