test/java/lang/invoke/LFCaching/LambdaFormTestCase.java

Print this page




   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 import com.oracle.testlibrary.jsr292.Helper;



  25 import java.lang.reflect.Method;
  26 import java.util.Collection;
  27 import java.util.function.Function;

  28 
  29 /**
  30  * Lambda forms caching test case class. Contains all necessary test routines to
  31  * test lambda forms caching in method handles returned by methods of
  32  * MethodHandles class.
  33  *
  34  * @author kshefov
  35  */
  36 public abstract class LambdaFormTestCase {
  37 
  38     private final static String METHOD_HANDLE_CLASS_NAME = "java.lang.invoke.MethodHandle";
  39     private final static String INTERNAL_FORM_METHOD_NAME = "internalForm";




  40 
  41     /**
  42      * Reflection link to {@code j.l.i.MethodHandle.internalForm} method. It is
  43      * used to get a lambda form from a method handle.
  44      */
  45     protected final static Method INTERNAL_FORM;
  46 
  47     static {
  48         try {
  49             Class mhClass = Class.forName(METHOD_HANDLE_CLASS_NAME);
  50             INTERNAL_FORM = mhClass.getDeclaredMethod(INTERNAL_FORM_METHOD_NAME);
  51             INTERNAL_FORM.setAccessible(true);
  52         } catch (Exception ex) {
  53             throw new Error("Unexpected exception: ", ex);
  54         }
  55     }
  56 
  57     private final TestMethods testMethod;
  58 
  59     /**


  70     public TestMethods getTestMethod() {
  71         return testMethod;
  72     }
  73 
  74     /**
  75      * Routine that executes a test case.
  76      */
  77     public abstract void doTest();
  78 
  79     /**
  80      * Runs a number of test cases defined by the size of testCases list.
  81      *
  82      * @param ctor constructor of LambdaFormCachingTest or its child classes
  83      * object.
  84      * @param testMethods list of test methods
  85      */
  86     public static void runTests(Function<TestMethods, LambdaFormTestCase> ctor, Collection<TestMethods> testMethods) {
  87         boolean passed = true;
  88         int testCounter = 0;
  89         int failCounter = 0;
  90         long iterations = Math.max(1, Helper.TEST_LIMIT / testMethods.size());
























  91         for (long i = 0; i < iterations; i++) {
  92             System.err.println(String.format("Iteration %d:", i));
  93             for (TestMethods testMethod : testMethods) {
  94                 LambdaFormTestCase testCase = ctor.apply(testMethod);
  95                 try {
  96                     System.err.printf("Tested LF caching feature with MethodHandles.%s method.%n",
  97                             testCase.getTestMethod().name);
  98                     testCase.doTest();
  99                     System.err.println("PASSED");
 100                 } catch (Throwable t) {
 101                     t.printStackTrace();
 102                     System.err.println("FAILED");
 103                     passed = false;
 104                     failCounter++;
 105                 }
 106                 testCounter++;
 107             }
 108         }
 109         if (!passed) {
 110             throw new Error(String.format("%d of %d test cases FAILED! %n"
 111                     + "Rerun the test with the same \"-Dseed=\" option as in the log file!",
 112                     failCounter, testCounter));
 113         } else {
 114             System.err.println(String.format("All %d test cases PASSED!", testCounter));
 115         }
 116     }
 117 }



   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 import com.oracle.testlibrary.jsr292.Helper;
  25 import com.sun.management.HotSpotDiagnosticMXBean;
  26 import java.io.IOException;
  27 import java.lang.management.ManagementFactory;
  28 import java.lang.reflect.Method;
  29 import java.util.Collection;
  30 import java.util.function.Function;
  31 import javax.management.MBeanServer;
  32 
  33 /**
  34  * Lambda forms caching test case class. Contains all necessary test routines to
  35  * test lambda forms caching in method handles returned by methods of
  36  * MethodHandles class.
  37  *
  38  * @author kshefov
  39  */
  40 public abstract class LambdaFormTestCase {
  41 
  42     private final static String METHOD_HANDLE_CLASS_NAME = "java.lang.invoke.MethodHandle";
  43     private final static String INTERNAL_FORM_METHOD_NAME = "internalForm";
  44     private static final String HOTSPOT_BEAN_NAME
  45             = "com.sun.management:type=HotSpotDiagnostic";
  46     private static final double ITERATIONS_TO_CODE_CACHE_SIZE_RATIO
  47             = 45 / (128.0 * 1024 * 1024);
  48 
  49     /**
  50      * Reflection link to {@code j.l.i.MethodHandle.internalForm} method. It is
  51      * used to get a lambda form from a method handle.
  52      */
  53     protected final static Method INTERNAL_FORM;
  54 
  55     static {
  56         try {
  57             Class mhClass = Class.forName(METHOD_HANDLE_CLASS_NAME);
  58             INTERNAL_FORM = mhClass.getDeclaredMethod(INTERNAL_FORM_METHOD_NAME);
  59             INTERNAL_FORM.setAccessible(true);
  60         } catch (Exception ex) {
  61             throw new Error("Unexpected exception: ", ex);
  62         }
  63     }
  64 
  65     private final TestMethods testMethod;
  66 
  67     /**


  78     public TestMethods getTestMethod() {
  79         return testMethod;
  80     }
  81 
  82     /**
  83      * Routine that executes a test case.
  84      */
  85     public abstract void doTest();
  86 
  87     /**
  88      * Runs a number of test cases defined by the size of testCases list.
  89      *
  90      * @param ctor constructor of LambdaFormCachingTest or its child classes
  91      * object.
  92      * @param testMethods list of test methods
  93      */
  94     public static void runTests(Function<TestMethods, LambdaFormTestCase> ctor, Collection<TestMethods> testMethods) {
  95         boolean passed = true;
  96         int testCounter = 0;
  97         int failCounter = 0;
  98         long testCaseNum = testMethods.size();
  99         long iterations = Math.max(1, Helper.TEST_LIMIT / testCaseNum);
 100         System.out.printf("Number of iterations according to -DtestLimit is %d (%d cases)%n",
 101                 iterations, iterations * testCaseNum);
 102         MBeanServer server = ManagementFactory.getPlatformMBeanServer();
 103         HotSpotDiagnosticMXBean hsDiagBean;
 104         try {
 105             hsDiagBean = ManagementFactory.newPlatformMXBeanProxy(server,
 106                     HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
 107         } catch (IOException ex) {
 108             throw new Error("TESTBUG: ", ex);
 109         }
 110         long codeCacheSize = Long.parseLong(
 111                 hsDiagBean.getVMOption("ReservedCodeCacheSize").getValue());
 112         System.out.printf("Code Cache Size is %d bytes%n", codeCacheSize);
 113         long iterationsByCodeCacheSize = (long) (codeCacheSize
 114                 * ITERATIONS_TO_CODE_CACHE_SIZE_RATIO);
 115         System.out.printf("Number of iterations limited by code cache size is %d (%d cases)%n",
 116                 iterationsByCodeCacheSize, iterationsByCodeCacheSize * testCaseNum);
 117         if (iterations > iterationsByCodeCacheSize) {
 118             iterations = iterationsByCodeCacheSize;
 119         }
 120         System.out.printf("Number of iterations is set to %d (%d cases)%n",
 121                 iterations, iterations * testCaseNum);
 122         System.out.flush();
 123         for (long i = 0; i < iterations; i++) {
 124             System.err.println(String.format("Iteration %d:", i));
 125             for (TestMethods testMethod : testMethods) {
 126                 LambdaFormTestCase testCase = ctor.apply(testMethod);
 127                 try {
 128                     System.err.printf("Tested LF caching feature with MethodHandles.%s method.%n",
 129                             testCase.getTestMethod().name);
 130                     testCase.doTest();
 131                     System.err.println("PASSED");
 132                 } catch (Throwable t) {
 133                     t.printStackTrace();
 134                     System.err.println("FAILED");
 135                     passed = false;
 136                     failCounter++;
 137                 }
 138                 testCounter++;
 139             }
 140         }
 141         if (!passed) {
 142             throw new Error(String.format("%d of %d test cases FAILED! %n"
 143                     + "Rerun the test with the same \"-Dseed=\" option as in the log file!",
 144                     failCounter, testCounter));
 145         } else {
 146             System.err.println(String.format("All %d test cases PASSED!", testCounter));
 147         }
 148     }
 149 }
 150