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;
21
22public class ParcelBenchmark {
23    private static final int INNER_REPS = 1000;
24
25    private Parcel mParcel;
26
27    @BeforeExperiment
28    protected void setUp() {
29        mParcel = Parcel.obtain();
30        mParcel.setDataPosition(0);
31        mParcel.setDataCapacity(INNER_REPS * 8);
32    }
33
34    @AfterExperiment
35    protected void tearDown() {
36        mParcel.recycle();
37        mParcel = null;
38    }
39
40    public void timeWriteByte(int reps) {
41        final byte val = 0xF;
42        for (int i = 0; i < (reps / INNER_REPS); i++) {
43            mParcel.setDataPosition(0);
44            for (int j = 0; j < INNER_REPS; j++) {
45                mParcel.writeByte(val);
46            }
47        }
48    }
49
50    public void timeReadByte(int reps) {
51        for (int i = 0; i < (reps / INNER_REPS); i++) {
52            mParcel.setDataPosition(0);
53            for (int j = 0; j < INNER_REPS; j++) {
54                mParcel.readByte();
55            }
56        }
57    }
58
59    public void timeWriteInt(int reps) {
60        final int val = 0xF;
61        for (int i = 0; i < (reps / INNER_REPS); i++) {
62            mParcel.setDataPosition(0);
63            for (int j = 0; j < INNER_REPS; j++) {
64                mParcel.writeInt(val);
65            }
66        }
67    }
68
69    public void timeReadInt(int reps) {
70        for (int i = 0; i < (reps / INNER_REPS); i++) {
71            mParcel.setDataPosition(0);
72            for (int j = 0; j < INNER_REPS; j++) {
73                mParcel.readInt();
74            }
75        }
76    }
77
78    public void timeWriteLong(int reps) {
79        final long val = 0xF;
80        for (int i = 0; i < (reps / INNER_REPS); i++) {
81            mParcel.setDataPosition(0);
82            for (int j = 0; j < INNER_REPS; j++) {
83                mParcel.writeLong(val);
84            }
85        }
86    }
87
88    public void timeReadLong(int reps) {
89        for (int i = 0; i < (reps / INNER_REPS); i++) {
90            mParcel.setDataPosition(0);
91            for (int j = 0; j < INNER_REPS; j++) {
92                mParcel.readLong();
93            }
94        }
95    }
96}
97