1/*
2 * Copyright (C) 2013 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 benchmarks;
18
19import com.google.caliper.Param;
20import java.lang.reflect.Array;
21import java.lang.reflect.Constructor;
22import java.util.Arrays;
23
24public class DeepArrayOpsBenchmark {
25    @Param({"1", "4", "16", "256", "2048"}) int arrayLength;
26
27    private Object[] array;
28    private Object[] array2;
29
30    private Object[] array3;
31    private Object[] array4;
32
33    protected void setUp() throws Exception {
34        array = new Object[arrayLength * 13];
35        array2 = new Object[arrayLength * 13];
36        for (int i = 0; i < arrayLength; i += 13) {
37            array[i] = new IntWrapper(i);
38            array2[i] = new IntWrapper(i);
39
40            array[i + 1] = new16ElementObjectarray();
41            array2[i + 1] = new16ElementObjectarray();
42
43            array[i + 2] = new boolean[16];
44            array2[i + 2] = new boolean[16];
45
46            array[i + 3] = new byte[16];
47            array2[i + 3] = new byte[16];
48
49            array[i + 4] = new char[16];
50            array2[i + 4] = new char[16];
51
52            array[i + 5] = new short[16];
53            array2[i + 5] = new short[16];
54
55            array[i + 6] = new float[16];
56            array2[i + 6] = new float[16];
57
58            array[i + 7] = new long[16];
59            array2[i + 7] = new long[16];
60
61            array[i + 8] = new int[16];
62            array2[i + 8] = new int[16];
63
64            array[i + 9] = new double[16];
65            array2[i + 9] = new double[16];
66
67            // Subarray types are concrete objects.
68            array[i + 10] = new16ElementArray(String.class, String.class);
69            array2[i + 10] = new16ElementArray(String.class, String.class);
70
71            array[i + 11] = new16ElementArray(Integer.class, Integer.class);
72            array2[i + 11] = new16ElementArray(Integer.class, Integer.class);
73
74            // Subarray types is an interface.
75            array[i + 12] = new16ElementArray(CharSequence.class, String.class);
76            array2[i + 12] = new16ElementArray(CharSequence.class, String.class);
77        }
78    }
79
80    public void timeDeepHashCode(int reps) {
81        for (int i = 0; i < reps; ++i) {
82            Arrays.deepHashCode(array);
83        }
84    }
85
86    public void timeEquals(int reps) {
87        for (int i = 0; i < reps; ++i) {
88            Arrays.deepEquals(array, array2);
89        }
90    }
91
92    private static final Object[] new16ElementObjectarray() {
93        Object[] array = new Object[16];
94        for (int i = 0; i < 16; ++i) {
95            array[i] = new IntWrapper(i);
96        }
97
98        return array;
99    }
100
101    @SuppressWarnings("unchecked")
102    private static final <T, V> T[] new16ElementArray(Class<T> arrayType, Class<V> type)
103            throws Exception {
104        T[] array = (T []) Array.newInstance(type, 16);
105        if (!arrayType.isAssignableFrom(type)) {
106            throw new IllegalArgumentException(arrayType + " is not assignable from " + type);
107        }
108
109        Constructor<V> constructor = type.getDeclaredConstructor(String.class);
110        for (int i = 0; i < 16; ++i) {
111            array[i] = (T) constructor.newInstance(String.valueOf(i + 1000));
112        }
113
114        return array;
115    }
116
117    /**
118     * A class that provides very basic equals() and hashCode() operations
119     * and doesn't resort to memoization tricks like {@link java.lang.Integer}.
120     *
121     * Useful for providing equal objects that aren't the same (a.equals(b) but
122     * a != b).
123     */
124    public static final class IntWrapper {
125        private final int wrapped;
126
127        public IntWrapper(int wrap) {
128            wrapped = wrap;
129        }
130
131        @Override
132        public int hashCode() {
133            return wrapped;
134        }
135
136        @Override
137        public boolean equals(Object o) {
138            if (!(o instanceof IntWrapper)) {
139                return false;
140            }
141
142            return ((IntWrapper) o).wrapped == this.wrapped;
143        }
144    }
145}
146
147