Skip to content

Commit b2e204a

Browse files
committedMay 15, 2024
Refactored applications/JavacBench.java test; added sanity validation

File tree

6 files changed

+86
-152
lines changed

6 files changed

+86
-152
lines changed
 

‎test/hotspot/jtreg/runtime/cds/appcds/applications/javac/JavacBench.java ‎test/hotspot/jtreg/runtime/cds/appcds/applications/JavacBench.java

+35-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,44 @@
2222
*
2323
*/
2424

25+
/*
26+
* @test id=static
27+
* @summary Run JavacBenchApp with the classic static archive workflow
28+
* @requires vm.cds
29+
* @library /test/lib
30+
* @run driver JavacBench STATIC
31+
*/
32+
33+
/*
34+
* @test id=dynamic
35+
* @summary Run JavacBenchApp with the classic dynamic archive workflow
36+
* @requires vm.cds
37+
* @library /test/lib
38+
* @run driver JavacBench DYNAMIC
39+
*/
40+
41+
/*
42+
* @test id=leyden
43+
* @summary Run JavacBenchApp with Leyden workflow
44+
* @requires vm.cds
45+
* @library /test/lib
46+
* @run driver JavacBench LEYDEN
47+
*/
48+
49+
/*
50+
* @test id=leyden_old
51+
* @summary Run JavacBenchApp with the "OLD" Leyden workflow
52+
* @requires vm.cds
53+
* @library /test/lib
54+
* @run driver JavacBench LEYDEN_OLD
55+
*/
56+
2557
import jdk.test.lib.cds.CDSAppTester;
2658
import jdk.test.lib.helpers.ClassFileInstaller;
2759
import jdk.test.lib.process.OutputAnalyzer;
2860

2961
public class JavacBench {
62+
static String mainClass = JavacBenchApp.class.getName();
3063
static String appJar;
3164

3265
public static void main(String args[]) throws Exception {
@@ -52,8 +85,8 @@ public String classpath(RunMode runMode) {
5285
@Override
5386
public String[] appCommandLine(RunMode runMode) {
5487
return new String[] {
55-
"JavacBenchApp",
56-
"30",
88+
mainClass,
89+
"90",
5790
};
5891
}
5992
}

‎test/hotspot/jtreg/runtime/cds/appcds/applications/javac/JavacBenchApp.java ‎test/hotspot/jtreg/runtime/cds/appcds/applications/JavacBenchApp.java

+51-18
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,25 @@
2222
*
2323
*/
2424

25-
import javax.tools.*;
26-
import java.io.*;
25+
import java.lang.invoke.MethodHandles;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.IOException;
2728
import java.net.URI;
28-
import java.util.*;
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;
2944

3045
/**
3146
* This program tries to compile a large number of classes that exercise a fair amount of
@@ -57,7 +72,7 @@ public ClassFile getJavaFileForOutput(Location location, String name, JavaFileOb
5772
classesMap.put(name, classFile);
5873
return classFile;
5974
}
60-
public Map<String, byte[]> getByteCode() {
75+
public Map<String, byte[]> getCompiledClasses() {
6176
Map<String, byte[]> result = new HashMap<>();
6277
for (Map.Entry<String, ClassFile> entry : classesMap.entrySet()) {
6378
result.put(entry.getKey(), entry.getValue().toByteArray());
@@ -78,19 +93,19 @@ public CharSequence getCharContent(boolean ignore) {
7893
}
7994
}
8095

81-
public Object compile(int count) {
96+
public Map<String, byte[]> compile() {
8297
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
8398
DiagnosticCollector<JavaFileObject> ds = new DiagnosticCollector<>();
84-
Collection<SourceFile> sourceFiles = sources.subList(0, count);
99+
Collection<SourceFile> sourceFiles = sources;
85100

86101
try (FileManager fileManager = new FileManager(compiler.getStandardFileManager(ds, null, null))) {
87102
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, sourceFiles);
88103
if (task.call()) {
89-
return fileManager.getByteCode();
104+
return fileManager.getCompiledClasses();
90105
} else {
91106
for (Diagnostic<? extends JavaFileObject> d : ds.getDiagnostics()) {
92-
System.out.format("Line: %d, %s in %s", d.getLineNumber(), d.getMessage(null), d.getSource().getName());
93-
}
107+
System.out.format("Line: %d, %s in %s", d.getLineNumber(), d.getMessage(null), d.getSource().getName());
108+
}
94109
throw new InternalError("compilation failure");
95110
}
96111
} catch (IOException e) {
@@ -161,34 +176,52 @@ public Point(int x) {
161176
}
162177
""";
163178

164-
List<SourceFile> generate(int count) {
165-
ArrayList<SourceFile> sources = new ArrayList<>(count);
179+
String sanitySource = """
180+
public class Sanity implements java.util.concurrent.Callable<String> {
181+
public String call() {
182+
return "this is a test";
183+
}
184+
}
185+
""";
186+
187+
void setup(int count) {
188+
sources = new ArrayList<>(count);
166189
for (int i = 0; i < count; i++) {
167190
String source = imports + "public class Test" + i + " {" + testClassBody + "}";
168191
sources.add(new SourceFile("Test" + i, source));
169192
}
170-
return sources;
193+
194+
sources.add(new SourceFile("Sanity", sanitySource));
171195
}
172196

173-
public void setup() {
174-
sources = generate(10_000);
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+
}
175206
}
176207

177208
public static void main(String args[]) throws Throwable {
178209
long started = System.currentTimeMillis();
179210
JavacBenchApp bench = new JavacBenchApp();
180-
bench.setup();
211+
181212
int count = 0;
182213
if (args.length > 0) {
183214
count = Integer.parseInt(args[0]);
184-
if (count > 0) {
185-
bench.compile(count);
215+
if (count >= 0) {
216+
bench.setup(count);
217+
Map<String, byte[]> allClasses = bench.compile();
218+
validate(allClasses.get("Sanity"));
186219
}
187220
}
188221
long elapsed = System.currentTimeMillis() - started;
189222
if (System.getProperty("JavacBenchApp.silent") == null) {
190223
// Set this property when running with "perf stat", etc
191-
System.out.println("Generated source code for " + bench.sources.size() + " classes and compiled them for " + count + " times in " + elapsed + " ms");
224+
System.out.println("Generated source code for " + bench.sources.size() + " classes and compiled them in " + elapsed + " ms");
192225
}
193226
}
194227
}

‎test/hotspot/jtreg/runtime/cds/appcds/applications/javac/JavacBenchDynamic.java

-33
This file was deleted.

‎test/hotspot/jtreg/runtime/cds/appcds/applications/javac/JavacBenchLeyden.java

-33
This file was deleted.

‎test/hotspot/jtreg/runtime/cds/appcds/applications/javac/JavacBenchLeydenOldWF.java

-33
This file was deleted.

‎test/hotspot/jtreg/runtime/cds/appcds/applications/javac/JavacBenchStatic.java

-33
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.