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
24    private Parcel mParcel;
25
26    @BeforeExperiment
27    protected void setUp() {
28        mParcel = Parcel.obtain();
29    }
30
31    @AfterExperiment
32    protected void tearDown() {
33        mParcel.recycle();
34        mParcel = null;
35    }
36
37    public void timeWriteByte(int reps) {
38        final byte val = 0xF;
39        for (int i = 0; i < reps; i++) {
40            mParcel.writeByte(val);
41        }
42    }
43
44    public void timeReadByte(int reps) {
45        mParcel.setDataCapacity(reps);
46        for (int i = 0; i < reps; i++) {
47            mParcel.readByte();
48        }
49    }
50
51    public void timeWriteInt(int reps) {
52        final int val = 0xF;
53        for (int i = 0; i < reps; i++) {
54            mParcel.writeInt(val);
55        }
56    }
57
58    public void timeReadInt(int reps) {
59        mParcel.setDataCapacity(reps << 2);
60        for (int i = 0; i < reps; i++) {
61            mParcel.readInt();
62        }
63    }
64
65    public void timeWriteLong(int reps) {
66        final long val = 0xF;
67        for (int i = 0; i < reps; i++) {
68            mParcel.writeLong(val);
69        }
70    }
71
72    public void timeReadLong(int reps) {
73        mParcel.setDataCapacity(reps << 3);
74        for (int i = 0; i < reps; i++) {
75            mParcel.readLong();
76        }
77    }
78}
79