8066798: [TEST] Make java/lang/invoke/LFCaching tests use lib/testlibrary/jdk/testlibrary/TimeLimitedRunner.java to define their number of iterations
authorkshefov
Thu, 11 Dec 2014 15:10:35 +0300
changeset 27958 51c87937ff0c
parent 27957 24b4e6082f19
child 27959 701e99e828ca
8066798: [TEST] Make java/lang/invoke/LFCaching tests use lib/testlibrary/jdk/testlibrary/TimeLimitedRunner.java to define their number of iterations Reviewed-by: iignatyev, vlivanov
jdk/test/java/lang/invoke/LFCaching/LFGarbageCollectedTest.java
jdk/test/java/lang/invoke/LFCaching/LambdaFormTestCase.java
--- a/jdk/test/java/lang/invoke/LFCaching/LFGarbageCollectedTest.java	Thu Dec 11 15:23:02 2014 +0800
+++ b/jdk/test/java/lang/invoke/LFCaching/LFGarbageCollectedTest.java	Thu Dec 11 15:10:35 2014 +0300
@@ -26,7 +26,6 @@
  * @bug 8046703
  * @summary Test verifies that lambda forms are garbage collected
  * @author kshefov
- * @ignore 8057020
  * @library /lib/testlibrary/jsr292 /lib/testlibrary
  * @build TestMethods
  * @build LambdaFormTestCase
--- a/jdk/test/java/lang/invoke/LFCaching/LambdaFormTestCase.java	Thu Dec 11 15:23:02 2014 +0800
+++ b/jdk/test/java/lang/invoke/LFCaching/LambdaFormTestCase.java	Thu Dec 11 15:10:35 2014 +0300
@@ -31,6 +31,7 @@
 import java.util.List;
 import java.util.function.Function;
 import jdk.testlibrary.Utils;
+import jdk.testlibrary.TimeLimitedRunner;
 
 /**
  * Lambda forms caching test case class. Contains all necessary test routines to
@@ -45,7 +46,7 @@
     private final static String INTERNAL_FORM_METHOD_NAME = "internalForm";
     private static final double ITERATIONS_TO_CODE_CACHE_SIZE_RATIO
             = 45 / (128.0 * 1024 * 1024);
-    private static final long TIMEOUT = Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT);
+    private static final long TIMEOUT = Helper.IS_THOROUGH ? 0L : (long) (Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT) * 0.9);
 
     /**
      * Reflection link to {@code j.l.i.MethodHandle.internalForm} method. It is
@@ -68,13 +69,98 @@
         }
 
         gcInfo = ManagementFactory.getGarbageCollectorMXBeans();
-        if (gcInfo.size() == 0)  {
+        if (gcInfo.size() == 0) {
             throw new Error("No GarbageCollectorMXBeans found.");
         }
     }
 
     private final TestMethods testMethod;
     private long gcCountAtStart;
+
+    private static class TestRun {
+
+        final Function<TestMethods, LambdaFormTestCase> ctor;
+        final Collection<TestMethods> testMethods;
+        final long totalIterations;
+        long doneIterations;
+        long testCounter;
+        long failCounter;
+        boolean passed;
+
+        TestRun(Function<TestMethods, LambdaFormTestCase> ctor, Collection<TestMethods> testMethods) {
+            this.ctor = ctor;
+            this.testMethods = testMethods;
+            long testCaseNum = testMethods.size();
+            long iterations = Math.max(1, Helper.TEST_LIMIT / testCaseNum);
+            System.out.printf("Number of iterations according to -DtestLimit is %d (%d cases)%n",
+                    iterations, iterations * testCaseNum);
+            HotSpotDiagnosticMXBean hsDiagBean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
+            long codeCacheSize = Long.parseLong(
+                    hsDiagBean.getVMOption("ReservedCodeCacheSize").getValue());
+            System.out.printf("Code cache size is %d bytes%n", codeCacheSize);
+            long iterationsByCodeCacheSize = (long) (codeCacheSize
+                    * ITERATIONS_TO_CODE_CACHE_SIZE_RATIO);
+            long nonProfiledCodeCacheSize = Long.parseLong(
+                    hsDiagBean.getVMOption("NonProfiledCodeHeapSize").getValue());
+            System.out.printf("Non-profiled code cache size is %d bytes%n", nonProfiledCodeCacheSize);
+            long iterationsByNonProfiledCodeCacheSize = (long) (nonProfiledCodeCacheSize
+                    * ITERATIONS_TO_CODE_CACHE_SIZE_RATIO);
+            System.out.printf("Number of iterations limited by code cache size is %d (%d cases)%n",
+                    iterationsByCodeCacheSize, iterationsByCodeCacheSize * testCaseNum);
+            System.out.printf("Number of iterations limited by non-profiled code cache size is %d (%d cases)%n",
+                    iterationsByNonProfiledCodeCacheSize, iterationsByNonProfiledCodeCacheSize * testCaseNum);
+            iterations = Math.min(iterationsByCodeCacheSize,
+                    Math.min(iterations, iterationsByNonProfiledCodeCacheSize));
+            if (iterations == 0) {
+                System.out.println("Warning: code cache size is too small to provide at"
+                        + " least one iteration! Test will try to do one iteration.");
+                iterations = 1;
+            }
+            System.out.printf("Number of iterations is set to %d (%d cases)%n",
+                    iterations, iterations * testCaseNum);
+            System.out.flush();
+            totalIterations = iterations;
+            doneIterations = 0L;
+            testCounter = 0L;
+            failCounter = 0L;
+            passed = true;
+        }
+
+        Boolean doIteration() {
+            if (doneIterations >= totalIterations) {
+                return false;
+            }
+            System.err.println(String.format("Iteration %d:", doneIterations));
+            for (TestMethods testMethod : testMethods) {
+                LambdaFormTestCase testCase = ctor.apply(testMethod);
+                try {
+                    System.err.printf("Tested LF caching feature with MethodHandles.%s method.%n",
+                            testCase.getTestMethod().name);
+                    testCase.doTest();
+                    System.err.println("PASSED");
+                } catch (Throwable t) {
+                    t.printStackTrace();
+                    System.err.printf("FAILED. Caused by %s%n", t.getMessage());
+                    passed = false;
+                    failCounter++;
+                }
+                    testCounter++;
+                }
+            doneIterations++;
+            return true;
+        }
+
+        void checkPassed() {
+            if (!passed) {
+                throw new Error(String.format("%d of %d test cases FAILED! %n"
+                        + "Rerun the test with the same \"-Dseed=\" option as in the log file!",
+                        failCounter, testCounter));
+            } else {
+                System.err.printf("All %d test cases PASSED!%n", testCounter);
+            }
+        }
+    }
+
     /**
      * Test case constructor. Generates test cases with random method types for
      * given methods form {@code j.l.i.MethodHandles} class.
@@ -108,71 +194,15 @@
      * @param testMethods list of test methods
      */
     public static void runTests(Function<TestMethods, LambdaFormTestCase> ctor, Collection<TestMethods> testMethods) {
-        boolean passed = true;
-        int testCounter = 0;
-        int failCounter = 0;
-        long testCaseNum = testMethods.size();
-        long iterations = Math.max(1, Helper.TEST_LIMIT / testCaseNum);
-        System.out.printf("Number of iterations according to -DtestLimit is %d (%d cases)%n",
-                iterations, iterations * testCaseNum);
-        HotSpotDiagnosticMXBean hsDiagBean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
-        long codeCacheSize = Long.parseLong(
-                hsDiagBean.getVMOption("ReservedCodeCacheSize").getValue());
-        System.out.printf("Code cache size is %d bytes%n", codeCacheSize);
-        long iterationsByCodeCacheSize = (long) (codeCacheSize
-                * ITERATIONS_TO_CODE_CACHE_SIZE_RATIO);
-        long nonProfiledCodeCacheSize = Long.parseLong(
-                hsDiagBean.getVMOption("NonProfiledCodeHeapSize").getValue());
-        System.out.printf("Non-profiled code cache size is %d bytes%n", nonProfiledCodeCacheSize);
-        long iterationsByNonProfiledCodeCacheSize = (long) (nonProfiledCodeCacheSize
-                * ITERATIONS_TO_CODE_CACHE_SIZE_RATIO);
-        System.out.printf("Number of iterations limited by code cache size is %d (%d cases)%n",
-                iterationsByCodeCacheSize, iterationsByCodeCacheSize * testCaseNum);
-        System.out.printf("Number of iterations limited by non-profiled code cache size is %d (%d cases)%n",
-                iterationsByNonProfiledCodeCacheSize, iterationsByNonProfiledCodeCacheSize * testCaseNum);
-        iterations = Math.min(iterationsByCodeCacheSize,
-                Math.min(iterations, iterationsByNonProfiledCodeCacheSize));
-        if (iterations == 0) {
-            System.out.println("Warning: code cache size is too small to provide at"
-                    + " least one iteration! Test will try to do one iteration.");
-            iterations = 1;
+        LambdaFormTestCase.TestRun run =
+                new LambdaFormTestCase.TestRun(ctor, testMethods);
+        TimeLimitedRunner runner = new TimeLimitedRunner(TIMEOUT, 4.0d, run::doIteration);
+        try {
+            runner.call();
+        } catch (Exception ex) {
+            System.err.println("FAILED");
+            throw new Error("Unexpected error!", ex);
         }
-        System.out.printf("Number of iterations is set to %d (%d cases)%n",
-                iterations, iterations * testCaseNum);
-        System.out.flush();
-        long startTime = System.currentTimeMillis();
-        for (long i = 0; i < iterations; i++) {
-            System.err.println(String.format("Iteration %d:", i));
-            for (TestMethods testMethod : testMethods) {
-                LambdaFormTestCase testCase = ctor.apply(testMethod);
-                try {
-                    System.err.printf("Tested LF caching feature with MethodHandles.%s method.%n",
-                            testCase.getTestMethod().name);
-                    testCase.doTest();
-                    System.err.println("PASSED");
-                } catch (Throwable t) {
-                    t.printStackTrace();
-                    System.err.println("FAILED");
-                    passed = false;
-                    failCounter++;
-                }
-                testCounter++;
-            }
-            long passedTime = System.currentTimeMillis() - startTime;
-            long avgIterTime = passedTime / (i + 1);
-            long remainTime = TIMEOUT - passedTime;
-            if (avgIterTime > 2 * remainTime) {
-                System.err.printf("Stopping iterations because of lack of time.%n"
-                        + "Increase timeout factor for more iterations.%n");
-                break;
-            }
-        }
-        if (!passed) {
-            throw new Error(String.format("%d of %d test cases FAILED! %n"
-                    + "Rerun the test with the same \"-Dseed=\" option as in the log file!",
-                    failCounter, testCounter));
-        } else {
-            System.err.println(String.format("All %d test cases PASSED!", testCounter));
-        }
+        run.checkPassed();
     }
 }