1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os;
18
19import com.google.caliper.AfterExperiment;
20import com.google.caliper.BeforeExperiment;
21import com.google.caliper.Param;
22
23public class ParcelArrayBenchmark {
24
25    @Param({ "1", "10", "100", "1000" })
26    private int mSize;
27
28    private Parcel mWriteParcel;
29
30    private byte[] mByteArray;
31    private int[] mIntArray;
32    private long[] mLongArray;
33
34    private Parcel mByteParcel;
35    private Parcel mIntParcel;
36    private Parcel mLongParcel;
37
38    @BeforeExperiment
39    protected void setUp() {
40        mWriteParcel = Parcel.obtain();
41
42        mByteArray = new byte[mSize];
43        mIntArray = new int[mSize];
44        mLongArray = new long[mSize];
45
46        mByteParcel = Parcel.obtain();
47        mByteParcel.writeByteArray(mByteArray);
48        mIntParcel = Parcel.obtain();
49        mIntParcel.writeIntArray(mIntArray);
50        mLongParcel = Parcel.obtain();
51        mLongParcel.writeLongArray(mLongArray);
52    }
53
54    @AfterExperiment
55    protected void tearDown() {
56        mWriteParcel.recycle();
57        mWriteParcel = null;
58    }
59
60    public void timeWriteByteArray(int reps) {
61        for (int i = 0; i < reps; i++) {
62            mWriteParcel.setDataPosition(0);
63            mWriteParcel.writeByteArray(mByteArray);
64        }
65    }
66
67    public void timeCreateByteArray(int reps) {
68        for (int i = 0; i < reps; i++) {
69            mByteParcel.setDataPosition(0);
70            mByteParcel.createByteArray();
71        }
72    }
73
74    public void timeReadByteArray(int reps) {
75        for (int i = 0; i < reps; i++) {
76            mByteParcel.setDataPosition(0);
77            mByteParcel.readByteArray(mByteArray);
78        }
79    }
80
81    public void timeWriteIntArray(int reps) {
82        for (int i = 0; i < reps; i++) {
83            mWriteParcel.setDataPosition(0);
84            mWriteParcel.writeIntArray(mIntArray);
85        }
86    }
87
88    public void timeCreateIntArray(int reps) {
89        for (int i = 0; i < reps; i++) {
90            mIntParcel.setDataPosition(0);
91            mIntParcel.createIntArray();
92        }
93    }
94
95    public void timeReadIntArray(int reps) {
96        for (int i = 0; i < reps; i++) {
97            mIntParcel.setDataPosition(0);
98            mIntParcel.readIntArray(mIntArray);
99        }
100    }
101
102    public void timeWriteLongArray(int reps) {
103        for (int i = 0; i < reps; i++) {
104            mWriteParcel.setDataPosition(0);
105            mWriteParcel.writeLongArray(mLongArray);
106        }
107    }
108
109    public void timeCreateLongArray(int reps) {
110        for (int i = 0; i < reps; i++) {
111            mLongParcel.setDataPosition(0);
112            mLongParcel.createLongArray();
113        }
114    }
115
116    public void timeReadLongArray(int reps) {
117        for (int i = 0; i < reps; i++) {
118            mLongParcel.setDataPosition(0);
119            mLongParcel.readLongArray(mLongArray);
120        }
121    }
122}
123