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 */
24
25 import java.util.*;
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Paths;
29 import java.nio.charset.StandardCharsets;
30
31 import com.oracle.java.testlibrary.*;
32
33 /**
34 * Test runner that invokes all methods implemented by particular Expr
35 * with random arguments in two different JVM processes and compares output.
36 * JVMs being started in different modes - one in int and other in comp
37 * with C2 and disabled tiered compilation.
38 */
39 public class BMITestRunner {
40
41 enum VMMode {
42 COMP, INT;
43 };
44
45 public static int DEFAULT_ITERATIONS_COUNT = 4000;
46
47 /**
48 * Execute all methods implemented by <b>expr</b> in int and comp modes
49 * and compare output.
50 * Test pass only of output obtained with different VM modes is equal.
51 * To control behaviour of test following options could be passed:
52 * <ul>
53 * <li>-iterations=<N> each operation implemented by
54 * <b>expr</b> will be executed <i>N</i> times. Default value
55 * is 4000.</li>
56 * <li>-seed=<SEED> arguments for <b>expr</b>'s methods
57 * obtained via RNG initiated with seed <i>SEED</i>. By default
58 * some random seed will be used.</li>
59 * </ul>
60 *
61 * @param expr operation that should be tested
62 * @param testOpts options to control test behaviour
63 * @param additionalVMOpts additional options for VM
64 *
65 * @throws Throwable if test failed.
66 */
67 public static void runTests(Class<? extends Expr> expr,
68 String testOpts[],
69 String... additionalVMOpts)
70 throws Throwable {
71
72 int seed = new Random().nextInt();
73 int iterations = DEFAULT_ITERATIONS_COUNT;
74
75 for (String testOption : testOpts) {
76 if (testOption.startsWith("-iterations=")) {
77 iterations = Integer.valueOf(testOption.
78 replace("-iterations=", ""));
79 } else if (testOption.startsWith("-seed=")) {
80 seed = Integer.valueOf(testOption.replace("-seed=", ""));
81 }
82 }
83
84 System.out.println("Running test with seed: " + seed);
85
86 OutputAnalyzer intOutput = runTest(expr, VMMode.INT,
87 additionalVMOpts,
88 seed, iterations);
89 OutputAnalyzer compOutput = runTest(expr, VMMode.COMP,
90 additionalVMOpts,
91 seed, iterations);
92
93 dumpOutput(intOutput, "int");
94 dumpOutput(compOutput, "comp");
95
96 Asserts.assertStringsEqual(intOutput.getStdout(),
97 compOutput.getStdout(),
98 "Results obtained in -Xint and " +
99 "-Xcomp should be the same.");
100 }
101
102 /**
103 * Execute tests on methods implemented by <b>expr</b> in new VM
104 * started in <b>testVMMode</b> mode.
105 *
122
123 Collections.addAll(vmOpts, additionalVMOpts);
124
125 //setup mode-specific options
126 switch (testVMMode) {
127 case INT:
128 Collections.addAll(vmOpts, new String[] { "-Xint" });
129 break;
130 case COMP:
131 Collections.addAll(vmOpts, new String[] {
132 "-Xcomp",
133 "-XX:-TieredCompilation",
134 String.format("-XX:CompileCommand=compileonly,%s::*",
135 expr.getName())
136 });
137 break;
138 }
139
140 Collections.addAll(vmOpts, new String[] {
141 "-XX:+DisplayVMOutputToStderr",
142 Executor.class.getName(),
143 expr.getName(),
144 new Integer(seed).toString(),
145 new Integer(iterations).toString()
146 });
147
148 OutputAnalyzer outputAnalyzer = ProcessTools.
149 executeTestJvm(vmOpts.toArray(new String[vmOpts.size()]));
150
151 outputAnalyzer.shouldHaveExitValue(0);
152
153 return outputAnalyzer;
154 }
155
156 /**
157 * Dump stdout and stderr of test process to <i>prefix</i>.test.out
158 * and <i>prefix</i>.test.err respectively.
159 *
160 * @param outputAnalyzer OutputAnalyzer whom output should be dumped
161 * @param prefix Prefix that will be used in file names.
162 * @throws IOException if unable to dump output to file.
163 */
164 protected static void dumpOutput(OutputAnalyzer outputAnalyzer,
165 String prefix)
166 throws IOException {
167 Files.write(Paths.get(prefix + ".test.out"),
168 outputAnalyzer.getStdout().getBytes());
169
170 Files.write(Paths.get(prefix + ".test.err"),
171 outputAnalyzer.getStderr().getBytes());
172 }
173
174
175 /**
176 * Executor that invoke all methods implemented by particular
177 * Expr instance.
178 */
179 public static class Executor {
180
181 /**
182 * Usage: BMITestRunner$Executor <ExprClassName> <seed> <iterations>
183 */
184 public static void main(String args[]) throws Exception {
185 @SuppressWarnings("unchecked")
186 Class<? extends Expr> exprClass =
187 (Class<? extends Expr>)Class.forName(args[0]);
188 Expr expr = exprClass.getConstructor().newInstance();
189 Random rng = new Random(Integer.valueOf(args[1]));
190 int iterations = Integer.valueOf(args[2]);
191 runTests(expr, iterations, rng);
192 }
193
194
195 public static int[] getIntBitShifts() {
196 //SIZE+1 shift is for zero.
197 int data[] = new int[Integer.SIZE+1];
198 for (int s = 0; s < data.length; s++) {
199 data[s] = 1<<s;
200 }
201 return data;
202 }
203
204 public static long[] getLongBitShifts() {
205 //SIZE+1 shift is for zero.
206 long data[] = new long[Long.SIZE+1];
207 for (int s = 0; s < data.length; s++) {
208 data[s] = 1L<<s;
209 }
210 return data;
211 }
|
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 */
24
25 import com.oracle.java.testlibrary.Asserts;
26 import com.oracle.java.testlibrary.OutputAnalyzer;
27 import com.oracle.java.testlibrary.ProcessTools;
28 import com.oracle.java.testlibrary.Utils;
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32 import java.util.Collections;
33 import java.util.LinkedList;
34 import java.util.List;
35 import java.util.Random;
36
37 /**
38 * Test runner that invokes all methods implemented by particular Expr
39 * with random arguments in two different JVM processes and compares output.
40 * JVMs being started in different modes - one in int and other in comp
41 * with C2 and disabled tiered compilation.
42 */
43 public class BMITestRunner {
44
45 enum VMMode {
46 COMP, INT;
47 };
48
49 public static int DEFAULT_ITERATIONS_COUNT = 4000;
50
51 /**
52 * Execute all methods implemented by <b>expr</b> in int and comp modes
53 * and compare output.
54 * Test pass only of output obtained with different VM modes is equal.
55 * To control behaviour of test following options could be passed:
56 * <ul>
57 * <li>-iterations=<N> each operation implemented by
58 * <b>expr</b> will be executed <i>N</i> times. Default value
59 * is 4000.</li>
60 * <li>-seed=<SEED> arguments for <b>expr</b>'s methods
61 * obtained via RNG initiated with seed <i>SEED</i>. By default
62 * some random seed will be used.</li>
63 * </ul>
64 *
65 * @param expr operation that should be tested
66 * @param testOpts options to control test behaviour
67 * @param additionalVMOpts additional options for VM
68 *
69 * @throws Throwable if test failed.
70 */
71 public static void runTests(Class<? extends Expr> expr,
72 String testOpts[],
73 String... additionalVMOpts)
74 throws Throwable {
75
76 int seed = Utils.getRandomInstance().nextInt();
77 int iterations = DEFAULT_ITERATIONS_COUNT;
78
79 for (String testOption : testOpts) {
80 if (testOption.startsWith("-iterations=")) {
81 iterations = Integer.valueOf(testOption.
82 replace("-iterations=", ""));
83 } else if (testOption.startsWith("-seed=")) {
84 seed = Integer.valueOf(testOption.replace("-seed=", ""));
85 }
86 }
87
88 OutputAnalyzer intOutput = runTest(expr, VMMode.INT,
89 additionalVMOpts,
90 seed, iterations);
91 OutputAnalyzer compOutput = runTest(expr, VMMode.COMP,
92 additionalVMOpts,
93 seed, iterations);
94
95 dumpOutput(intOutput, "int");
96 dumpOutput(compOutput, "comp");
97
98 Asserts.assertStringsEqual(intOutput.getStdout(),
99 compOutput.getStdout(),
100 "Results obtained in -Xint and " +
101 "-Xcomp should be the same.");
102 }
103
104 /**
105 * Execute tests on methods implemented by <b>expr</b> in new VM
106 * started in <b>testVMMode</b> mode.
107 *
124
125 Collections.addAll(vmOpts, additionalVMOpts);
126
127 //setup mode-specific options
128 switch (testVMMode) {
129 case INT:
130 Collections.addAll(vmOpts, new String[] { "-Xint" });
131 break;
132 case COMP:
133 Collections.addAll(vmOpts, new String[] {
134 "-Xcomp",
135 "-XX:-TieredCompilation",
136 String.format("-XX:CompileCommand=compileonly,%s::*",
137 expr.getName())
138 });
139 break;
140 }
141
142 Collections.addAll(vmOpts, new String[] {
143 "-XX:+DisplayVMOutputToStderr",
144 "-D" + Utils.SEED_PROPERTY_NAME + "=" + seed,
145 Executor.class.getName(),
146 expr.getName(),
147 new Integer(iterations).toString()
148 });
149
150 OutputAnalyzer outputAnalyzer = ProcessTools.
151 executeTestJvm(vmOpts.toArray(new String[vmOpts.size()]));
152
153 outputAnalyzer.shouldHaveExitValue(0);
154
155 return outputAnalyzer;
156 }
157
158 /**
159 * Dump stdout and stderr of test process to <i>prefix</i>.test.out
160 * and <i>prefix</i>.test.err respectively.
161 *
162 * @param outputAnalyzer OutputAnalyzer whom output should be dumped
163 * @param prefix Prefix that will be used in file names.
164 * @throws IOException if unable to dump output to file.
165 */
166 protected static void dumpOutput(OutputAnalyzer outputAnalyzer,
167 String prefix)
168 throws IOException {
169 Files.write(Paths.get(prefix + ".test.out"),
170 outputAnalyzer.getStdout().getBytes());
171
172 Files.write(Paths.get(prefix + ".test.err"),
173 outputAnalyzer.getStderr().getBytes());
174 }
175
176
177 /**
178 * Executor that invoke all methods implemented by particular
179 * Expr instance.
180 */
181 public static class Executor {
182
183 /**
184 * Usage: BMITestRunner$Executor <ExprClassName> <iterations>
185 */
186 public static void main(String args[]) throws Exception {
187 @SuppressWarnings("unchecked")
188 Class<? extends Expr> exprClass =
189 (Class<? extends Expr>)Class.forName(args[0]);
190 Expr expr = exprClass.getConstructor().newInstance();
191 int iterations = Integer.valueOf(args[1]);
192 runTests(expr, iterations, Utils.getRandomInstance());
193 }
194
195
196 public static int[] getIntBitShifts() {
197 //SIZE+1 shift is for zero.
198 int data[] = new int[Integer.SIZE+1];
199 for (int s = 0; s < data.length; s++) {
200 data[s] = 1<<s;
201 }
202 return data;
203 }
204
205 public static long[] getLongBitShifts() {
206 //SIZE+1 shift is for zero.
207 long data[] = new long[Long.SIZE+1];
208 for (int s = 0; s < data.length; s++) {
209 data[s] = 1L<<s;
210 }
211 return data;
212 }
|