Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/6925573/SortMethodsTest.java
32284 views
/*1* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324import java.io.ByteArrayOutputStream;25import java.io.IOException;26import java.io.OutputStream;27import java.io.PrintWriter;28import java.io.StringWriter;2930import java.lang.reflect.Method;31import java.net.URI;32import java.util.Arrays;33import java.util.Vector;3435import javax.tools.Diagnostic;36import javax.tools.DiagnosticCollector;37import javax.tools.FileObject;38import javax.tools.ForwardingJavaFileManager;39import javax.tools.JavaCompiler;40import javax.tools.JavaCompiler.CompilationTask;41import javax.tools.JavaFileManager;42import javax.tools.JavaFileObject;43import javax.tools.JavaFileObject.Kind;44import javax.tools.SimpleJavaFileObject;45import javax.tools.StandardJavaFileManager;46import javax.tools.ToolProvider;4748/*49* @test SortMethodsTest50* @bug 692557351* @summary verify that class loading does not need quadratic time with regard to the number of class52methods.53* @run main SortMethodsTest54* @author [email protected]55*/5657public class SortMethodsTest {5859static String createClass(String name, int nrOfMethods) {60StringWriter sw = new StringWriter();61PrintWriter pw = new PrintWriter(sw);62pw.println("public class " + name + "{");63for (int i = 0; i < nrOfMethods; i++) {64pw.println(" public void m" + i + "() {}");65}66pw.println(" public static String sayHello() {");67pw.println(" return \"Hello from class \" + " + name +68".class.getName() + \" with \" + " + name +69".class.getDeclaredMethods().length + \" methods\";");70pw.println(" }");71pw.println("}");72pw.close();73return sw.toString();74}7576public static void main(String args[]) {7778JavaCompiler comp = ToolProvider.getSystemJavaCompiler();79DiagnosticCollector<JavaFileObject> diags = new DiagnosticCollector<JavaFileObject>();80final String cName = new String("ManyMethodsClass");81Vector<Long> results = new Vector<Long>();8283for (int i = 6; i < 600000; i*=10) {84String klass = createClass(cName, i);85JavaMemoryFileObject file = new JavaMemoryFileObject(cName, klass);86MemoryFileManager mfm = new MemoryFileManager(comp.getStandardFileManager(diags, null, null), file);87CompilationTask task = comp.getTask(null, mfm, diags, null, null, Arrays.asList(file));8889if (task.call()) {90try {91MemoryClassLoader mcl = new MemoryClassLoader(file);92long start = System.nanoTime();93Class<? extends Object> c = Class.forName(cName, true, mcl);94long end = System.nanoTime();95results.add(end - start);96Method m = c.getDeclaredMethod("sayHello", new Class[0]);97String ret = (String)m.invoke(null, new Object[0]);98System.out.println(ret + " (loaded and resloved in " + (end - start) + "ns)");99} catch (Exception e) {100System.err.println(e);101}102}103else {104System.out.println(klass);105System.out.println();106for (Diagnostic diag : diags.getDiagnostics()) {107System.out.println(diag.getCode() + "\n" + diag.getKind() + "\n" + diag.getPosition());108System.out.println(diag.getSource() + "\n" + diag.getMessage(null));109}110}111}112113long lastRatio = 0;114for (int i = 2; i < results.size(); i++) {115long normalized1 = Math.max(results.get(i-1) - results.get(0), 1);116long normalized2 = Math.max(results.get(i) - results.get(0), 1);117long ratio = normalized2/normalized1;118lastRatio = ratio;119System.out.println("10 x more methods requires " + ratio + " x more time");120}121// The following is just vague estimation but seems to work on current x86_64 and sparcv9 machines122if (lastRatio > 80) {123throw new RuntimeException("ATTENTION: it seems that class loading needs quadratic time with regard to the number of class methods!!!");124}125}126}127128class JavaMemoryFileObject extends SimpleJavaFileObject {129130private final String code;131private ByteArrayOutputStream byteCode;132133JavaMemoryFileObject(String name, String code) {134super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE);135this.code = code;136}137138@Override139public CharSequence getCharContent(boolean ignoreEncodingErrors) {140return code;141}142143@Override144public OutputStream openOutputStream() {145byteCode = new ByteArrayOutputStream();146return byteCode;147}148149byte[] getByteCode() {150return byteCode.toByteArray();151}152}153154class MemoryClassLoader extends ClassLoader {155156private final JavaMemoryFileObject jfo;157158public MemoryClassLoader(JavaMemoryFileObject jfo) {159this.jfo = jfo;160}161162public Class findClass(String name) {163byte[] b = jfo.getByteCode();164return defineClass(name, b, 0, b.length);165}166}167168class MemoryFileManager extends ForwardingJavaFileManager<JavaFileManager> {169170private final JavaFileObject jfo;171172public MemoryFileManager(StandardJavaFileManager jfm, JavaFileObject jfo) {173super(jfm);174this.jfo = jfo;175}176177@Override178public FileObject getFileForInput(Location location, String packageName,179String relativeName) throws IOException {180return jfo;181}182183@Override184public JavaFileObject getJavaFileForOutput(Location location, String qualifiedName,185Kind kind, FileObject outputFile) throws IOException {186return jfo;187}188189}190191192