comparison truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/profiles/LoopConditionProfileTest.java @ 22501:a63bda98cfdb

Extract profiles into separate package. Add isProfilingEnabled in TruffleRuntime to disable profiling in the default runtime; Add low overhead profiles for primitives; Add LoopConditionProfile; Profile footprint/threadsafety improvements; Make toString implementations more consistent; Greatly enhanced javadoc documentation for profiles; Deprecate old profiles
author Christian Humer <christian.humer@oracle.com>
date Wed, 16 Dec 2015 16:38:13 +0100
parents
children 4ba1aa33fda4
comparison
equal deleted inserted replaced
22500:fbe1eb7b4172 22501:a63bda98cfdb
1 /*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.truffle.api.profiles;
24
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.junit.Assert.assertThat;
27
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.experimental.theories.DataPoints;
31 import org.junit.experimental.theories.Theories;
32 import org.junit.experimental.theories.Theory;
33 import org.junit.runner.RunWith;
34
35 @RunWith(Theories.class)
36 public class LoopConditionProfileTest {
37
38 @DataPoints public static boolean[] data = new boolean[]{true, false};
39 @DataPoints public static long[] lengthes = new long[]{Long.MAX_VALUE, 0L, Long.MAX_VALUE / 2L, Long.MAX_VALUE / 2 + 1L, 1L};
40
41 private LoopConditionProfile.Enabled profile;
42
43 @Before
44 public void create() {
45 profile = (LoopConditionProfile.Enabled) LoopConditionProfile.Enabled.create();
46 }
47
48 @Test
49 public void testInitial() {
50 assertThat(profile.getTrueCount(), is(0L));
51 assertThat(profile.getFalseCount(), is(0));
52 profile.toString(); // not crashing
53 }
54
55 @Theory
56 public void testDisabled(boolean value, long length) {
57 LoopConditionProfile.Disabled p = (LoopConditionProfile.Disabled) LoopConditionProfile.Disabled.INSTANCE;
58 p.profileCounted(length); // not crashing
59 assertThat(p.profile(value), is(value));
60 assertThat(p.inject(value), is(value));
61 p.toString(); // test that it is not crashing
62 }
63
64 @Theory
65 public void testProfileOne(boolean value) {
66 boolean result = profile.profile(value);
67
68 assertThat(result, is(value));
69 assertThat(profile.getTrueCount(), is(value ? 1L : 0L));
70 assertThat(profile.getFalseCount(), is(!value ? 1 : 0));
71 profile.toString(); // not crashing
72 }
73
74 @Theory
75 public void testProfileTwo(boolean value0, boolean value1) {
76 boolean result0 = profile.profile(value0);
77 boolean result1 = profile.profile(value1);
78
79 assertThat(result0, is(value0));
80 assertThat(result1, is(value1));
81 assertThat(profile.getTrueCount(), is((value0 ? 1L : 0L) + (value1 ? 1L : 0L)));
82 assertThat(profile.getFalseCount(), is((!value0 ? 1 : 0) + (!value1 ? 1 : 0)));
83 profile.toString(); // not crashing
84 }
85
86 @Theory
87 public void testProfileThree(boolean value0, boolean value1, boolean value2) {
88 boolean result0 = profile.profile(value0);
89 boolean result1 = profile.profile(value1);
90 boolean result2 = profile.profile(value2);
91
92 assertThat(result0, is(value0));
93 assertThat(result1, is(value1));
94 assertThat(result2, is(value2));
95 assertThat(profile.getTrueCount(), is((value0 ? 1L : 0L) + (value1 ? 1L : 0L) + (value2 ? 1L : 0L)));
96 assertThat(profile.getFalseCount(), is((!value0 ? 1 : 0) + (!value1 ? 1 : 0) + (!value2 ? 1 : 0)));
97 profile.toString(); // not crashing
98 }
99
100 @Theory
101 public void testLengthProfiling(long length1, long length2, long length3) {
102 assertThat(profile.inject(false), is(false));
103
104 profile.toString(); // not crashing
105 profile.profileCounted(length1);
106 profile.toString(); // not crashing
107 long expectedLength = length1;
108 assertThat(profile.getTrueCount(), is(expectedLength));
109 assertThat(profile.getFalseCount(), is(1));
110 assertThat(profile.inject(false), is(false));
111
112 profile.profileCounted(length2);
113 profile.toString(); // not crashing
114 expectedLength += length2;
115 if (expectedLength < 0) {
116 assertThat(profile.getTrueCount(), is(length1));
117 assertThat(profile.getFalseCount(), is(1));
118 assertThat(profile.inject(false), is(false));
119
120 profile.profileCounted(length3);
121 expectedLength = length1 + length3;
122
123 if (expectedLength < 0) {
124 assertThat(profile.getTrueCount(), is(length1));
125 assertThat(profile.getFalseCount(), is(1));
126 assertThat(profile.inject(false), is(false));
127 } else {
128 assertThat(profile.getTrueCount(), is(expectedLength));
129 assertThat(profile.getFalseCount(), is(2));
130 assertThat(profile.inject(false), is(false));
131 }
132 return;
133 } else {
134 assertThat(profile.getTrueCount(), is(expectedLength));
135 assertThat(profile.getFalseCount(), is(2));
136 assertThat(profile.inject(false), is(false));
137 }
138
139 profile.profileCounted(length3);
140 profile.toString(); // not crashing
141 expectedLength += length3;
142
143 if (expectedLength < 0) {
144 assertThat(profile.getTrueCount(), is(length1 + length2));
145 assertThat(profile.getFalseCount(), is(2));
146 assertThat(profile.inject(false), is(false));
147 } else {
148 assertThat(profile.getTrueCount(), is(expectedLength));
149 assertThat(profile.getFalseCount(), is(3));
150 assertThat(profile.inject(false), is(false));
151 }
152 profile.toString(); // not crashing
153 }
154
155 }