test/runtime/6925573/SortMethodsTest.java

changeset 1853
77261afdc5f2
child 1907
c18cbe5936b8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/runtime/6925573/SortMethodsTest.java	Tue May 04 15:12:08 2010 -0400
     1.3 @@ -0,0 +1,190 @@
     1.4 +/*
     1.5 + * Copyright 2008-2010 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +import java.io.ByteArrayOutputStream;
    1.29 +import java.io.IOException;
    1.30 +import java.io.OutputStream;
    1.31 +import java.io.PrintWriter;
    1.32 +import java.io.StringWriter;
    1.33 +
    1.34 +import java.lang.reflect.Method;
    1.35 +import java.net.URI;
    1.36 +import java.util.Arrays;
    1.37 +import java.util.Vector;
    1.38 +
    1.39 +import javax.tools.Diagnostic;
    1.40 +import javax.tools.DiagnosticCollector;
    1.41 +import javax.tools.FileObject;
    1.42 +import javax.tools.ForwardingJavaFileManager;
    1.43 +import javax.tools.JavaCompiler;
    1.44 +import javax.tools.JavaCompiler.CompilationTask;
    1.45 +import javax.tools.JavaFileManager;
    1.46 +import javax.tools.JavaFileObject;
    1.47 +import javax.tools.JavaFileObject.Kind;
    1.48 +import javax.tools.SimpleJavaFileObject;
    1.49 +import javax.tools.StandardJavaFileManager;
    1.50 +import javax.tools.ToolProvider;
    1.51 +
    1.52 +/*
    1.53 + * @test SortMethodsTest
    1.54 + * @bug 6925573
    1.55 + * @summary verify that class loading does not need quadratic time with regard to the number of class
    1.56 +methods.
    1.57 + * @run main SortMethodsTest
    1.58 + * @author volker.simonis@gmail.com
    1.59 +*/
    1.60 +
    1.61 +public class SortMethodsTest {
    1.62 +
    1.63 +  static String createClass(String name, int nrOfMethods) {
    1.64 +    StringWriter sw = new StringWriter();
    1.65 +    PrintWriter pw = new PrintWriter(sw);
    1.66 +    pw.println("public class " + name + "{");
    1.67 +    for (int i = 0; i < nrOfMethods; i++) {
    1.68 +      pw.println("  public void m" + i + "() {}");
    1.69 +    }
    1.70 +    pw.println("  public static String sayHello() {");
    1.71 +    pw.println("    return \"Hello from class \" + " + name +
    1.72 +               ".class.getName() + \" with \" + " + name +
    1.73 +               ".class.getDeclaredMethods().length + \" methods\";");
    1.74 +    pw.println("  }");
    1.75 +    pw.println("}");
    1.76 +    pw.close();
    1.77 +    return sw.toString();
    1.78 +  }
    1.79 +
    1.80 +  public static void main(String args[]) {
    1.81 +
    1.82 +    JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
    1.83 +    DiagnosticCollector<JavaFileObject> diags = new DiagnosticCollector<JavaFileObject>();
    1.84 +    final String cName = new String("ManyMethodsClass");
    1.85 +    Vector<Long> results = new Vector<Long>();
    1.86 +
    1.87 +    for (int i = 6; i < 600000; i*=10) {
    1.88 +      String klass =  createClass(cName, i);
    1.89 +      JavaMemoryFileObject file = new JavaMemoryFileObject(cName, klass);
    1.90 +      MemoryFileManager mfm = new MemoryFileManager(comp.getStandardFileManager(diags, null, null), file);
    1.91 +      CompilationTask task = comp.getTask(null, mfm, diags, null, null, Arrays.asList(file));
    1.92 +
    1.93 +      if (task.call()) {
    1.94 +        try {
    1.95 +          MemoryClassLoader mcl = new MemoryClassLoader(file);
    1.96 +          long start = System.nanoTime();
    1.97 +          Class<? extends Object> c = Class.forName(cName, true, mcl);
    1.98 +          long end = System.nanoTime();
    1.99 +          results.add(end - start);
   1.100 +          Method m = c.getDeclaredMethod("sayHello", new Class[0]);
   1.101 +          String ret = (String)m.invoke(null, new Object[0]);
   1.102 +          System.out.println(ret + " (loaded and resloved in " + (end - start) + "ns)");
   1.103 +        } catch (Exception e) {
   1.104 +          System.err.println(e);
   1.105 +        }
   1.106 +      }
   1.107 +      else {
   1.108 +        System.out.println(klass);
   1.109 +        System.out.println();
   1.110 +        for (Diagnostic diag : diags.getDiagnostics()) {
   1.111 +          System.out.println(diag.getCode() + "\n" + diag.getKind() + "\n" + diag.getPosition());
   1.112 +          System.out.println(diag.getSource() + "\n" + diag.getMessage(null));
   1.113 +        }
   1.114 +      }
   1.115 +    }
   1.116 +
   1.117 +    long lastRatio = 0;
   1.118 +    for (int i = 2; i < results.size(); i++) {
   1.119 +      long normalized1 = Math.max(results.get(i-1) - results.get(0), 1);
   1.120 +      long normalized2 = Math.max(results.get(i) - results.get(0), 1);
   1.121 +      long ratio = normalized2/normalized1;
   1.122 +      lastRatio = ratio;
   1.123 +      System.out.println("10 x more methods requires " + ratio + " x more time");
   1.124 +    }
   1.125 +    // The following is just vague estimation but seems to work on current x86_64 and sparcv9 machines
   1.126 +    if (lastRatio > 80) {
   1.127 +      throw new RuntimeException("ATTENTION: it seems that class loading needs quadratic time with regard to the number of class methods!!!");
   1.128 +    }
   1.129 +  }
   1.130 +}
   1.131 +
   1.132 +class JavaMemoryFileObject extends SimpleJavaFileObject {
   1.133 +
   1.134 +  private final String code;
   1.135 +  private ByteArrayOutputStream byteCode;
   1.136 +
   1.137 +  JavaMemoryFileObject(String name, String code) {
   1.138 +    super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE);
   1.139 +    this.code = code;
   1.140 +  }
   1.141 +
   1.142 +  @Override
   1.143 +  public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.144 +    return code;
   1.145 +  }
   1.146 +
   1.147 +  @Override
   1.148 +  public OutputStream openOutputStream() {
   1.149 +    byteCode = new ByteArrayOutputStream();
   1.150 +    return byteCode;
   1.151 +  }
   1.152 +
   1.153 +  byte[] getByteCode() {
   1.154 +    return byteCode.toByteArray();
   1.155 +   }
   1.156 +}
   1.157 +
   1.158 +class MemoryClassLoader extends ClassLoader {
   1.159 +
   1.160 +  private final JavaMemoryFileObject jfo;
   1.161 +
   1.162 +  public MemoryClassLoader(JavaMemoryFileObject jfo) {
   1.163 +    this.jfo = jfo;
   1.164 +  }
   1.165 +
   1.166 +  public Class findClass(String name) {
   1.167 +    byte[] b = jfo.getByteCode();
   1.168 +    return defineClass(name, b, 0, b.length);
   1.169 +  }
   1.170 +}
   1.171 +
   1.172 +class MemoryFileManager extends ForwardingJavaFileManager<JavaFileManager> {
   1.173 +
   1.174 +  private final JavaFileObject jfo;
   1.175 +
   1.176 +  public MemoryFileManager(StandardJavaFileManager jfm, JavaFileObject jfo) {
   1.177 +    super(jfm);
   1.178 +    this.jfo = jfo;
   1.179 +  }
   1.180 +
   1.181 +  @Override
   1.182 +  public FileObject getFileForInput(Location location, String packageName,
   1.183 +                                    String relativeName) throws IOException {
   1.184 +    return jfo;
   1.185 +  }
   1.186 +
   1.187 +  @Override
   1.188 +  public JavaFileObject getJavaFileForOutput(Location location, String qualifiedName,
   1.189 +                                             Kind kind, FileObject outputFile) throws IOException {
   1.190 +    return jfo;
   1.191 +  }
   1.192 +
   1.193 +}

mercurial