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 23 import org.junit.After; 24 import org.junit.Before; 25 import org.junit.Rule; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.Parameterized; 29 import org.junit.runners.Parameterized.Parameters; 30 31 import java.util.Arrays; 32 import java.util.Collection; 33 34 @RunWith(Parameterized.class) 35 @LargeTest 36 public class ParcelArrayPerfTest { 37 @Rule 38 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); 39 40 @Parameters(name = "size={0}") 41 public static Collection<Object[]> data() { 42 return Arrays.asList(new Object[][] { {1}, {10}, {100}, {1000} }); 43 } 44 45 private final int mSize; 46 47 private Parcel mWriteParcel; 48 49 private byte[] mByteArray; 50 private int[] mIntArray; 51 private long[] mLongArray; 52 53 private Parcel mByteParcel; 54 private Parcel mIntParcel; 55 private Parcel mLongParcel; 56 57 public ParcelArrayPerfTest(int size) { 58 mSize = size; 59 } 60 61 @Before 62 public void setUp() { 63 mWriteParcel = Parcel.obtain(); 64 65 mByteArray = new byte[mSize]; 66 mIntArray = new int[mSize]; 67 mLongArray = new long[mSize]; 68 69 mByteParcel = Parcel.obtain(); 70 mByteParcel.writeByteArray(mByteArray); 71 mIntParcel = Parcel.obtain(); 72 mIntParcel.writeIntArray(mIntArray); 73 mLongParcel = Parcel.obtain(); 74 mLongParcel.writeLongArray(mLongArray); 75 } 76 77 @After 78 public void tearDown() { 79 mWriteParcel.recycle(); 80 mWriteParcel = null; 81 } 82 83 @Test 84 public void timeWriteByteArray() { 85 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 86 while (state.keepRunning()) { 87 mWriteParcel.setDataPosition(0); 88 mWriteParcel.writeByteArray(mByteArray); 89 } 90 } 91 92 @Test 93 public void timeCreateByteArray() { 94 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 95 while (state.keepRunning()) { 96 mByteParcel.setDataPosition(0); 97 mByteParcel.createByteArray(); 98 } 99 } 100 101 @Test 102 public void timeReadByteArray() { 103 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 104 while (state.keepRunning()) { 105 mByteParcel.setDataPosition(0); 106 mByteParcel.readByteArray(mByteArray); 107 } 108 } 109 110 @Test 111 public void timeWriteIntArray() { 112 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 113 while (state.keepRunning()) { 114 mWriteParcel.setDataPosition(0); 115 mWriteParcel.writeIntArray(mIntArray); 116 } 117 } 118 119 @Test 120 public void timeCreateIntArray() { 121 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 122 while (state.keepRunning()) { 123 mIntParcel.setDataPosition(0); 124 mIntParcel.createIntArray(); 125 } 126 } 127 128 @Test 129 public void timeReadIntArray() { 130 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 131 while (state.keepRunning()) { 132 mIntParcel.setDataPosition(0); 133 mIntParcel.readIntArray(mIntArray); 134 } 135 } 136 137 @Test 138 public void timeWriteLongArray() { 139 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 140 while (state.keepRunning()) { 141 mWriteParcel.setDataPosition(0); 142 mWriteParcel.writeLongArray(mLongArray); 143 } 144 } 145 146 @Test 147 public void timeCreateLongArray() { 148 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 149 while (state.keepRunning()) { 150 mLongParcel.setDataPosition(0); 151 mLongParcel.createLongArray(); 152 } 153 } 154 155 @Test 156 public void timeReadLongArray() { 157 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 158 while (state.keepRunning()) { 159 mLongParcel.setDataPosition(0); 160 mLongParcel.readLongArray(mLongArray); 161 } 162 } 163 } 164