changeset 126:d1accd77974e

Make the main JMH thread to set the InfraControl.isDone flag. This spares additional thread per benchmark.
author shade
date Wed, 21 Aug 2013 13:04:36 +0400
parents 40356c1dda20
children 9e18c79cb757
files jmh-core/src/main/java/org/openjdk/jmh/logic/InfraControl.java jmh-core/src/main/java/org/openjdk/jmh/runner/LoopMicroBenchmarkHandler.java jmh-core/src/main/java/org/openjdk/jmh/runner/parameters/TimeValue.java
diffstat 3 files changed, 13 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/logic/InfraControl.java	Wed Aug 21 12:47:08 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/logic/InfraControl.java	Wed Aug 21 13:04:36 2013 +0400
@@ -41,31 +41,10 @@
  */
 public class InfraControl extends InfraControlL4 {
 
-    /** Timers */
-    private static final ScheduledExecutorService timers = Executors.newScheduledThreadPool(1, new ThreadFactory() {
-        @Override
-        public Thread newThread(Runnable r) {
-            Thread t = new Thread(r);
-            t.setPriority(Thread.MAX_PRIORITY);
-            t.setDaemon(true);
-            t.setName("LoopTimer");
-            return t;
-        }
-    });
-
     public InfraControl(int threads, boolean syncIterations, TimeValue loopTime, CountDownLatch preSetup, CountDownLatch preTearDown, boolean lastIteration, TimeUnit timeUnit) {
         super(threads, syncIterations, loopTime, preSetup, preTearDown, lastIteration, timeUnit);
     }
 
-    public void enableTimer() {
-        timers.schedule(new Runnable() {
-            @Override
-            public void run() {
-                isDone = true;
-            }
-        }, duration, TimeUnit.NANOSECONDS);
-    }
-
     /**
      * returns requested loop duration in milliseconds.
      * the primary purpose of the method - integration tests.
@@ -116,7 +95,7 @@
     }
 }
 
-class InfraControl_L1 {
+class InfraControlL1 {
     public int p01, p02, p03, p04, p05, p06, p07, p08;
     public int p11, p12, p13, p14, p15, p16, p17, p18;
     public int p21, p22, p23, p24, p25, p26, p27, p28;
@@ -126,7 +105,7 @@
 /**
  * @see BlackHole for rationale
  */
-class InfraControlL2 extends InfraControl_L1 {
+class InfraControlL2 extends InfraControlL1 {
     /* Flag for if we are done or not.
      * This is specifically the public field, so to spare one virtual call.
      */
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/LoopMicroBenchmarkHandler.java	Wed Aug 21 12:47:08 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/LoopMicroBenchmarkHandler.java	Wed Aug 21 13:04:36 2013 +0400
@@ -102,8 +102,13 @@
         // profilers start when iteration starts
         startProfilers();
 
-        // enable the timers
-        control.enableTimer();
+        // wait for the iteration time to expire, then set the termination flag
+        try {
+            runtime.sleep();
+        } catch (InterruptedException e) {
+            // regardless...
+        }
+        control.isDone = true;
 
         // wait for all workers to complete run and ready to proceed
         try {
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/parameters/TimeValue.java	Wed Aug 21 12:47:08 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/parameters/TimeValue.java	Wed Aug 21 13:04:36 2013 +0400
@@ -135,4 +135,8 @@
     public long convertTo(TimeUnit unit) {
         return unit.convert(time, timeUnit);
     }
+
+    public void sleep() throws InterruptedException {
+        timeUnit.sleep(time);
+    }
 }