comparison graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/ProfilingInfoTest.java @ 8612:02ef91b94656

finished ProfilingInfo testcases
author Christian Haeubl <haeubl@ssw.jku.at>
date Thu, 28 Mar 2013 12:55:13 +0100
parents 6c4db417385a
children 7c00d66b81bb
comparison
equal deleted inserted replaced
8611:6c4db417385a 8612:02ef91b94656
38 38
39 @Test 39 @Test
40 public void testBranchTakenProbability() { 40 public void testBranchTakenProbability() {
41 ProfilingInfo info = profile("branchProbabilitySnippet", 0); 41 ProfilingInfo info = profile("branchProbabilitySnippet", 0);
42 assertEquals(0.0, info.getBranchTakenProbability(1)); 42 assertEquals(0.0, info.getBranchTakenProbability(1));
43 assertEquals(100, info.getExecutionCount(1)); 43 assertEquals(N, info.getExecutionCount(1));
44 assertEquals(-1.0, info.getBranchTakenProbability(8)); 44 assertEquals(-1.0, info.getBranchTakenProbability(8));
45 assertEquals(0, info.getExecutionCount(8)); 45 assertEquals(0, info.getExecutionCount(8));
46 46
47 info = profile("branchProbabilitySnippet", 1); 47 info = profile("branchProbabilitySnippet", 1);
48 assertEquals(1.0, info.getBranchTakenProbability(1)); 48 assertEquals(1.0, info.getBranchTakenProbability(1));
49 assertEquals(100, info.getExecutionCount(1)); 49 assertEquals(N, info.getExecutionCount(1));
50 assertEquals(0.0, info.getBranchTakenProbability(8)); 50 assertEquals(0.0, info.getBranchTakenProbability(8));
51 assertEquals(100, info.getExecutionCount(8)); 51 assertEquals(N, info.getExecutionCount(8));
52 52
53 info = profile("branchProbabilitySnippet", 2); 53 info = profile("branchProbabilitySnippet", 2);
54 assertEquals(1.0, info.getBranchTakenProbability(1)); 54 assertEquals(1.0, info.getBranchTakenProbability(1));
55 assertEquals(100, info.getExecutionCount(1)); 55 assertEquals(N, info.getExecutionCount(1));
56 assertEquals(1.0, info.getBranchTakenProbability(8)); 56 assertEquals(1.0, info.getBranchTakenProbability(8));
57 assertEquals(100, info.getExecutionCount(8)); 57 assertEquals(N, info.getExecutionCount(8));
58
59 continueProfiling(3 * N, "branchProbabilitySnippet", 0);
60 assertEquals(0.25, info.getBranchTakenProbability(1));
61 assertEquals(4 * N, info.getExecutionCount(1));
62 assertEquals(1.0, info.getBranchTakenProbability(8));
63 assertEquals(N, info.getExecutionCount(8));
64
65 resetProfile("branchProbabilitySnippet");
66 assertEquals(-1.0, info.getBranchTakenProbability(1));
67 assertEquals(0, info.getExecutionCount(1));
68 assertEquals(-1.0, info.getBranchTakenProbability(8));
69 assertEquals(0, info.getExecutionCount(8));
58 } 70 }
59 71
60 public static int branchProbabilitySnippet(int value) { 72 public static int branchProbabilitySnippet(int value) {
61 if (value == 0) { 73 if (value == 0) {
62 return -1; 74 return -1;
75 info = profile("switchProbabilitySnippet", 1); 87 info = profile("switchProbabilitySnippet", 1);
76 assertEquals(new double[]{0.0, 1.0, 0.0}, info.getSwitchProbabilities(1)); 88 assertEquals(new double[]{0.0, 1.0, 0.0}, info.getSwitchProbabilities(1));
77 89
78 info = profile("switchProbabilitySnippet", 2); 90 info = profile("switchProbabilitySnippet", 2);
79 assertEquals(new double[]{0.0, 0.0, 1.0}, info.getSwitchProbabilities(1)); 91 assertEquals(new double[]{0.0, 0.0, 1.0}, info.getSwitchProbabilities(1));
92
93 resetProfile("switchProbabilitySnippet");
94 assertNull(info.getSwitchProbabilities(1));
80 } 95 }
81 96
82 public static int switchProbabilitySnippet(int value) { 97 public static int switchProbabilitySnippet(int value) {
83 switch (value) { 98 switch (value) {
84 case 0: 99 case 0:
143 assertEquals(2, typeProfile.getTypes().length); 158 assertEquals(2, typeProfile.getTypes().length);
144 assertEquals(stringType, typeProfile.getTypes()[0].getType()); 159 assertEquals(stringType, typeProfile.getTypes()[0].getType());
145 assertEquals(stringBuilderType, typeProfile.getTypes()[1].getType()); 160 assertEquals(stringBuilderType, typeProfile.getTypes()[1].getType());
146 assertEquals(0.5, typeProfile.getTypes()[0].getProbability()); 161 assertEquals(0.5, typeProfile.getTypes()[0].getProbability());
147 assertEquals(0.5, typeProfile.getTypes()[1].getProbability()); 162 assertEquals(0.5, typeProfile.getTypes()[1].getProbability());
163
164 resetProfile(methodName);
165 typeProfile = info.getTypeProfile(bci);
166 assertNull(typeProfile);
148 } 167 }
149 168
150 @Test 169 @Test
151 public void testExceptionSeen() { 170 public void testExceptionSeen() {
152 ProfilingInfo info = profile("nullPointerExceptionSnippet", (Object) null); 171 // NullPointerException
172 ProfilingInfo info = profile("nullPointerExceptionSnippet", 5);
173 assertEquals(TriState.FALSE, info.getExceptionSeen(1));
174
175 info = profile("nullPointerExceptionSnippet", (Object) null);
153 assertEquals(TriState.TRUE, info.getExceptionSeen(1)); 176 assertEquals(TriState.TRUE, info.getExceptionSeen(1));
154 177
155 info = profile("nullPointerExceptionSnippet", 5); 178 resetProfile("nullPointerExceptionSnippet");
156 assertEquals(TriState.FALSE, info.getExceptionSeen(1)); 179 assertEquals(TriState.FALSE, info.getExceptionSeen(1));
180
181 // ArrayOutOfBoundsException
182 info = profile("arrayIndexOutOfBoundsExceptionSnippet", new int[1]);
183 assertEquals(TriState.FALSE, info.getExceptionSeen(2));
157 184
158 info = profile("arrayIndexOutOfBoundsExceptionSnippet", new int[0]); 185 info = profile("arrayIndexOutOfBoundsExceptionSnippet", new int[0]);
159 assertEquals(TriState.TRUE, info.getExceptionSeen(2)); 186 assertEquals(TriState.TRUE, info.getExceptionSeen(2));
160 187
161 info = profile("arrayIndexOutOfBoundsExceptionSnippet", new int[1]); 188 resetProfile("arrayIndexOutOfBoundsExceptionSnippet");
162 assertEquals(TriState.FALSE, info.getExceptionSeen(2)); 189 assertEquals(TriState.FALSE, info.getExceptionSeen(2));
190
191 // CheckCastException
192 info = profile("checkCastExceptionSnippet", "ABC");
193 assertEquals(TriState.FALSE, info.getExceptionSeen(1));
163 194
164 info = profile("checkCastExceptionSnippet", 5); 195 info = profile("checkCastExceptionSnippet", 5);
165 assertEquals(TriState.TRUE, info.getExceptionSeen(1)); 196 assertEquals(TriState.TRUE, info.getExceptionSeen(1));
166 197
167 info = profile("checkCastExceptionSnippet", "ABC"); 198 resetProfile("checkCastExceptionSnippet");
199 assertEquals(TriState.FALSE, info.getExceptionSeen(1));
200
201 // Invoke with exception
202 info = profile("invokeWithExceptionSnippet", false);
168 assertEquals(TriState.FALSE, info.getExceptionSeen(1)); 203 assertEquals(TriState.FALSE, info.getExceptionSeen(1));
169 204
170 info = profile("invokeWithExceptionSnippet", true); 205 info = profile("invokeWithExceptionSnippet", true);
171 assertEquals(TriState.TRUE, info.getExceptionSeen(1)); 206 assertEquals(TriState.TRUE, info.getExceptionSeen(1));
172 207
173 info = profile("invokeWithExceptionSnippet", false); 208 resetProfile("invokeWithExceptionSnippet");
174 assertEquals(TriState.FALSE, info.getExceptionSeen(1)); 209 assertEquals(TriState.FALSE, info.getExceptionSeen(1));
175 } 210 }
176 211
177 public static int nullPointerExceptionSnippet(Object obj) { 212 public static int nullPointerExceptionSnippet(Object obj) {
178 try { 213 try {
226 assertEquals(TriState.TRUE, info.getNullSeen(1)); 261 assertEquals(TriState.TRUE, info.getNullSeen(1));
227 262
228 continueProfiling("instanceOfSnippet", 0.0); 263 continueProfiling("instanceOfSnippet", 0.0);
229 assertEquals(TriState.TRUE, info.getNullSeen(1)); 264 assertEquals(TriState.TRUE, info.getNullSeen(1));
230 265
231 info = profile("instanceOfSnippet", (Object) null); 266 resetProfile("instanceOfSnippet");
232 assertEquals(TriState.TRUE, info.getNullSeen(1)); 267 assertEquals(TriState.FALSE, info.getNullSeen(1));
233 }
234
235 @Test
236 public void testDeoptimizationCount() {
237 // TODO (chaeubl): implement
238 } 268 }
239 269
240 private ProfilingInfo profile(String methodName, Object... args) { 270 private ProfilingInfo profile(String methodName, Object... args) {
241 return profile(true, methodName, args); 271 return profile(true, N, methodName, args);
242 } 272 }
243 273
244 private void continueProfiling(String methodName, Object... args) { 274 private void continueProfiling(String methodName, Object... args) {
245 profile(false, methodName, args); 275 profile(false, N, methodName, args);
246 } 276 }
247 277
248 private ProfilingInfo profile(boolean resetProfile, String methodName, Object... args) { 278 private void continueProfiling(int executions, String methodName, Object... args) {
279 profile(false, executions, methodName, args);
280 }
281
282 private ProfilingInfo profile(boolean resetProfile, int executions, String methodName, Object... args) {
249 Method method = getMethod(methodName); 283 Method method = getMethod(methodName);
250 Assert.assertTrue(Modifier.isStatic(method.getModifiers())); 284 Assert.assertTrue(Modifier.isStatic(method.getModifiers()));
251 285
252 ResolvedJavaMethod javaMethod = runtime.lookupJavaMethod(method); 286 ResolvedJavaMethod javaMethod = runtime.lookupJavaMethod(method);
253 if (resetProfile) { 287 if (resetProfile) {
254 javaMethod.reprofile(); 288 javaMethod.reprofile();
255 } 289 }
256 290
257 for (int i = 0; i < N; ++i) { 291 for (int i = 0; i < executions; ++i) {
258 try { 292 try {
259 method.invoke(null, args); 293 method.invoke(null, args);
260 } catch (Throwable e) { 294 } catch (Throwable e) {
261 fail("method should not throw an exception: " + e.toString()); 295 fail("method should not throw an exception: " + e.toString());
262 } 296 }
263 } 297 }
264 298
265 return javaMethod.getProfilingInfo(); 299 return javaMethod.getProfilingInfo();
266 } 300 }
301
302 private void resetProfile(String methodName) {
303 ResolvedJavaMethod javaMethod = runtime.lookupJavaMethod(getMethod(methodName));
304 javaMethod.reprofile();
305 }
267 } 306 }