1 /*
2 * Copyright (c) 2023, 2024, 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 */
24
25 import java.lang.invoke.MethodHandles;
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.net.URI;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.concurrent.Callable;
35 import javax.tools.Diagnostic;
36 import javax.tools.DiagnosticCollector;
37 import javax.tools.FileObject;
38 import javax.tools.ForwardingJavaFileManager;
39 import javax.tools.JavaCompiler;
40 import javax.tools.JavaFileManager;
41 import javax.tools.JavaFileObject;
42 import javax.tools.SimpleJavaFileObject;
43 import javax.tools.ToolProvider;
44
45 /**
189 for (int i = 0; i < count; i++) {
190 String source = imports + "public class Test" + i + " {" + testClassBody + "}";
191 sources.add(new SourceFile("Test" + i, source));
192 }
193
194 sources.add(new SourceFile("Sanity", sanitySource));
195 }
196
197 @SuppressWarnings("unchecked")
198 static void validate(byte[] sanityClassFile) throws Throwable {
199 MethodHandles.Lookup lookup = MethodHandles.lookup();
200 Class<?> cls = lookup.defineClass(sanityClassFile);
201 Callable<String> obj = (Callable<String>)cls.getDeclaredConstructor().newInstance();
202 String s = obj.call();
203 if (!s.equals("this is a test")) {
204 throw new RuntimeException("Expected \"this is a test\", but got \"" + s + "\"");
205 }
206 }
207
208 public static void main(String args[]) throws Throwable {
209 long started = System.currentTimeMillis();
210 JavacBenchApp bench = new JavacBenchApp();
211
212 int count = 0;
213 if (args.length > 0) {
214 count = Integer.parseInt(args[0]);
215 if (count >= 0) {
216 bench.setup(count);
217 Map<String, byte[]> allClasses = bench.compile();
218 validate(allClasses.get("Sanity"));
219 }
220 }
221 if (System.getProperty("JavacBenchApp.silent") == null) {
222 // Set this property when running with "perf stat", etc
223 long elapsed = System.currentTimeMillis() - started;
224 System.out.println("Generated source code for " + bench.sources.size() + " classes and compiled them in " + elapsed + " ms");
225 }
226 }
227 }
228
|
1 /*
2 * Copyright (c) 2023, 2025, 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 */
24
25 import java.lang.invoke.MethodHandles;
26 import java.lang.management.ManagementFactory;
27 import java.lang.management.RuntimeMXBean;
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.net.URI;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.concurrent.Callable;
37 import javax.tools.Diagnostic;
38 import javax.tools.DiagnosticCollector;
39 import javax.tools.FileObject;
40 import javax.tools.ForwardingJavaFileManager;
41 import javax.tools.JavaCompiler;
42 import javax.tools.JavaFileManager;
43 import javax.tools.JavaFileObject;
44 import javax.tools.SimpleJavaFileObject;
45 import javax.tools.ToolProvider;
46
47 /**
191 for (int i = 0; i < count; i++) {
192 String source = imports + "public class Test" + i + " {" + testClassBody + "}";
193 sources.add(new SourceFile("Test" + i, source));
194 }
195
196 sources.add(new SourceFile("Sanity", sanitySource));
197 }
198
199 @SuppressWarnings("unchecked")
200 static void validate(byte[] sanityClassFile) throws Throwable {
201 MethodHandles.Lookup lookup = MethodHandles.lookup();
202 Class<?> cls = lookup.defineClass(sanityClassFile);
203 Callable<String> obj = (Callable<String>)cls.getDeclaredConstructor().newInstance();
204 String s = obj.call();
205 if (!s.equals("this is a test")) {
206 throw new RuntimeException("Expected \"this is a test\", but got \"" + s + "\"");
207 }
208 }
209
210 public static void main(String args[]) throws Throwable {
211 if (System.getProperty("JavacBenchApp.leyden.bench") == null) {
212 run(args);
213 } else {
214 leydenDriver(args);
215 }
216 }
217
218 // Write the same output as other leyden benchmarks for easy benchmarking.
219 // See https://github.com/openjdk/leyden/blob/premain/test/hotspot/jtreg/premain/lib/Bench.gmk
220 static void leydenDriver(String args[]) throws Throwable {
221 long mainStart = System.currentTimeMillis();
222 RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
223 // This includes all the time spent inside the JVM before main() is reached
224 // (since os::Posix::init is called and initial_time_count is initialized).
225 long vmStart = runtimeMXBean.getStartTime();
226 long maxBeanOverHead = System.currentTimeMillis() - mainStart;
227
228 run(args);
229
230 long end = System.currentTimeMillis();
231 System.out.println("#### Booted and returned in " + (end - vmStart - maxBeanOverHead) + "ms");
232 System.out.println("#### (debug) mainStart = " + mainStart);
233 System.out.println("#### (debug) vmStart = " + vmStart);
234 System.out.println("#### (debug) before main (mainStart - vmStart) = " + (mainStart - vmStart));
235 System.out.println("#### (debug) maxBeanOverHead = " + maxBeanOverHead);
236 System.out.println("#### (debug) end = " + end);
237 }
238
239 static void run(String args[]) throws Throwable {
240 long started = System.currentTimeMillis();
241 JavacBenchApp bench = new JavacBenchApp();
242
243 int count = 0;
244 if (args.length > 0) {
245 count = Integer.parseInt(args[0]);
246 if (count >= 0) {
247 bench.setup(count);
248 Map<String, byte[]> allClasses = bench.compile();
249 validate(allClasses.get("Sanity"));
250 }
251 }
252 if (System.getProperty("JavacBenchApp.silent") == null) {
253 // Set this property when running with "perf stat", etc
254 long elapsed = System.currentTimeMillis() - started;
255 System.out.println("Generated source code for " + bench.sources.size() + " classes and compiled them in " + elapsed + " ms");
256 }
257 }
258 }
259
|