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.Param;
20import com.google.caliper.SimpleBenchmark;
21
22public class ParcelArrayBenchmark extends SimpleBenchmark {
23
24    @Param({ "1", "10", "100", "1000" })
25    private int mSize;
26
27    private Parcel mWriteParcel;
28
29    private byte[] mByteArray;
30    private int[] mIntArray;
31    private long[] mLongArray;
32
33    private Parcel mByteParcel;
34    private Parcel mIntParcel;
35    private Parcel mLongParcel;
36
37    @Override
38    protected void setUp() {
39        mWriteParcel = Parcel.obtain();
40
41        mByteArray = new byte[mSize];
42        mIntArray = new int[mSize];
43        mLongArray = new long[mSize];
44
45        mByteParcel = Parcel.obtain();
46        mByteParcel.writeByteArray(mByteArray);
47        mIntParcel = Parcel.obtain();
48        mIntParcel.writeIntArray(mIntArray);
49        mLongParcel = Parcel.obtain();
50        mLongParcel.writeLongArray(mLongArray);
51    }
52
53    @Override
54    protected void tearDown() {
55        mWriteParcel.recycle();
56        mWriteParcel = null;
57    }
58
59    public void timeWriteByteArray(int reps) {
60        for (int i = 0; i < reps; i++) {
61            mWriteParcel.setDataPosition(0);
62            mWriteParcel.writeByteArray(mByteArray);
63        }
64    }
65
66    public void timeCreateByteArray(int reps) {
67        for (int i = 0; i < reps; i++) {
68            mByteParcel.setDataPosition(0);
69            mByteParcel.createByteArray();
70        }
71    }
72
73    public void timeReadByteArray(int reps) {
74        for (int i = 0; i < reps; i++) {
75            mByteParcel.setDataPosition(0);
76            mByteParcel.readByteArray(mByteArray);
77        }
78    }
79
80    public void timeWriteIntArray(int reps) {
81        for (int i = 0; i < reps; i++) {
82            mWriteParcel.setDataPosition(0);
83            mWriteParcel.writeIntArray(mIntArray);
84        }
85    }
86
87    public void timeCreateIntArray(int reps) {
88        for (int i = 0; i < reps; i++) {
89            mIntParcel.setDataPosition(0);
90            mIntParcel.createIntArray();
91        }
92    }
93
94    public void timeReadIntArray(int reps) {
95        for (int i = 0; i < reps; i++) {
96            mIntParcel.setDataPosition(0);
97            mIntParcel.readIntArray(mIntArray);
98        }
99    }
100
101    public void timeWriteLongArray(int reps) {
102        for (int i = 0; i < reps; i++) {
103            mWriteParcel.setDataPosition(0);
104            mWriteParcel.writeLongArray(mLongArray);
105        }
106    }
107
108    public void timeCreateLongArray(int reps) {
109        for (int i = 0; i < reps; i++) {
110            mLongParcel.setDataPosition(0);
111            mLongParcel.createLongArray();
112        }
113    }
114
115    public void timeReadLongArray(int reps) {
116        for (int i = 0; i < reps; i++) {
117            mLongParcel.setDataPosition(0);
118            mLongParcel.readLongArray(mLongArray);
119        }
120    }
121
122}
123