Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a197e37f authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Caliper benchmarks for Parcel.

Bug: 6111276
Change-Id: I4b89e4c14d2dc20e71bbaed78f285e6d539a1f0f
parent 09c8f48c
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line

These benchmarks use the Caliper benchmark framework, and can be
run on a remote device using Vogar:

http://code.google.com/p/caliper/
http://code.google.com/p/vogar/

$ vogar --benchmark path/to/Benchmark.java
+122 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 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 com.google.caliper.Param;
import com.google.caliper.SimpleBenchmark;

public class ParcelArrayBenchmark extends SimpleBenchmark {

    @Param({ "1", "10", "100", "1000" })
    private int mSize;

    private Parcel mWriteParcel;

    private byte[] mByteArray;
    private int[] mIntArray;
    private long[] mLongArray;

    private Parcel mByteParcel;
    private Parcel mIntParcel;
    private Parcel mLongParcel;

    @Override
    protected void setUp() {
        mWriteParcel = Parcel.obtain();

        mByteArray = new byte[mSize];
        mIntArray = new int[mSize];
        mLongArray = new long[mSize];

        mByteParcel = Parcel.obtain();
        mByteParcel.writeByteArray(mByteArray);
        mIntParcel = Parcel.obtain();
        mIntParcel.writeIntArray(mIntArray);
        mLongParcel = Parcel.obtain();
        mLongParcel.writeLongArray(mLongArray);
    }

    @Override
    protected void tearDown() {
        mWriteParcel.recycle();
        mWriteParcel = null;
    }

    public void timeWriteByteArray(int reps) {
        for (int i = 0; i < reps; i++) {
            mWriteParcel.setDataPosition(0);
            mWriteParcel.writeByteArray(mByteArray);
        }
    }

    public void timeCreateByteArray(int reps) {
        for (int i = 0; i < reps; i++) {
            mByteParcel.setDataPosition(0);
            mByteParcel.createByteArray();
        }
    }

    public void timeReadByteArray(int reps) {
        for (int i = 0; i < reps; i++) {
            mByteParcel.setDataPosition(0);
            mByteParcel.readByteArray(mByteArray);
        }
    }

    public void timeWriteIntArray(int reps) {
        for (int i = 0; i < reps; i++) {
            mWriteParcel.setDataPosition(0);
            mWriteParcel.writeIntArray(mIntArray);
        }
    }

    public void timeCreateIntArray(int reps) {
        for (int i = 0; i < reps; i++) {
            mIntParcel.setDataPosition(0);
            mIntParcel.createIntArray();
        }
    }

    public void timeReadIntArray(int reps) {
        for (int i = 0; i < reps; i++) {
            mIntParcel.setDataPosition(0);
            mIntParcel.readIntArray(mIntArray);
        }
    }

    public void timeWriteLongArray(int reps) {
        for (int i = 0; i < reps; i++) {
            mWriteParcel.setDataPosition(0);
            mWriteParcel.writeLongArray(mLongArray);
        }
    }

    public void timeCreateLongArray(int reps) {
        for (int i = 0; i < reps; i++) {
            mLongParcel.setDataPosition(0);
            mLongParcel.createLongArray();
        }
    }

    public void timeReadLongArray(int reps) {
        for (int i = 0; i < reps; i++) {
            mLongParcel.setDataPosition(0);
            mLongParcel.readLongArray(mLongArray);
        }
    }

}
+77 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 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 com.google.caliper.SimpleBenchmark;

public class ParcelBenchmark extends SimpleBenchmark {

    private Parcel mParcel;

    @Override
    protected void setUp() {
        mParcel = Parcel.obtain();
    }

    @Override
    protected void tearDown() {
        mParcel.recycle();
        mParcel = null;
    }

    public void timeWriteByte(int reps) {
        final byte val = 0xF;
        for (int i = 0; i < reps; i++) {
            mParcel.writeByte(val);
        }
    }

    public void timeReadByte(int reps) {
        mParcel.setDataCapacity(reps);
        for (int i = 0; i < reps; i++) {
            mParcel.readByte();
        }
    }

    public void timeWriteInt(int reps) {
        final int val = 0xF;
        for (int i = 0; i < reps; i++) {
            mParcel.writeInt(val);
        }
    }

    public void timeReadInt(int reps) {
        mParcel.setDataCapacity(reps << 2);
        for (int i = 0; i < reps; i++) {
            mParcel.readInt();
        }
    }

    public void timeWriteLong(int reps) {
        final long val = 0xF;
        for (int i = 0; i < reps; i++) {
            mParcel.writeLong(val);
        }
    }

    public void timeReadLong(int reps) {
        mParcel.setDataCapacity(reps << 3);
        for (int i = 0; i < reps; i++) {
            mParcel.readLong();
        }
    }
}