summaryrefslogtreecommitdiff
path: root/android/os/ParcelPerfTest.java
blob: a92597f131c33f251ecf1b927a686ba3f145d8f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package android.os;

import android.perftests.utils.BenchmarkState;
import android.perftests.utils.PerfStatusReporter;
import android.support.test.filters.LargeTest;
import android.support.test.runner.AndroidJUnit4;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class ParcelPerfTest {
    @Rule
    public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();

    private Parcel mParcel;

    @Before
    public void setUp() {
        mParcel = Parcel.obtain();
        mParcel.setDataPosition(0);
        mParcel.setDataCapacity(8);
    }

    @After
    public void tearDown() {
        mParcel.recycle();
        mParcel = null;
    }

    @Test
    public void timeSetDataPosition() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mParcel.setDataPosition(0);
        }
    }

    @Test
    public void timeGetDataPosition() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mParcel.dataPosition();
        }
    }

    @Test
    public void timeSetDataSize() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mParcel.setDataSize(0);
        }
    }

    @Test
    public void timeGetDataSize() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mParcel.dataSize();
        }
    }

    @Test
    public void timeSetDataCapacity() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mParcel.setDataCapacity(0);
        }
    }

    @Test
    public void timeGetDataCapacity() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mParcel.dataCapacity();
        }
    }

    @Test
    public void timeWriteByte() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        final byte val = 0xF;
        while (state.keepRunning()) {
            mParcel.setDataPosition(0);
            mParcel.writeByte(val);
        }
    }

    @Test
    public void timeReadByte() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mParcel.setDataPosition(0);
            mParcel.readByte();
        }
    }

    @Test
    public void timeWriteInt() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        final int val = 0xF;
        while (state.keepRunning()) {
            mParcel.setDataPosition(0);
            mParcel.writeInt(val);
        }
    }

    @Test
    public void timeReadInt() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mParcel.setDataPosition(0);
            mParcel.readInt();
        }
    }

    @Test
    public void timeWriteLong() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        final long val = 0xF;
        while (state.keepRunning()) {
            mParcel.setDataPosition(0);
            mParcel.writeLong(val);
        }
    }

    @Test
    public void timeReadLong() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mParcel.setDataPosition(0);
            mParcel.readLong();
        }
    }

    @Test
    public void timeObtainRecycle() {
        // Use up the pooled instances.
        // A lot bigger than the actual size but in case someone increased it.
        final int POOL_SIZE = 100;
        for (int i = 0; i < POOL_SIZE; i++) {
            Parcel.obtain();
        }

        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            Parcel.obtain().recycle();
        }
    }
}