changeset 86:9e18920ec444

Introduce -tu (timeunit) command line option. The time units are now selectable at runtime!
author shade
date Mon, 10 Jun 2013 19:50:29 +0400
parents 005b4d1a4c80
children 81994ce1bd24
files jmh-core/src/main/java/org/openjdk/jmh/logic/Loop.java jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java jmh-core/src/main/java/org/openjdk/jmh/runner/BaseMicroBenchmarkHandler.java jmh-core/src/main/java/org/openjdk/jmh/runner/LoopMicroBenchmarkHandler.java jmh-core/src/main/java/org/openjdk/jmh/runner/ShotMicroBenchmarkHandler.java jmh-core/src/main/java/org/openjdk/jmh/runner/options/BaseOptions.java jmh-core/src/main/java/org/openjdk/jmh/runner/options/handlers/TimeValueOptionHandler.java
diffstat 7 files changed, 48 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/logic/Loop.java	Mon Jun 10 18:32:58 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/logic/Loop.java	Mon Jun 10 19:50:29 2013 +0400
@@ -67,11 +67,11 @@
      * @param loopTime How long we should loop
      */
     public Loop(TimeValue loopTime) {
-        this(1, loopTime, null, null, false, false);
+        this(1, loopTime, null, null, false, false, null);
     }
 
-    public Loop(int threads, TimeValue loopTime, CountDownLatch preSetup, CountDownLatch preTearDown, boolean lastIteration, boolean syncIterations) {
-        super(threads, loopTime, preSetup, preTearDown, lastIteration, syncIterations);
+    public Loop(int threads, TimeValue loopTime, CountDownLatch preSetup, CountDownLatch preTearDown, boolean lastIteration, boolean syncIterations, TimeUnit timeUnit) {
+        super(threads, loopTime, preSetup, preTearDown, lastIteration, syncIterations, timeUnit);
     }
 
 
@@ -292,8 +292,9 @@
     public final AtomicInteger warmupVisited, warmdownVisited;
     public volatile boolean warmupShouldWait, warmdownShouldWait;
 
+    public final TimeUnit timeUnit;
 
-    public LoopL2(int threads, TimeValue loopTime, CountDownLatch preSetup, CountDownLatch preTearDown, boolean lastIteration, boolean syncIterations) {
+    public LoopL2(int threads, TimeValue loopTime, CountDownLatch preSetup, CountDownLatch preTearDown, boolean lastIteration, boolean syncIterations, TimeUnit timeUnit) {
         this.threads = threads;
         this.preSetup = preSetup;
         this.preTearDown = preTearDown;
@@ -309,8 +310,8 @@
         }
 
         this.syncIterations = syncIterations;
+        this.timeUnit = timeUnit;
     }
-
 }
 
 class LoopL3 extends LoopL2 {
@@ -319,16 +320,16 @@
     public int e21, e22, e23, e24, e25, e26, e27, e28;
     public int e31, e32, e33, e34, e35, e36, e37, e38;
 
-    public LoopL3(int threads, TimeValue loopTime, CountDownLatch preSetup, CountDownLatch preTearDown, boolean lastIteration, boolean syncIterations) {
-        super(threads, loopTime, preSetup, preTearDown, lastIteration, syncIterations);
+    public LoopL3(int threads, TimeValue loopTime, CountDownLatch preSetup, CountDownLatch preTearDown, boolean lastIteration, boolean syncIterations, TimeUnit timeUnit) {
+        super(threads, loopTime, preSetup, preTearDown, lastIteration, syncIterations, timeUnit);
     }
 }
 
 class LoopL4 extends LoopL3 {
     public int marker;
 
-    public LoopL4(int threads, TimeValue loopTime, CountDownLatch preSetup, CountDownLatch preTearDown, boolean lastIteration, boolean syncIterations) {
-        super(threads, loopTime, preSetup, preTearDown, lastIteration, syncIterations);
+    public LoopL4(int threads, TimeValue loopTime, CountDownLatch preSetup, CountDownLatch preTearDown, boolean lastIteration, boolean syncIterations, TimeUnit timeUnit) {
+        super(threads, loopTime, preSetup, preTearDown, lastIteration, syncIterations, timeUnit);
     }
 }
 
--- a/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Mon Jun 10 18:32:58 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Mon Jun 10 19:50:29 2013 +0400
@@ -769,7 +769,7 @@
             // iteration prolog
             iterationEpilog(writer, 3, method, states);
 
-            writer.println(ident(3) + "return new OpsPerTimeUnit(\"" + method.getSimpleName() + "\", res.operations, res.time, TimeUnit." + timeUnit + ");");
+            writer.println(ident(3) + "return new OpsPerTimeUnit(\"" + method.getSimpleName() + "\", res.operations, res.time, (loop.timeUnit != null) ? loop.timeUnit : TimeUnit." + timeUnit + ");");
             writer.println(ident(2) + "} else");
         }
         writer.println(ident(3) + "throw new IllegalStateException(\"Harness failed to distribute threads among groups properly\");");
@@ -862,7 +862,7 @@
 
             iterationEpilog(writer, 3, method, states);
 
-            writer.println(ident(3) + "return new AverageTimePerOp(\"" + method.getSimpleName() + "\", res.operations, res.time, TimeUnit." + timeUnit + ");");
+            writer.println(ident(3) + "return new AverageTimePerOp(\"" + method.getSimpleName() + "\", res.operations, res.time, (loop.timeUnit != null) ? loop.timeUnit : TimeUnit." + timeUnit + ");");
             writer.println(ident(2) + "} else");
         }
         writer.println(ident(3) + "throw new IllegalStateException(\"Harness failed to distribute threads among groups properly\");");
@@ -1013,7 +1013,7 @@
             invocationEpilog(writer, 4, method, states, true);
 
             writer.println("        } while(!loop.isDone);");
-            writer.println("        return new SampleTimePerOp(\"" + method.getSimpleName() + "\", buffer, TimeUnit." + timeUnit + ");");
+            writer.println("        return new SampleTimePerOp(\"" + method.getSimpleName() + "\", buffer, (loop.timeUnit != null) ? loop.timeUnit : TimeUnit." + timeUnit + ");");
             writer.println("    }");
             writer.println();
         }
@@ -1049,7 +1049,7 @@
 
             iterationEpilog(writer, 3, method, states);
 
-            writer.println(ident(3) + "return new SingleShotTime(\"" + method.getSimpleName() + "\", (realTime > 0) ? realTime : (time2 - time1), TimeUnit." + timeUnit + ");");
+            writer.println(ident(3) + "return new SingleShotTime(\"" + method.getSimpleName() + "\", (realTime > 0) ? realTime : (time2 - time1), (loop.timeUnit != null) ? loop.timeUnit : TimeUnit." + timeUnit + ");");
             writer.println(ident(2) + "} else");
         }
         writer.println(ident(3) + "throw new IllegalStateException(\"Harness failed to distribute threads among groups properly\");");
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/BaseMicroBenchmarkHandler.java	Mon Jun 10 18:32:58 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/BaseMicroBenchmarkHandler.java	Mon Jun 10 19:50:29 2013 +0400
@@ -60,6 +60,7 @@
     protected final ThreadLocal<InstanceProvider> threadLocal;
 
     protected final OutputFormat format;
+    protected final TimeUnit timeUnit;
 
     private final List<Profiler> registeredProfilers;
 
@@ -74,6 +75,7 @@
             }
         };
         this.format = format;
+        this.timeUnit = options.getTimeUnit();
     }
 
     private static List<Profiler> createProfilers(BaseOptions options) {
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/LoopMicroBenchmarkHandler.java	Mon Jun 10 18:32:58 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/LoopMicroBenchmarkHandler.java	Mon Jun 10 19:50:29 2013 +0400
@@ -96,7 +96,7 @@
 
         BenchmarkTask[] runners = new BenchmarkTask[numThreads];
         for (int i = 0; i < runners.length; i++) {
-            runners[i] = new BenchmarkTask(threadLocal, new Loop(numThreads, runtime, preSetupBarrier, preTearDownBarrier, last, shouldSynchIterations));
+            runners[i] = new BenchmarkTask(threadLocal, new Loop(numThreads, runtime, preSetupBarrier, preTearDownBarrier, last, shouldSynchIterations, timeUnit));
         }
 
         // submit tasks to threadpool
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/ShotMicroBenchmarkHandler.java	Mon Jun 10 18:32:58 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/ShotMicroBenchmarkHandler.java	Mon Jun 10 19:50:29 2013 +0400
@@ -40,6 +40,7 @@
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
 /**
@@ -87,7 +88,7 @@
         // create tasks
         BenchmarkTask[] runners = new BenchmarkTask[numThreads];
         for (int i = 0; i < runners.length; i++) {
-            runners[i] = new BenchmarkTask(threadLocal, new Loop(numThreads, TimeValue.NONE, preSetupBarrier, preTearDownBarrier, last, false));
+            runners[i] = new BenchmarkTask(threadLocal, new Loop(numThreads, TimeValue.NONE, preSetupBarrier, preTearDownBarrier, last, false, timeUnit));
         }
 
         // submit tasks to threadpool
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/options/BaseOptions.java	Mon Jun 10 18:32:58 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/options/BaseOptions.java	Mon Jun 10 19:50:29 2013 +0400
@@ -32,6 +32,7 @@
 import org.openjdk.jmh.runner.options.handlers.ProfilersOptionHandler;
 import org.openjdk.jmh.runner.options.handlers.ThreadCountsOptionHandler;
 import org.openjdk.jmh.runner.options.handlers.ThreadsOptionHandler;
+import org.openjdk.jmh.runner.options.handlers.TimeUnitOptionHandler;
 import org.openjdk.jmh.runner.options.handlers.TimeValueOptionHandler;
 import org.openjdk.jmh.runner.parameters.Defaults;
 import org.openjdk.jmh.runner.parameters.TimeValue;
@@ -44,6 +45,7 @@
 import java.util.EnumSet;
 import java.util.List;
 import java.util.Set;
+import java.util.concurrent.TimeUnit;
 
 /**
  * Class that handles options which are omni-present everywhere.
@@ -121,6 +123,8 @@
     @Option(name = "-prof", aliases = {"--useprofiler"}, multiValued = false, usage = "Use profilers for collecting additional info, use --listprofilers to list available profilers", handler = ProfilersOptionHandler.class)
     protected Set<ProfilerFactory.Profilers> profilers = EnumSet.noneOf(ProfilerFactory.Profilers.class);
 
+    @Option(name = "-tu", aliases = {"--timeunit"}, usage = "Output time unit. Available values: m, s, ms, us, ns", handler = TimeUnitOptionHandler.class)
+    protected TimeUnit timeUnit = null;
 
     /**
      * returns canonical command line containing all options (differ than default value)
@@ -186,6 +190,9 @@
         if (TimeValue.class.equals(t)) {
             return timeValueFieldToString(f);
         }
+        if (TimeUnit.class.equals(t)) {
+            return timeUnitFieldToString(f);
+        }
         if (Mode.class.equals(t)) {
             return benchTypeFieldToString(f);
         }
@@ -294,6 +301,19 @@
         }
     }
 
+    private String timeUnitFieldToString(Field f) {
+        try {
+            Object value = f.get(this);
+            if (value != null && value instanceof TimeUnit) {
+                return f.getAnnotation(Option.class).name() + " " + TimeUnitOptionHandler.toString((TimeUnit)value);
+            } else {
+                return null;
+            }
+        } catch (IllegalAccessException e) {
+            throw new IllegalAccessError("Caused by: " + e.getMessage());
+        }
+    }
+
     private String benchTypeFieldToString(Field f) {
         try {
             Object value = f.get(this);
@@ -406,6 +426,14 @@
         return verbose;
     }
 
+    /**
+     * Getter
+     *
+     * @return the value
+     */
+    public TimeUnit getTimeUnit() {
+        return timeUnit;
+    }
 
     /**
      * Getter
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/options/handlers/TimeValueOptionHandler.java	Mon Jun 10 18:32:58 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/options/handlers/TimeValueOptionHandler.java	Mon Jun 10 19:50:29 2013 +0400
@@ -65,6 +65,6 @@
 
     @Override
     public String getDefaultMetaVariable() {
-        return "TIME";
+        return "TimeUnit";
     }
 }