test/runtime/6925573/SortMethodsTest.java

Print this page
rev 6248 : [mq]: 6959423


  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 




  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  * @test SortMethodsTest
  51  * @bug 6925573
  52  * @summary verify that class loading does not need quadratic time with regard to the number of class
  53 methods.
  54  * @run main SortMethodsTest
  55  * @author volker.simonis@gmail.com
  56 */
  57 
  58 public class SortMethodsTest {
  59 
  60   static String createClass(String name, int nrOfMethods) {
  61     StringWriter sw = new StringWriter();
  62     PrintWriter pw = new PrintWriter(sw);
  63     pw.println("public class " + name + "{");
  64     for (int i = 0; i < nrOfMethods; i++) {
  65       pw.println("  public void m" + i + "() {}");
  66     }
  67     pw.println("  public static String sayHello() {");
  68     pw.println("    return \"Hello from class \" + " + name +
  69                ".class.getName() + \" with \" + " + name +
  70                ".class.getDeclaredMethods().length + \" methods\";");
  71     pw.println("  }");
  72     pw.println("}");
  73     pw.close();
  74     return sw.toString();
  75   }
  76 
  77   public static void main(String args[]) {
  78 
  79     JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
  80     DiagnosticCollector<JavaFileObject> diags = new DiagnosticCollector<JavaFileObject>();
  81     final String cName = new String("ManyMethodsClass");
  82     Vector<Long> results = new Vector<Long>();
  83     
  84     for (int i = 6; i < 60000; i*=10) {
  85       String klass =  createClass(cName, i);
  86       JavaMemoryFileObject file = new JavaMemoryFileObject(cName, klass);
  87       MemoryFileManager mfm = new MemoryFileManager(comp.getStandardFileManager(diags, null, null), file);
  88       CompilationTask task = comp.getTask(null, mfm, diags, null, null, Arrays.asList(file));
  89 
  90       if (task.call()) {
  91         try {
  92           MemoryClassLoader mcl = new MemoryClassLoader(file);
  93           long start = System.nanoTime();
  94           Class<? extends Object> c = Class.forName(cName, true, mcl);
  95           long end = System.nanoTime();
  96           results.add(end - start);
  97           Method m = c.getDeclaredMethod("sayHello", new Class[0]);
  98           String ret = (String)m.invoke(null, new Object[0]);
  99           System.out.println(ret + " (loaded and resloved in " + (end - start) + "ns)");
 100         } catch (Exception e) {
 101           System.err.println(e);
 102         }
 103       }
 104       else {
 105         System.out.println(klass);
 106         System.out.println();
 107         for (Diagnostic diag : diags.getDiagnostics()) {
 108           System.out.println(diag.getCode() + "\n" + diag.getKind() + "\n" + diag.getPosition());
 109           System.out.println(diag.getSource() + "\n" + diag.getMessage(null));
 110         }
 111       }
 112     }
 113 
 114     long lastRatio = 0;
 115     for (int i = 2; i < results.size(); i++) {
 116       long normalized1 = Math.max(results.get(i-1) - results.get(0), 1);
 117       long normalized2 = Math.max(results.get(i) - results.get(0), 1);
 118       long ratio = normalized2/normalized1;
 119       lastRatio = ratio;
 120       System.out.println("10 x more methods requires " + ratio + " x more time");
 121     }
 122     // The following is just vague estimation but seems to work on current x86_64 and sparcv9 machines
 123     if (lastRatio > 60) {
 124       throw new RuntimeException("ATTENTION: it seems that class loading needs quadratic time with regard to the number of class methods!!!");
 125     }
 126   }
 127 }
 128 
 129 class JavaMemoryFileObject extends SimpleJavaFileObject {
 130 
 131   private final String code;
 132   private ByteArrayOutputStream byteCode;
 133 
 134   JavaMemoryFileObject(String name, String code) {
 135     super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE);
 136     this.code = code;
 137   }
 138 
 139   @Override
 140   public CharSequence getCharContent(boolean ignoreEncodingErrors) {
 141     return code;
 142   }
 143