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 Reckimport android.support.test.runner.AndroidJUnit4;
23306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
24306928c49d271f55eecac7d7686295d2a12f905cJohn Reckimport org.junit.After;
25306928c49d271f55eecac7d7686295d2a12f905cJohn Reckimport org.junit.Before;
26306928c49d271f55eecac7d7686295d2a12f905cJohn Reckimport org.junit.Rule;
27306928c49d271f55eecac7d7686295d2a12f905cJohn Reckimport org.junit.Test;
28306928c49d271f55eecac7d7686295d2a12f905cJohn Reckimport org.junit.runner.RunWith;
29306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
30306928c49d271f55eecac7d7686295d2a12f905cJohn Reck@RunWith(AndroidJUnit4.class)
31306928c49d271f55eecac7d7686295d2a12f905cJohn Reck@LargeTest
32306928c49d271f55eecac7d7686295d2a12f905cJohn Reckpublic class ParcelPerfTest {
33306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Rule
34306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
35306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
36306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    private Parcel mParcel;
37306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
38306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Before
39306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void setUp() {
40306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        mParcel = Parcel.obtain();
41306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        mParcel.setDataPosition(0);
42306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        mParcel.setDataCapacity(8);
43306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
44306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
45306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @After
46306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void tearDown() {
47306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        mParcel.recycle();
48306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        mParcel = null;
49306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
50306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
51306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Test
52306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void timeSetDataPosition() {
53306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
54306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        while (state.keepRunning()) {
55306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mParcel.setDataPosition(0);
56306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        }
57306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
58306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
59306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Test
60b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki    public void timeGetDataPosition() {
61b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
62b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        while (state.keepRunning()) {
63b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki            mParcel.dataPosition();
64b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        }
65b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki    }
66b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki
67b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki    @Test
68b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki    public void timeSetDataSize() {
69b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
70b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        while (state.keepRunning()) {
71b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki            mParcel.setDataSize(0);
72b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        }
73b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki    }
74b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki
75b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki    @Test
76b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki    public void timeGetDataSize() {
77b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
78b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        while (state.keepRunning()) {
79b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki            mParcel.dataSize();
80b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        }
81b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki    }
82b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki
83b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki    @Test
84b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki    public void timeSetDataCapacity() {
85b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
86b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        while (state.keepRunning()) {
87b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki            mParcel.setDataCapacity(0);
88b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        }
89b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki    }
90b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki
91b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki    @Test
92b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki    public void timeGetDataCapacity() {
93b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
94b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        while (state.keepRunning()) {
95b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki            mParcel.dataCapacity();
96b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        }
97b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki    }
98b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki
99b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki    @Test
100306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void timeWriteByte() {
101306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
102306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        final byte val = 0xF;
103306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        while (state.keepRunning()) {
104306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mParcel.setDataPosition(0);
105306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mParcel.writeByte(val);
106306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        }
107306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
108306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
109306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Test
110306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void timeReadByte() {
111306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
112306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        while (state.keepRunning()) {
113306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mParcel.setDataPosition(0);
114306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mParcel.readByte();
115306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        }
116306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
117306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
118306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Test
119306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void timeWriteInt() {
120306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
121306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        final int val = 0xF;
122306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        while (state.keepRunning()) {
123306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mParcel.setDataPosition(0);
124306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mParcel.writeInt(val);
125306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        }
126306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
127306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
128306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Test
129306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void timeReadInt() {
130306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
131306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        while (state.keepRunning()) {
132306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mParcel.setDataPosition(0);
133306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mParcel.readInt();
134306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        }
135306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
136306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
137306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Test
138306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void timeWriteLong() {
139306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
140306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        final long val = 0xF;
141306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        while (state.keepRunning()) {
142306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mParcel.setDataPosition(0);
143306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mParcel.writeLong(val);
144306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        }
145306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
146306928c49d271f55eecac7d7686295d2a12f905cJohn Reck
147306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    @Test
148306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    public void timeReadLong() {
149306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
150306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        while (state.keepRunning()) {
151306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mParcel.setDataPosition(0);
152306928c49d271f55eecac7d7686295d2a12f905cJohn Reck            mParcel.readLong();
153306928c49d271f55eecac7d7686295d2a12f905cJohn Reck        }
154306928c49d271f55eecac7d7686295d2a12f905cJohn Reck    }
155b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki
156b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki    @Test
157b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki    public void timeObtainRecycle() {
158b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        // Use up the pooled instances.
159b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        // A lot bigger than the actual size but in case someone increased it.
160b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        final int POOL_SIZE = 100;
161b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        for (int i = 0; i < POOL_SIZE; i++) {
162b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki            Parcel.obtain();
163b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        }
164b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki
165b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
166b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        while (state.keepRunning()) {
167b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki            Parcel.obtain().recycle();
168b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki        }
169b148b6c3c8547143946726a4f6143106406f2fd2Makoto Onuki    }
170306928c49d271f55eecac7d7686295d2a12f905cJohn Reck}
171