1 /* |
|
2 * Copyright (c) 2008, 2010, 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.io.ByteArrayOutputStream; |
|
26 import java.io.IOException; |
|
27 import java.io.OutputStream; |
|
28 import java.io.PrintWriter; |
|
29 import java.io.StringWriter; |
|
30 |
|
31 import java.lang.reflect.Method; |
|
32 import java.net.URI; |
|
33 import java.util.Arrays; |
|
34 import java.util.Vector; |
|
35 |
|
36 import javax.tools.Diagnostic; |
|
37 import javax.tools.DiagnosticCollector; |
|
38 import javax.tools.FileObject; |
|
39 import javax.tools.ForwardingJavaFileManager; |
|
40 import javax.tools.JavaCompiler; |
|
41 import javax.tools.JavaCompiler.CompilationTask; |
|
42 import javax.tools.JavaFileManager; |
|
43 import javax.tools.JavaFileObject; |
|
44 import javax.tools.JavaFileObject.Kind; |
|
45 import javax.tools.SimpleJavaFileObject; |
|
46 import javax.tools.StandardJavaFileManager; |
|
47 import javax.tools.ToolProvider; |
|
48 |
|
49 /* |
|
50 * @ignore 6959423 |
|
51 * @test SortMethodsTest |
|
52 * @bug 6925573 |
|
53 * @summary verify that class loading does not need quadratic time with regard to the number of class |
|
54 methods. |
|
55 * @run main SortMethodsTest |
|
56 * @author volker.simonis@gmail.com |
|
57 */ |
|
58 |
|
59 public class SortMethodsTest { |
|
60 |
|
61 static String createClass(String name, int nrOfMethods) { |
|
62 StringWriter sw = new StringWriter(); |
|
63 PrintWriter pw = new PrintWriter(sw); |
|
64 pw.println("public class " + name + "{"); |
|
65 for (int i = 0; i < nrOfMethods; i++) { |
|
66 pw.println(" public void m" + i + "() {}"); |
|
67 } |
|
68 pw.println(" public static String sayHello() {"); |
|
69 pw.println(" return \"Hello from class \" + " + name + |
|
70 ".class.getName() + \" with \" + " + name + |
|
71 ".class.getDeclaredMethods().length + \" methods\";"); |
|
72 pw.println(" }"); |
|
73 pw.println("}"); |
|
74 pw.close(); |
|
75 return sw.toString(); |
|
76 } |
|
77 |
|
78 public static void main(String args[]) { |
|
79 |
|
80 JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
|
81 DiagnosticCollector<JavaFileObject> diags = new DiagnosticCollector<JavaFileObject>(); |
|
82 final String cName = new String("ManyMethodsClass"); |
|
83 Vector<Long> results = new Vector<Long>(); |
|
84 |
|
85 for (int i = 6; i < 600000; i*=10) { |
|
86 String klass = createClass(cName, i); |
|
87 JavaMemoryFileObject file = new JavaMemoryFileObject(cName, klass); |
|
88 MemoryFileManager mfm = new MemoryFileManager(comp.getStandardFileManager(diags, null, null), file); |
|
89 CompilationTask task = comp.getTask(null, mfm, diags, null, null, Arrays.asList(file)); |
|
90 |
|
91 if (task.call()) { |
|
92 try { |
|
93 MemoryClassLoader mcl = new MemoryClassLoader(file); |
|
94 long start = System.nanoTime(); |
|
95 Class<? extends Object> c = Class.forName(cName, true, mcl); |
|
96 long end = System.nanoTime(); |
|
97 results.add(end - start); |
|
98 Method m = c.getDeclaredMethod("sayHello", new Class[0]); |
|
99 String ret = (String)m.invoke(null, new Object[0]); |
|
100 System.out.println(ret + " (loaded and resloved in " + (end - start) + "ns)"); |
|
101 } catch (Exception e) { |
|
102 System.err.println(e); |
|
103 } |
|
104 } |
|
105 else { |
|
106 System.out.println(klass); |
|
107 System.out.println(); |
|
108 for (Diagnostic diag : diags.getDiagnostics()) { |
|
109 System.out.println(diag.getCode() + "\n" + diag.getKind() + "\n" + diag.getPosition()); |
|
110 System.out.println(diag.getSource() + "\n" + diag.getMessage(null)); |
|
111 } |
|
112 } |
|
113 } |
|
114 |
|
115 long lastRatio = 0; |
|
116 for (int i = 2; i < results.size(); i++) { |
|
117 long normalized1 = Math.max(results.get(i-1) - results.get(0), 1); |
|
118 long normalized2 = Math.max(results.get(i) - results.get(0), 1); |
|
119 long ratio = normalized2/normalized1; |
|
120 lastRatio = ratio; |
|
121 System.out.println("10 x more methods requires " + ratio + " x more time"); |
|
122 } |
|
123 // The following is just vague estimation but seems to work on current x86_64 and sparcv9 machines |
|
124 if (lastRatio > 80) { |
|
125 throw new RuntimeException("ATTENTION: it seems that class loading needs quadratic time with regard to the number of class methods!!!"); |
|
126 } |
|
127 } |
|
128 } |
|
129 |
|
130 class JavaMemoryFileObject extends SimpleJavaFileObject { |
|
131 |
|
132 private final String code; |
|
133 private ByteArrayOutputStream byteCode; |
|
134 |
|
135 JavaMemoryFileObject(String name, String code) { |
|
136 super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE); |
|
137 this.code = code; |
|
138 } |
|
139 |
|
140 @Override |
|
141 public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
|
142 return code; |
|
143 } |
|
144 |
|
145 @Override |
|
146 public OutputStream openOutputStream() { |
|
147 byteCode = new ByteArrayOutputStream(); |
|
148 return byteCode; |
|
149 } |
|
150 |
|
151 byte[] getByteCode() { |
|
152 return byteCode.toByteArray(); |
|
153 } |
|
154 } |
|
155 |
|
156 class MemoryClassLoader extends ClassLoader { |
|
157 |
|
158 private final JavaMemoryFileObject jfo; |
|
159 |
|
160 public MemoryClassLoader(JavaMemoryFileObject jfo) { |
|
161 this.jfo = jfo; |
|
162 } |
|
163 |
|
164 public Class findClass(String name) { |
|
165 byte[] b = jfo.getByteCode(); |
|
166 return defineClass(name, b, 0, b.length); |
|
167 } |
|
168 } |
|
169 |
|
170 class MemoryFileManager extends ForwardingJavaFileManager<JavaFileManager> { |
|
171 |
|
172 private final JavaFileObject jfo; |
|
173 |
|
174 public MemoryFileManager(StandardJavaFileManager jfm, JavaFileObject jfo) { |
|
175 super(jfm); |
|
176 this.jfo = jfo; |
|
177 } |
|
178 |
|
179 @Override |
|
180 public FileObject getFileForInput(Location location, String packageName, |
|
181 String relativeName) throws IOException { |
|
182 return jfo; |
|
183 } |
|
184 |
|
185 @Override |
|
186 public JavaFileObject getJavaFileForOutput(Location location, String qualifiedName, |
|
187 Kind kind, FileObject outputFile) throws IOException { |
|
188 return jfo; |
|
189 } |
|
190 |
|
191 } |
|