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