1306928c49d271f55eecac7d7686295d2a12f905cJohn Reck/*
2306928c49d271f55eecac7d7686295d2a12f905cJohn Reck * Copyright (C) 2016 The Android Open Source Project
3306928c49d271f55eecac7d7686295d2a12f905cJohn Reck *
4306928c49d271f55eecac7d7686295d2a12f905cJohn Reck * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5306928c49d271f55eecac7d7686295d2a12f905cJohn Reck * use this file except in compliance with the License. You may obtain a copy of
6306928c49d271f55eecac7d7686295d2a12f905cJohn Reck * the License at
7306928c49d271f55eecac7d7686295d2a12f905cJohn Reck *
8306928c49d271f55eecac7d7686295d2a12f905cJohn Reck * http://www.apache.org/licenses/LICENSE-2.0
9306928c49d271f55eecac7d7686295d2a12f905cJohn Reck *
10306928c49d271f55eecac7d7686295d2a12f905cJohn Reck * Unless required by applicable law or agreed to in writing, software
11306928c49d271f55eecac7d7686295d2a12f905cJohn Reck * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12306928c49d271f55eecac7d7686295d2a12f905cJohn Reck * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13306928c49d271f55eecac7d7686295d2a12f905cJohn Reck * License for the specific language governing permissions and limitations under
14306928c49d271f55eecac7d7686295d2a12f905cJohn Reck * the License.
15306928c49d271f55eecac7d7686295d2a12f905cJohn Reck */
16306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
17306928c49d271f55eecac7d7686295d2a12f905cJohn Reckpackage android.os;
18306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
19306928c49d271f55eecac7d7686295d2a12f905cJohn Reckimport android.perftests.utils.BenchmarkState;
20306928c49d271f55eecac7d7686295d2a12f905cJohn Reckimport android.perftests.utils.PerfStatusReporter;
21306928c49d271f55eecac7d7686295d2a12f905cJohn Reckimport android.support.test.filters.LargeTest;
22306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
23306928c49d271f55eecac7d7686295d2a12f905cJohn Reckimport org.junit.After;
24306928c49d271f55eecac7d7686295d2a12f905cJohn Reckimport org.junit.Before;
25306928c49d271f55eecac7d7686295d2a12f905cJohn Reckimport org.junit.Rule;
26306928c49d271f55eecac7d7686295d2a12f905cJohn Reckimport org.junit.Test;
27306928c49d271f55eecac7d7686295d2a12f905cJohn Reckimport org.junit.runner.RunWith;
28306928c49d271f55eecac7d7686295d2a12f905cJohn Reckimport org.junit.runners.Parameterized;
29306928c49d271f55eecac7d7686295d2a12f905cJohn Reckimport org.junit.runners.Parameterized.Parameters;
30306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
31306928c49d271f55eecac7d7686295d2a12f905cJohn Reckimport java.util.Arrays;
32306928c49d271f55eecac7d7686295d2a12f905cJohn Reckimport java.util.Collection;
33306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
34306928c49d271f55eecac7d7686295d2a12f905cJohn Reck@RunWith(Parameterized.class)
35306928c49d271f55eecac7d7686295d2a12f905cJohn Reck@LargeTest
36306928c49d271f55eecac7d7686295d2a12f905cJohn Reckpublic class ParcelArrayPerfTest {
37306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Rule
38306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
39306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
40306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Parameters(name = "size={0}")
41306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public static Collection<Object[]> data() {
42306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        return Arrays.asList(new Object[][] { {1}, {10}, {100}, {1000} });
43306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
44306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
45306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    private final int mSize;
46306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
47306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    private Parcel mWriteParcel;
48306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
49306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    private byte[] mByteArray;
50306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    private int[] mIntArray;
51306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    private long[] mLongArray;
52306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
53306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    private Parcel mByteParcel;
54306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    private Parcel mIntParcel;
55306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    private Parcel mLongParcel;
56306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
57306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public ParcelArrayPerfTest(int size) {
58306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        mSize = size;
59306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
60306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
61306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Before
62306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void setUp() {
63306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        mWriteParcel = Parcel.obtain();
64306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
65306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        mByteArray = new byte[mSize];
66306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        mIntArray = new int[mSize];
67306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        mLongArray = new long[mSize];
68306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
69306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        mByteParcel = Parcel.obtain();
70306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        mByteParcel.writeByteArray(mByteArray);
71306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        mIntParcel = Parcel.obtain();
72306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        mIntParcel.writeIntArray(mIntArray);
73306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        mLongParcel = Parcel.obtain();
74306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        mLongParcel.writeLongArray(mLongArray);
75306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
76306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
77306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @After
78306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void tearDown() {
79306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        mWriteParcel.recycle();
80306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        mWriteParcel = null;
81306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
82306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
83306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Test
84306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void timeWriteByteArray() {
85306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
86306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        while (state.keepRunning()) {
87306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mWriteParcel.setDataPosition(0);
88306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mWriteParcel.writeByteArray(mByteArray);
89306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        }
90306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
91306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
92306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Test
93306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void timeCreateByteArray() {
94306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
95306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        while (state.keepRunning()) {
96306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mByteParcel.setDataPosition(0);
97306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mByteParcel.createByteArray();
98306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        }
99306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
100306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
101306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Test
102306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void timeReadByteArray() {
103306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
104306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        while (state.keepRunning()) {
105306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mByteParcel.setDataPosition(0);
106306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mByteParcel.readByteArray(mByteArray);
107306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        }
108306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
109306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
110306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Test
111306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void timeWriteIntArray() {
112306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
113306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        while (state.keepRunning()) {
114306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mWriteParcel.setDataPosition(0);
115306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mWriteParcel.writeIntArray(mIntArray);
116306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        }
117306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
118306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
119306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Test
120306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void timeCreateIntArray() {
121306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
122306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        while (state.keepRunning()) {
123306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mIntParcel.setDataPosition(0);
124306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mIntParcel.createIntArray();
125306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        }
126306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
127306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
128306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Test
129306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void timeReadIntArray() {
130306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
131306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        while (state.keepRunning()) {
132306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mIntParcel.setDataPosition(0);
133306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mIntParcel.readIntArray(mIntArray);
134306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        }
135306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
136306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
137306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Test
138306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void timeWriteLongArray() {
139306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
140306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        while (state.keepRunning()) {
141306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mWriteParcel.setDataPosition(0);
142306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mWriteParcel.writeLongArray(mLongArray);
143306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        }
144306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
145306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
146306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Test
147306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void timeCreateLongArray() {
148306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
149306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        while (state.keepRunning()) {
150306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mLongParcel.setDataPosition(0);
151306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mLongParcel.createLongArray();
152306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        }
153306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
154306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
155306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Test
156306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void timeReadLongArray() {
157306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
158306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        while (state.keepRunning()) {
159306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mLongParcel.setDataPosition(0);
160306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mLongParcel.readLongArray(mLongArray);
161306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        }
162306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
163306928c49d271f55eecac7d7686295d2a12f905cJohn Reck}
164