| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import android.annotation.SuppressLint; |
| 20 |
import android.graphics.Point; |
| 21 |
import android.graphics.Rect; |
| 22 |
import android.util.MergedConfiguration; |
| 23 |
import android.view.InsetsSource; |
| 24 |
import android.view.InsetsState; |
| 25 |
|
| 26 |
import com.google.caliper.AfterExperiment; |
| 27 |
import com.google.caliper.BeforeExperiment; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class ParcelableBenchmark { |
| 39 |
private Parcel mParcel; |
| 40 |
|
| 41 |
@BeforeExperiment |
| 42 |
protected void setUp() { |
| 43 |
mParcel = Parcel.obtain(); |
| 44 |
} |
| 45 |
|
| 46 |
@AfterExperiment |
| 47 |
protected void tearDown() { |
| 48 |
mParcel.recycle(); |
| 49 |
mParcel = null; |
| 50 |
} |
| 51 |
|
| 52 |
public void timeReadWriteMergedConfiguration(int reps) { |
| 53 |
final MergedConfiguration mergedConfiguration = new MergedConfiguration(); |
| 54 |
for (int i = 0; i < reps; i++) { |
| 55 |
mergedConfiguration.writeToParcel(mParcel, 0); |
| 56 |
mParcel.setDataPosition(0); |
| 57 |
mergedConfiguration.readFromParcel(mParcel); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
public void timeReadWriteInsetsState(int reps) { |
| 62 |
final InsetsState insetsState = new InsetsState(); |
| 63 |
for (int i = 0; i < InsetsState.SIZE; i++) { |
| 64 |
insetsState.addSource(new InsetsSource(i)); |
| 65 |
} |
| 66 |
for (int i = 0; i < reps; i++) { |
| 67 |
insetsState.writeToParcel(mParcel, 0); |
| 68 |
mParcel.setDataPosition(0); |
| 69 |
insetsState.readFromParcel(mParcel); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public void timeReadWritePointArray(int reps) { |
| 74 |
final PointArray pointArray = new PointArray(); |
| 75 |
for (int i = 0; i < reps; i++) { |
| 76 |
pointArray.writeToParcel(mParcel, 0); |
| 77 |
mParcel.setDataPosition(0); |
| 78 |
pointArray.readFromParcel(mParcel); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public void timeReadWritePointArrayFast(int reps) { |
| 83 |
final PointArrayFast pointArray = new PointArrayFast(); |
| 84 |
for (int i = 0; i < reps; i++) { |
| 85 |
pointArray.writeToParcel(mParcel, 0); |
| 86 |
mParcel.setDataPosition(0); |
| 87 |
pointArray.readFromParcel(mParcel); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
@SuppressWarnings("ParcelableCreator") |
| 92 |
@SuppressLint("ParcelCreator") |
| 93 |
private static class PointArray implements Parcelable { |
| 94 |
Rect mBounds = new Rect(); |
| 95 |
Point[] mPoints = new Point[10]; |
| 96 |
{ |
| 97 |
for (int i = 0; i < mPoints.length; i++) { |
| 98 |
mPoints[i] = new Point(); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void writeToParcel(Parcel dest, int flags) { |
| 104 |
dest.writeParcelable(mBounds, flags); |
| 105 |
dest.writeParcelableArray(mPoints, flags); |
| 106 |
} |
| 107 |
|
| 108 |
void readFromParcel(Parcel in) { |
| 109 |
mBounds = in.readParcelable(Rect.class.getClassLoader()); |
| 110 |
mPoints = in.readParcelableArray(Point.class.getClassLoader(), Point.class); |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public int describeContents() { |
| 115 |
return 0; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
@SuppressLint("ParcelCreator") |
| 120 |
private static class PointArrayFast extends PointArray { |
| 121 |
|
| 122 |
@Override |
| 123 |
public void writeToParcel(Parcel dest, int flags) { |
| 124 |
mBounds.writeToParcel(dest, flags); |
| 125 |
dest.writeTypedArray(mPoints, flags); |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
void readFromParcel(Parcel in) { |
| 130 |
mBounds.readFromParcel(in); |
| 131 |
in.readTypedArray(mPoints, Point.CREATOR); |
| 132 |
} |
| 133 |
} |
| 134 |
} |