1 /* 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 17 package android.os; 18 19 import android.perftests.utils.BenchmarkState; 20 import android.perftests.utils.PerfStatusReporter; 21 import android.support.test.filters.LargeTest; 22 import android.support.test.runner.AndroidJUnit4; 23 24 import org.junit.After; 25 import org.junit.Before; 26 import org.junit.Rule; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 30 import static org.junit.Assert.assertEquals; 31 import static org.junit.Assert.assertNull; 32 import static org.junit.Assert.assertTrue; 33 34 @RunWith(AndroidJUnit4.class) 35 @LargeTest 36 public class ParcelPerfTest { 37 @Rule 38 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); 39 40 private Parcel mParcel; 41 42 @Before 43 public void setUp() { 44 mParcel = Parcel.obtain(); 45 mParcel.setDataPosition(0); 46 mParcel.setDataCapacity(8); 47 } 48 49 @After 50 public void tearDown() { 51 mParcel.recycle(); 52 mParcel = null; 53 } 54 55 @Test 56 public void timeSetDataPosition() { 57 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 58 while (state.keepRunning()) { 59 mParcel.setDataPosition(0); 60 } 61 } 62 63 @Test 64 public void timeGetDataPosition() { 65 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 66 while (state.keepRunning()) { 67 mParcel.dataPosition(); 68 } 69 } 70 71 @Test 72 public void timeSetDataSize() { 73 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 74 while (state.keepRunning()) { 75 mParcel.setDataSize(0); 76 } 77 } 78 79 @Test 80 public void timeGetDataSize() { 81 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 82 while (state.keepRunning()) { 83 mParcel.dataSize(); 84 } 85 } 86 87 @Test 88 public void timeSetDataCapacity() { 89 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 90 while (state.keepRunning()) { 91 mParcel.setDataCapacity(0); 92 } 93 } 94 95 @Test 96 public void timeGetDataCapacity() { 97 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 98 while (state.keepRunning()) { 99 mParcel.dataCapacity(); 100 } 101 } 102 103 @Test 104 public void timeWriteByte() { 105 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 106 final byte val = 0xF; 107 while (state.keepRunning()) { 108 mParcel.setDataPosition(0); 109 mParcel.writeByte(val); 110 } 111 } 112 113 @Test 114 public void timeReadByte() { 115 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 116 while (state.keepRunning()) { 117 mParcel.setDataPosition(0); 118 mParcel.readByte(); 119 } 120 } 121 122 @Test 123 public void timeWriteInt() { 124 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 125 final int val = 0xF; 126 while (state.keepRunning()) { 127 mParcel.setDataPosition(0); 128 mParcel.writeInt(val); 129 } 130 } 131 132 @Test 133 public void timeReadInt() { 134 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 135 while (state.keepRunning()) { 136 mParcel.setDataPosition(0); 137 mParcel.readInt(); 138 } 139 } 140 141 @Test 142 public void timeWriteLong() { 143 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 144 final long val = 0xF; 145 while (state.keepRunning()) { 146 mParcel.setDataPosition(0); 147 mParcel.writeLong(val); 148 } 149 } 150 151 @Test 152 public void timeReadLong() { 153 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 154 while (state.keepRunning()) { 155 mParcel.setDataPosition(0); 156 mParcel.readLong(); 157 } 158 } 159 160 @Test 161 public void timeObtainRecycle() { 162 // Use up the pooled instances. 163 // A lot bigger than the actual size but in case someone increased it. 164 final int POOL_SIZE = 100; 165 for (int i = 0; i < POOL_SIZE; i++) { 166 Parcel.obtain(); 167 } 168 169 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 170 while (state.keepRunning()) { 171 Parcel.obtain().recycle(); 172 } 173 } 174 175 @Test 176 public void timeWriteException() { 177 timeWriteException(false); 178 } 179 180 @Test 181 public void timeWriteExceptionWithStackTraceParceling() { 182 timeWriteException(true); 183 } 184 185 @Test 186 public void timeReadException() { 187 timeReadException(false); 188 } 189 190 @Test 191 public void timeReadExceptionWithStackTraceParceling() { 192 timeReadException(true); 193 } 194 195 private void timeWriteException(boolean enableParceling) { 196 if (enableParceling) { 197 Parcel.setStackTraceParceling(true); 198 } 199 try { 200 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 201 Parcel p = Parcel.obtain(); 202 SecurityException e = new SecurityException("TestMessage"); 203 while (state.keepRunning()) { 204 p.setDataPosition(0); 205 p.writeException(e); 206 } 207 } finally { 208 if (enableParceling) { 209 Parcel.setStackTraceParceling(false); 210 } 211 } 212 } 213 214 private void timeReadException(boolean enableParceling) { 215 if (enableParceling) { 216 Parcel.setStackTraceParceling(true); 217 } 218 try { 219 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 220 Parcel p = Parcel.obtain(); 221 String msg = "TestMessage"; 222 p.writeException(new SecurityException(msg)); 223 p.setDataPosition(0); 224 // First verify that remote cause is set (if parceling is enabled) 225 try { 226 p.readException(); 227 } catch (SecurityException e) { 228 assertEquals(e.getMessage(), msg); 229 if (enableParceling) { 230 assertTrue(e.getCause() instanceof RemoteException); 231 } else { 232 assertNull(e.getCause()); 233 } 234 } 235 236 while (state.keepRunning()) { 237 p.setDataPosition(0); 238 try { 239 p.readException(); 240 } catch (SecurityException expected) { 241 } 242 } 243 } finally { 244 if (enableParceling) { 245 Parcel.setStackTraceParceling(false); 246 } 247 } 248 } 249 250 } 251