1 /* 2 * Copyright (C) 2010 Google Inc. 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 17 package benchmarks.regression; 18 19 import java.lang.reflect.Constructor; 20 import java.lang.reflect.Field; 21 import java.lang.reflect.Method; 22 23 public class ReflectionBenchmark { 24 public void timeObject_getClass(int reps) throws Exception { 25 C c = new C(); 26 for (int rep = 0; rep < reps; ++rep) { 27 c.getClass(); 28 } 29 } 30 31 public void timeClass_getField(int reps) throws Exception { 32 Class<?> klass = C.class; 33 for (int rep = 0; rep < reps; ++rep) { 34 klass.getField("f"); 35 } 36 } 37 38 public void timeClass_getDeclaredField(int reps) throws Exception { 39 Class<?> klass = C.class; 40 for (int rep = 0; rep < reps; ++rep) { 41 klass.getDeclaredField("f"); 42 } 43 } 44 45 public void timeClass_getConstructor(int reps) throws Exception { 46 Class<?> klass = C.class; 47 for (int rep = 0; rep < reps; ++rep) { 48 klass.getConstructor(); 49 } 50 } 51 52 public void timeClass_newInstance(int reps) throws Exception { 53 Class<?> klass = C.class; 54 Constructor constructor = klass.getConstructor(); 55 for (int rep = 0; rep < reps; ++rep) { 56 constructor.newInstance(); 57 } 58 } 59 60 public void timeClass_getMethod(int reps) throws Exception { 61 Class<?> klass = C.class; 62 for (int rep = 0; rep < reps; ++rep) { 63 klass.getMethod("m"); 64 } 65 } 66 67 public void timeClass_getDeclaredMethod(int reps) throws Exception { 68 Class<?> klass = C.class; 69 for (int rep = 0; rep < reps; ++rep) { 70 klass.getDeclaredMethod("m"); 71 } 72 } 73 74 public void timeField_setInt(int reps) throws Exception { 75 Class<?> klass = C.class; 76 Field f = klass.getDeclaredField("f"); 77 C instance = new C(); 78 for (int rep = 0; rep < reps; ++rep) { 79 f.setInt(instance, 1); 80 } 81 } 82 83 public void timeField_getInt(int reps) throws Exception { 84 Class<?> klass = C.class; 85 Field f = klass.getDeclaredField("f"); 86 C instance = new C(); 87 for (int rep = 0; rep < reps; ++rep) { 88 f.getInt(instance); 89 } 90 } 91 92 public void timeMethod_invokeV(int reps) throws Exception { 93 Class<?> klass = C.class; 94 Method m = klass.getDeclaredMethod("m"); 95 C instance = new C(); 96 for (int rep = 0; rep < reps; ++rep) { 97 m.invoke(instance); 98 } 99 } 100 101 public void timeMethod_invokeStaticV(int reps) throws Exception { 102 Class<?> klass = C.class; 103 Method m = klass.getDeclaredMethod("sm"); 104 for (int rep = 0; rep < reps; ++rep) { 105 m.invoke(null); 106 } 107 } 108 109 public void timeMethod_invokeI(int reps) throws Exception { 110 Class<?> klass = C.class; 111 Method m = klass.getDeclaredMethod("setField", int.class); 112 C instance = new C(); 113 for (int rep = 0; rep < reps; ++rep) { 114 m.invoke(instance, 1); 115 } 116 } 117 118 public void timeMethod_invokePreBoxedI(int reps) throws Exception { 119 Class<?> klass = C.class; 120 Method m = klass.getDeclaredMethod("setField", int.class); 121 C instance = new C(); 122 Integer one = Integer.valueOf(1); 123 for (int rep = 0; rep < reps; ++rep) { 124 m.invoke(instance, one); 125 } 126 } 127 128 public void timeMethod_invokeStaticI(int reps) throws Exception { 129 Class<?> klass = C.class; 130 Method m = klass.getDeclaredMethod("setStaticField", int.class); 131 for (int rep = 0; rep < reps; ++rep) { 132 m.invoke(null, 1); 133 } 134 } 135 136 public void timeMethod_invokeStaticPreBoxedI(int reps) throws Exception { 137 Class<?> klass = C.class; 138 Method m = klass.getDeclaredMethod("setStaticField", int.class); 139 Integer one = Integer.valueOf(1); 140 for (int rep = 0; rep < reps; ++rep) { 141 m.invoke(null, one); 142 } 143 } 144 145 public void timeRegularMethodInvocation(int reps) throws Exception { 146 C instance = new C(); 147 for (int rep = 0; rep < reps; ++rep) { 148 instance.setField(1); 149 } 150 } 151 152 public void timeRegularConstructor(int reps) throws Exception { 153 for (int rep = 0; rep < reps; ++rep) { 154 new C(); 155 } 156 } 157 158 public void timeClass_classNewInstance(int reps) throws Exception { 159 Class<?> klass = C.class; 160 for (int rep = 0; rep < reps; ++rep) { 161 klass.newInstance(); 162 } 163 } 164 165 public void timeClass_isInstance(int reps) throws Exception { 166 D d = new D(); 167 Class<?> klass = IC.class; 168 for (int rep = 0; rep < reps; ++rep) { 169 klass.isInstance(d); 170 } 171 } 172 173 public void timeGetInstanceField(int reps) throws Exception { 174 for (int rep = 0; rep < reps; ++rep) { 175 // The field here (and in timeGetStaticField) were chosen to be 176 // ~75% down the bottom of the alphabetically sorted field list. 177 // It's hard to construct a "fair" test case without resorting to 178 // a class whose field names are created algorithmically. 179 // 180 // TODO: Write a test script that generates both the classes we're 181 // reflecting on and the test case for each of its fields. 182 R.class.getField("mtextAppearanceLargePopupMenu"); 183 } 184 } 185 186 public void timeGetStaticField(int reps) throws Exception { 187 for (int rep = 0; rep < reps; ++rep) { 188 R.class.getField("weekNumberColor"); 189 } 190 } 191 192 public void timeGetInterfaceStaticField(int reps) throws Exception { 193 for (int rep = 0; rep < reps; ++rep) { 194 F.class.getField("sf"); 195 } 196 } 197 198 public void timeGetSuperClassField(int reps) throws Exception { 199 for (int rep = 0; rep < reps; ++rep) { 200 G.class.getField("f"); 201 } 202 } 203 204 205 public static class C { 206 public static int sf = 0; 207 public int f = 0; 208 209 public C() { 210 // A non-empty constructor so we don't get optimized away. 211 f = 1; 212 } 213 214 public void m() { 215 } 216 217 public static void sm() { 218 } 219 220 public void setField(int value) { 221 f = value; 222 } 223 224 public static void setStaticField(int value) { 225 sf = value; 226 } 227 } 228 229 interface IA { 230 } 231 232 interface IB extends IA { 233 } 234 235 interface IC extends IB { 236 public static final int sf = 0; 237 } 238 239 class D implements IC { 240 } 241 242 class E extends D { 243 } 244 245 class F extends E implements IB { 246 } 247 248 class G extends C { 249 } 250 } 251