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

Print this page




  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.lang.management.ManagementFactory;
  27 import java.lang.reflect.Method;
  28 import java.util.Collection;
  29 import java.util.function.Function;

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

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


  92         int testCounter = 0;
  93         int failCounter = 0;
  94         long testCaseNum = testMethods.size();
  95         long iterations = Math.max(1, Helper.TEST_LIMIT / testCaseNum);
  96         System.out.printf("Number of iterations according to -DtestLimit is %d (%d cases)%n",
  97                 iterations, iterations * testCaseNum);
  98         HotSpotDiagnosticMXBean hsDiagBean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
  99         long codeCacheSize = Long.parseLong(
 100                 hsDiagBean.getVMOption("ReservedCodeCacheSize").getValue());
 101         System.out.printf("Code Cache Size is %d bytes%n", codeCacheSize);
 102         long iterationsByCodeCacheSize = (long) (codeCacheSize
 103                 * ITERATIONS_TO_CODE_CACHE_SIZE_RATIO);
 104         System.out.printf("Number of iterations limited by code cache size is %d (%d cases)%n",
 105                 iterationsByCodeCacheSize, iterationsByCodeCacheSize * testCaseNum);
 106         if (iterations > iterationsByCodeCacheSize) {
 107             iterations = iterationsByCodeCacheSize;
 108         }
 109         System.out.printf("Number of iterations is set to %d (%d cases)%n",
 110                 iterations, iterations * testCaseNum);
 111         System.out.flush();


 112         for (long i = 0; i < iterations; i++) {

 113             System.err.println(String.format("Iteration %d:", i));
 114             for (TestMethods testMethod : testMethods) {
 115                 LambdaFormTestCase testCase = ctor.apply(testMethod);
 116                 try {
 117                     System.err.printf("Tested LF caching feature with MethodHandles.%s method.%n",
 118                             testCase.getTestMethod().name);
 119                     testCase.doTest();
 120                     System.err.println("PASSED");
 121                 } catch (Throwable t) {
 122                     t.printStackTrace();
 123                     System.err.println("FAILED");
 124                     passed = false;
 125                     failCounter++;
 126                 }
 127                 testCounter++;









 128             }
 129         }
 130         if (!passed) {
 131             throw new Error(String.format("%d of %d test cases FAILED! %n"
 132                     + "Rerun the test with the same \"-Dseed=\" option as in the log file!",
 133                     failCounter, testCounter));
 134         } else {
 135             System.err.println(String.format("All %d test cases PASSED!", testCounter));
 136         }
 137     }
 138 }


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


  94         int testCounter = 0;
  95         int failCounter = 0;
  96         long testCaseNum = testMethods.size();
  97         long iterations = Math.max(1, Helper.TEST_LIMIT / testCaseNum);
  98         System.out.printf("Number of iterations according to -DtestLimit is %d (%d cases)%n",
  99                 iterations, iterations * testCaseNum);
 100         HotSpotDiagnosticMXBean hsDiagBean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
 101         long codeCacheSize = Long.parseLong(
 102                 hsDiagBean.getVMOption("ReservedCodeCacheSize").getValue());
 103         System.out.printf("Code Cache Size is %d bytes%n", codeCacheSize);
 104         long iterationsByCodeCacheSize = (long) (codeCacheSize
 105                 * ITERATIONS_TO_CODE_CACHE_SIZE_RATIO);
 106         System.out.printf("Number of iterations limited by code cache size is %d (%d cases)%n",
 107                 iterationsByCodeCacheSize, iterationsByCodeCacheSize * testCaseNum);
 108         if (iterations > iterationsByCodeCacheSize) {
 109             iterations = iterationsByCodeCacheSize;
 110         }
 111         System.out.printf("Number of iterations is set to %d (%d cases)%n",
 112                 iterations, iterations * testCaseNum);
 113         System.out.flush();
 114         long maxIterTime = 0;
 115         long startTime = System.currentTimeMillis();
 116         for (long i = 0; i < iterations; i++) {
 117             long startIterTime = System.currentTimeMillis();
 118             System.err.println(String.format("Iteration %d:", i));
 119             for (TestMethods testMethod : testMethods) {
 120                 LambdaFormTestCase testCase = ctor.apply(testMethod);
 121                 try {
 122                     System.err.printf("Tested LF caching feature with MethodHandles.%s method.%n",
 123                             testCase.getTestMethod().name);
 124                     testCase.doTest();
 125                     System.err.println("PASSED");
 126                 } catch (Throwable t) {
 127                     t.printStackTrace();
 128                     System.err.println("FAILED");
 129                     passed = false;
 130                     failCounter++;
 131                 }
 132                 testCounter++;
 133             }
 134             long passedTime = System.currentTimeMillis() - startTime;
 135             long iterTime = System.currentTimeMillis() - startIterTime;
 136             maxIterTime = Math.max(maxIterTime, iterTime);
 137             long remainTime = TIMEOUT - passedTime;
 138             if (4 * maxIterTime > remainTime) {
 139                 System.err.printf("Stopping iterations because of lack of time.%n"
 140                         + "Increase timeout factor for more iterations.%n");
 141                 break;
 142             }
 143         }
 144         if (!passed) {
 145             throw new Error(String.format("%d of %d test cases FAILED! %n"
 146                     + "Rerun the test with the same \"-Dseed=\" option as in the log file!",
 147                     failCounter, testCounter));
 148         } else {
 149             System.err.println(String.format("All %d test cases PASSED!", testCounter));
 150         }
 151     }
 152 }