Debian Codegraph @

core/tests/benchmarks/src/android/os/ParcelableBenchmark.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2021 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.os;
18
19 import android.annotation.SuppressLint;
20 import android.graphics.Point;
21 import android.graphics.Rect;
22 import android.util.MergedConfiguration;
23 import android.view.InsetsSource;
24 import android.view.InsetsState;
25
26 import com.google.caliper.AfterExperiment;
27 import com.google.caliper.BeforeExperiment;
28
29 /**
30 * Benchmark of read/write large Parcelable class. This also shows the performance of different
31 * implementations for nested Parcelable class:
32 * <ul>
33 * <li>Well-written read/writeFromParcel (direct access)</li>
34 * <li>read/writeTypedObject (object creation + addition int to indicate nullity)</li>
35 * <li>read/writeParcelable (object creation + addition type String)</li>
36 * </ul>
37 */
38 public class ParcelableBenchmark {
39 private Parcel mParcel;
40
41 @BeforeExperiment
42 protected void setUp() {
43 mParcel = Parcel.obtain();
44 }
45
46 @AfterExperiment
47 protected void tearDown() {
48 mParcel.recycle();
49 mParcel = null;
50 }
51
52 public void timeReadWriteMergedConfiguration(int reps) {
53 final MergedConfiguration mergedConfiguration = new MergedConfiguration();
54 for (int i = 0; i < reps; i++) {
55 mergedConfiguration.writeToParcel(mParcel, 0);
56 mParcel.setDataPosition(0);
57 mergedConfiguration.readFromParcel(mParcel);
58 }
59 }
60
61 public void timeReadWriteInsetsState(int reps) {
62 final InsetsState insetsState = new InsetsState();
63 for (int i = 0; i < InsetsState.SIZE; i++) {
64 insetsState.addSource(new InsetsSource(i));
65 }
66 for (int i = 0; i < reps; i++) {
67 insetsState.writeToParcel(mParcel, 0);
68 mParcel.setDataPosition(0);
69 insetsState.readFromParcel(mParcel);
70 }
71 }
72
73 public void timeReadWritePointArray(int reps) {
74 final PointArray pointArray = new PointArray();
75 for (int i = 0; i < reps; i++) {
76 pointArray.writeToParcel(mParcel, 0);
77 mParcel.setDataPosition(0);
78 pointArray.readFromParcel(mParcel);
79 }
80 }
81
82 public void timeReadWritePointArrayFast(int reps) {
83 final PointArrayFast pointArray = new PointArrayFast();
84 for (int i = 0; i < reps; i++) {
85 pointArray.writeToParcel(mParcel, 0);
86 mParcel.setDataPosition(0);
87 pointArray.readFromParcel(mParcel);
88 }
89 }
90
91 @SuppressWarnings("ParcelableCreator")
92 @SuppressLint("ParcelCreator")
93 private static class PointArray implements Parcelable {
94 Rect mBounds = new Rect();
95 Point[] mPoints = new Point[10];
96 {
97 for (int i = 0; i < mPoints.length; i++) {
98 mPoints[i] = new Point();
99 }
100 }
101
102 @Override
103 public void writeToParcel(Parcel dest, int flags) {
104 dest.writeParcelable(mBounds, flags);
105 dest.writeParcelableArray(mPoints, flags);
106 }
107
108 void readFromParcel(Parcel in) {
109 mBounds = in.readParcelable(Rect.class.getClassLoader());
110 mPoints = in.readParcelableArray(Point.class.getClassLoader(), Point.class);
111 }
112
113 @Override
114 public int describeContents() {
115 return 0;
116 }
117 }
118
119 @SuppressLint("ParcelCreator")
120 private static class PointArrayFast extends PointArray {
121
122 @Override
123 public void writeToParcel(Parcel dest, int flags) {
124 mBounds.writeToParcel(dest, flags);
125 dest.writeTypedArray(mPoints, flags);
126 }
127
128 @Override
129 void readFromParcel(Parcel in) {
130 mBounds.readFromParcel(in);
131 in.readTypedArray(mPoints, Point.CREATOR);
132 }
133 }
134 }