blob: a92597f131c33f251ecf1b927a686ba3f145d8f5 [file] [log] [blame]
John Reck306928c2016-10-17 15:18:26 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package android.os;
18
19import android.perftests.utils.BenchmarkState;
20import android.perftests.utils.PerfStatusReporter;
21import android.support.test.filters.LargeTest;
22import android.support.test.runner.AndroidJUnit4;
23
24import org.junit.After;
25import org.junit.Before;
26import org.junit.Rule;
27import org.junit.Test;
28import org.junit.runner.RunWith;
29
30@RunWith(AndroidJUnit4.class)
31@LargeTest
32public class ParcelPerfTest {
33 @Rule
34 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
35
36 private Parcel mParcel;
37
38 @Before
39 public void setUp() {
40 mParcel = Parcel.obtain();
41 mParcel.setDataPosition(0);
42 mParcel.setDataCapacity(8);
43 }
44
45 @After
46 public void tearDown() {
47 mParcel.recycle();
48 mParcel = null;
49 }
50
51 @Test
52 public void timeSetDataPosition() {
53 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
54 while (state.keepRunning()) {
55 mParcel.setDataPosition(0);
56 }
57 }
58
59 @Test
Makoto Onukib148b6c2017-06-27 13:38:38 -070060 public void timeGetDataPosition() {
61 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
62 while (state.keepRunning()) {
63 mParcel.dataPosition();
64 }
65 }
66
67 @Test
68 public void timeSetDataSize() {
69 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
70 while (state.keepRunning()) {
71 mParcel.setDataSize(0);
72 }
73 }
74
75 @Test
76 public void timeGetDataSize() {
77 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
78 while (state.keepRunning()) {
79 mParcel.dataSize();
80 }
81 }
82
83 @Test
84 public void timeSetDataCapacity() {
85 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
86 while (state.keepRunning()) {
87 mParcel.setDataCapacity(0);
88 }
89 }
90
91 @Test
92 public void timeGetDataCapacity() {
93 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
94 while (state.keepRunning()) {
95 mParcel.dataCapacity();
96 }
97 }
98
99 @Test
John Reck306928c2016-10-17 15:18:26 -0700100 public void timeWriteByte() {
101 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
102 final byte val = 0xF;
103 while (state.keepRunning()) {
104 mParcel.setDataPosition(0);
105 mParcel.writeByte(val);
106 }
107 }
108
109 @Test
110 public void timeReadByte() {
111 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
112 while (state.keepRunning()) {
113 mParcel.setDataPosition(0);
114 mParcel.readByte();
115 }
116 }
117
118 @Test
119 public void timeWriteInt() {
120 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
121 final int val = 0xF;
122 while (state.keepRunning()) {
123 mParcel.setDataPosition(0);
124 mParcel.writeInt(val);
125 }
126 }
127
128 @Test
129 public void timeReadInt() {
130 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
131 while (state.keepRunning()) {
132 mParcel.setDataPosition(0);
133 mParcel.readInt();
134 }
135 }
136
137 @Test
138 public void timeWriteLong() {
139 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
140 final long val = 0xF;
141 while (state.keepRunning()) {
142 mParcel.setDataPosition(0);
143 mParcel.writeLong(val);
144 }
145 }
146
147 @Test
148 public void timeReadLong() {
149 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
150 while (state.keepRunning()) {
151 mParcel.setDataPosition(0);
152 mParcel.readLong();
153 }
154 }
Makoto Onukib148b6c2017-06-27 13:38:38 -0700155
156 @Test
157 public void timeObtainRecycle() {
158 // Use up the pooled instances.
159 // A lot bigger than the actual size but in case someone increased it.
160 final int POOL_SIZE = 100;
161 for (int i = 0; i < POOL_SIZE; i++) {
162 Parcel.obtain();
163 }
164
165 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
166 while (state.keepRunning()) {
167 Parcel.obtain().recycle();
168 }
169 }
John Reck306928c2016-10-17 15:18:26 -0700170}