changeset 134:3b358eae06de

Fold bulk warmup, classic, and forked runs into the same invoker.
author shade
date Wed, 21 Aug 2013 21:17:17 +0400
parents e908157a6c14
children 309279f710b7
files jmh-core/src/main/java/org/openjdk/jmh/runner/BaseRunner.java jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedRunner.java jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java jmh-core/src/main/java/org/openjdk/jmh/runner/parameters/MicroBenchmarkParameters.java jmh-core/src/main/java/org/openjdk/jmh/runner/parameters/MicroBenchmarkParametersFactory.java
diffstat 5 files changed, 69 insertions(+), 168 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/BaseRunner.java	Wed Aug 21 20:54:31 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/BaseRunner.java	Wed Aug 21 21:17:17 2013 +0400
@@ -60,70 +60,15 @@
         this.out = handler;
     }
 
-    /**
-     * Run a micro benchmark in classic execution mode
-     * The method manages both warmup and measurements iterations
-     * @param benchmark benchmark to run
-     */
-    void runClassicBenchmark(BenchmarkRecord benchmark) {
-        List<IterationData> allResults = new ArrayList<IterationData>();
+    void runBenchmark(BenchmarkRecord benchmark, boolean doWarmup, boolean doMeasurement) {
         try {
             Class<?> clazz = ClassUtils.loadClass(benchmark.generatedClass());
             Method method = MicroBenchmarkHandlers.findBenchmarkMethod(clazz, benchmark.generatedMethod());
 
-            MicroBenchmarkParameters executionParams = MicroBenchmarkParametersFactory.makeParams(options, benchmark, method);
+            MicroBenchmarkParameters executionParams = MicroBenchmarkParametersFactory.makeParams(options, benchmark, method, doWarmup, doMeasurement);
             MicroBenchmarkHandler handler = MicroBenchmarkHandlers.getInstance(out, benchmark, clazz, method, executionParams, options);
 
-            out.startBenchmark(handler.getBenchmark(), executionParams, this.options.isVerbose());
-
-            // execute the appropriate number of warmup iterations
-            // before the corresponding measurement interations.
-
-            IterationParams wp = executionParams.getWarmup();
-            for (int i = 1; i <= wp.getCount(); i++) {
-                // will run system gc if we should
-                if (runSystemGC()) {
-                    out.verbosePrintln("System.gc() executed");
-                }
-
-                out.iteration(handler.getBenchmark(), i, IterationType.WARMUP, wp.getThreads(), wp.getTime());
-                boolean isLastIteration = false; // warmup is never the last iteration
-                IterationData iterData = handler.runIteration(wp.getThreads(), wp.getTime(), isLastIteration).setWarmup();
-                out.iterationResult(handler.getBenchmark(), i, IterationType.WARMUP, options.getThreads(), iterData.getAggregatedResult(), iterData.getProfilerResults());
-            }
-
-            IterationParams mp = executionParams.getIteration();
-            for (int i = 1; i <= mp.getCount(); i++) {
-                // will run system gc if we should
-                if (runSystemGC()) {
-                    out.verbosePrintln("System.gc() executed");
-                }
-
-                // run benchmark iteration
-                out.iteration(handler.getBenchmark(), i, IterationType.MEASUREMENT, mp.getThreads(), mp.getTime());
-
-                boolean isLastIteration = (i == mp.getCount());
-                IterationData iterData = handler.runIteration(mp.getThreads(), mp.getTime(), isLastIteration);
-
-                // might get an exception above, in which case the results list will be empty
-                if (iterData.isResultsEmpty()) {
-                    out.println("WARNING: No results returned, benchmark payload threw exception?");
-                } else {
-                    out.iterationResult(handler.getBenchmark(), i, IterationType.MEASUREMENT, mp.getThreads(), iterData.getAggregatedResult(), iterData.getProfilerResults());
-
-                    if (options.shouldOutputDetailedResults()) {
-                        out.detailedResults(handler.getBenchmark(), i, mp.getThreads(), iterData.getAggregatedResult());
-                    }
-
-                    allResults.add(iterData);
-                }
-            }
-
-            // only print end-of-run output if we have actual results
-            if (!allResults.isEmpty()) {
-                RunResult result = aggregateIterationData(allResults);
-                out.endBenchmark(handler.getBenchmark(), result);
-            }
+            runBenchmark(executionParams, handler);
 
             handler.shutdown();
 
@@ -135,6 +80,60 @@
         }
     }
 
+    protected void runBenchmark(MicroBenchmarkParameters executionParams, MicroBenchmarkHandler handler) {
+        List<IterationData> allResults = new ArrayList<IterationData>();
+
+        out.startBenchmark(handler.getBenchmark(), executionParams, this.options.isVerbose());
+
+        // warmup
+        IterationParams wp = executionParams.getWarmup();
+        for (int i = 1; i <= wp.getCount(); i++) {
+            // will run system gc if we should
+            if (runSystemGC()) {
+                out.verbosePrintln("System.gc() executed");
+            }
+
+            out.iteration(handler.getBenchmark(), i, IterationType.WARMUP, wp.getThreads(), wp.getTime());
+            boolean isLastIteration = (executionParams.getIteration().getCount() == 0);
+            IterationData iterData = handler.runIteration(wp.getThreads(), wp.getTime(), isLastIteration).setWarmup();
+            out.iterationResult(handler.getBenchmark(), i, IterationType.WARMUP, options.getThreads(), iterData.getAggregatedResult(), iterData.getProfilerResults());
+        }
+
+        // measurement
+        IterationParams mp = executionParams.getIteration();
+        for (int i = 1; i <= mp.getCount(); i++) {
+            // will run system gc if we should
+            if (runSystemGC()) {
+                out.verbosePrintln("System.gc() executed");
+            }
+
+            // run benchmark iteration
+            out.iteration(handler.getBenchmark(), i, IterationType.MEASUREMENT, mp.getThreads(), mp.getTime());
+
+            boolean isLastIteration = (i == mp.getCount());
+            IterationData iterData = handler.runIteration(mp.getThreads(), mp.getTime(), isLastIteration);
+
+            // might get an exception above, in which case the results list will be empty
+            if (iterData.isResultsEmpty()) {
+                out.println("WARNING: No results returned, benchmark payload threw exception?");
+            } else {
+                out.iterationResult(handler.getBenchmark(), i, IterationType.MEASUREMENT, mp.getThreads(), iterData.getAggregatedResult(), iterData.getProfilerResults());
+
+                if (options.shouldOutputDetailedResults()) {
+                    out.detailedResults(handler.getBenchmark(), i, mp.getThreads(), iterData.getAggregatedResult());
+                }
+
+                allResults.add(iterData);
+            }
+        }
+
+        // only print end-of-run output if we have actual results
+        if (!allResults.isEmpty()) {
+            RunResult result = aggregateIterationData(allResults);
+            out.endBenchmark(handler.getBenchmark(), result);
+        }
+    }
+
 
     /**
      * Execute System.gc() if we the System.gc option is set.
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedRunner.java	Wed Aug 21 20:54:31 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedRunner.java	Wed Aug 21 21:17:17 2013 +0400
@@ -49,7 +49,7 @@
             out.println(benchmark.getUsername());
         }
         out.startRun("Measurement Section");
-        runClassicBenchmark(benchmark);
+        runBenchmark(benchmark, true, true);
         out.endRun(null);
         out.flush();
         out.close();
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Wed Aug 21 20:54:31 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Wed Aug 21 21:17:17 2013 +0400
@@ -27,14 +27,11 @@
 import org.openjdk.jmh.annotations.Fork;
 import org.openjdk.jmh.annotations.Mode;
 import org.openjdk.jmh.logic.results.IterationData;
-import org.openjdk.jmh.logic.results.internal.RunResult;
 import org.openjdk.jmh.output.OutputFormatFactory;
-import org.openjdk.jmh.output.format.IterationType;
 import org.openjdk.jmh.output.format.OutputFormat;
 import org.openjdk.jmh.output.format.internal.BinaryOutputFormatReader;
 import org.openjdk.jmh.runner.options.HarnessOptions;
 import org.openjdk.jmh.runner.parameters.Defaults;
-import org.openjdk.jmh.runner.parameters.IterationParams;
 import org.openjdk.jmh.runner.parameters.MicroBenchmarkParameters;
 import org.openjdk.jmh.runner.parameters.MicroBenchmarkParametersFactory;
 import org.openjdk.jmh.util.AnnotationUtils;
@@ -231,7 +228,7 @@
             // currently valid only for non-external JVM runs
             out.startRun("Warmup Section");
             for (BenchmarkRecord benchmark : warmupMicros) {
-                runBulkWarmupModeMicroBenchmark(benchmark, true);
+                runBenchmark(benchmark, true, false);
             }
             out.endRun(null);
         }
@@ -239,7 +236,7 @@
         //
         out.startRun("Measurement Section");
         for (BenchmarkRecord benchmark : benchmarks) {
-            runBulkWarmupModeMicroBenchmark(benchmark, false);
+            runBenchmark(benchmark, false, true);
         }
         out.endRun(null);
     }
@@ -279,7 +276,7 @@
         }
 
         for (BenchmarkRecord benchmark : embedded) {
-            runClassicBenchmark(benchmark);
+            runBenchmark(benchmark, true, true);
         }
 
         runSeparate(forked);
@@ -410,90 +407,4 @@
         }
     }
 
-    /**
-     * Run a micro benchmark in bulk warmup mode
-     *
-     * @param benchmark benchmark to run
-     */
-    private List<IterationData> runBulkWarmupModeMicroBenchmark(BenchmarkRecord benchmark, boolean warmup) {
-        List<IterationData> allResults = new ArrayList<IterationData>();
-        try {
-            String benchName = benchmark.generatedTarget();
-            int index = benchName.lastIndexOf('.');
-            String className = benchName.substring(0, index);
-            String methodName = benchName.substring(index + 1);
-
-            Class<?> clazz = ClassUtils.loadClass(className);
-            Method method = MicroBenchmarkHandlers.findBenchmarkMethod(clazz, methodName);
-
-            MicroBenchmarkParameters executionParams = MicroBenchmarkParametersFactory.makeParams(options, benchmark, method);
-            MicroBenchmarkHandler handler = MicroBenchmarkHandlers.getInstance(out, benchmark, clazz, method, executionParams, options);
-
-            out.startBenchmark(handler.getBenchmark(), executionParams, options.isVerbose());
-
-            if (warmup) {
-                IterationParams p = executionParams.getWarmup();
-                for (int i = 1; i <= p.getCount(); i++) {
-                    // will run system gc if we should
-                    if (runSystemGC()) {
-                        out.verbosePrintln("System.gc() executed");
-                    }
-
-                    // run benchmark iteration
-                    out.iteration(handler.getBenchmark(), i, IterationType.WARMUP, p.getThreads(), p.getTime());
-
-                    boolean isLastIteration = (i == p.getCount());
-                    IterationData iterData = handler.runIteration(p.getThreads(), p.getTime(), isLastIteration);
-
-                    // might get an exception above, in which case the results list will be empty
-                    if (iterData.isResultsEmpty()) {
-                        out.println("WARNING: No results returned, benchmark payload threw exception?");
-                    } else {
-                        // print out score for this iteration
-                        out.iterationResult(handler.getBenchmark(), i, IterationType.WARMUP, p.getThreads(), iterData.getAggregatedResult(), iterData.getProfilerResults());
-                    }
-                }
-            } else {
-                IterationParams p = executionParams.getIteration();
-                for (int i = 1; i <= p.getCount(); i++) {
-                    // will run system gc if we should
-                    if (runSystemGC()) {
-                        out.verbosePrintln("System.gc() executed");
-                    }
-
-                    // run benchmark iteration
-                    out.iteration(handler.getBenchmark(), i, IterationType.MEASUREMENT, p.getThreads(), p.getTime());
-
-                    boolean isLastIteration = (i == p.getCount());
-                    IterationData iterData = handler.runIteration(p.getThreads(), p.getTime(), isLastIteration);
-
-                    // might get an exception above, in which case the results list will be empty
-                    if (iterData.isResultsEmpty()) {
-                        out.println("WARNING: No results returned, benchmark payload threw exception?");
-                    } else {
-                        // non-empty list => output and aggregate results
-                        allResults.add(iterData);
-
-                        // print out score for this iteration
-                        out.iterationResult(handler.getBenchmark(), i, IterationType.MEASUREMENT, options.getThreads(), iterData.getAggregatedResult(), iterData.getProfilerResults());
-                    }
-                }
-            }
-            // only print end-of-run output if we have actual results
-            if (!allResults.isEmpty()) {
-                RunResult result = aggregateIterationData(allResults);
-                out.endBenchmark(handler.getBenchmark(), result);
-            }
-
-            handler.shutdown();
-
-        } catch (Throwable ex) {
-            out.exception(ex);
-            if (options.shouldFailOnError()) {
-                throw new IllegalStateException(ex.getMessage(), ex);
-            }
-        }
-        return allResults;
-    }
-
 }
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/parameters/MicroBenchmarkParameters.java	Wed Aug 21 20:54:31 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/parameters/MicroBenchmarkParameters.java	Wed Aug 21 21:17:17 2013 +0400
@@ -46,10 +46,4 @@
 
     public int getThreads();
 
-    /**
-     * returns MicroBenchmarkParameters where warmup parameters pushed to iteration
-     * parameters. Used by bulk warmup mode.
-     * @return
-     */
-    public MicroBenchmarkParameters warmupToIteration();
 }
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/parameters/MicroBenchmarkParametersFactory.java	Wed Aug 21 20:54:31 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/parameters/MicroBenchmarkParametersFactory.java	Wed Aug 21 21:17:17 2013 +0400
@@ -39,7 +39,7 @@
     private MicroBenchmarkParametersFactory() {
     }
 
-    public static MicroBenchmarkParameters makeParams(BaseOptions options, BenchmarkRecord benchmark, Method method) {
+    public static MicroBenchmarkParameters makeParams(BaseOptions options, BenchmarkRecord benchmark, Method method, boolean doWarmup, boolean doMeasurement) {
         boolean shouldSynchIterations = getBoolean(options.getSynchIterations(), Defaults.SHOULD_SYNCH_ITERATIONS);
 
         int threads = getThreads(options, method);
@@ -47,8 +47,13 @@
             threads = Runtime.getRuntime().availableProcessors();
         }
 
-        IterationParams measurement = getMeasurement(options, benchmark, method, threads);
-        IterationParams warmup = getWarmup(options, benchmark, method, threads);
+        IterationParams measurement = doMeasurement ?
+                getMeasurement(options, benchmark, method, threads) :
+                new IterationParams(0, TimeValue.NONE, 1);
+
+        IterationParams warmup = doWarmup ?
+                getWarmup(options, benchmark, method, threads) :
+                new IterationParams(0, TimeValue.NONE, 1);
 
         return new SameThreadParameters(
                 shouldSynchIterations,
@@ -151,14 +156,6 @@
             return threads;
         }
 
-        @Override
-        public MicroBenchmarkParameters warmupToIteration() {
-            return new SameThreadParameters(
-                    this.shouldSynchIterations(),
-                    this.getWarmup(),
-                    this.getWarmup(),
-                    this.getThreads());
-        }
     }
 
     private static boolean getBoolean(Boolean value, boolean defaultValue) {