changeset 79:f1383f50315b

Accept multiple BenchmarkMode arguments.
author shade
date Tue, 04 Jun 2013 15:01:31 +0400
parents 9d9133e41e4c
children d0a438249900
files jmh-core/src/main/java/org/openjdk/jmh/annotations/BenchmarkMode.java jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java jmh-core/src/main/java/org/openjdk/jmh/processor/internal/MethodGroup.java jmh-core/src/main/java/org/openjdk/jmh/runner/BenchmarkRecord.java jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_02_BenchmarkModes.java
diffstat 6 files changed, 43 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/annotations/BenchmarkMode.java	Tue Jun 04 14:46:43 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/annotations/BenchmarkMode.java	Tue Jun 04 15:01:31 2013 +0400
@@ -29,6 +29,6 @@
  */
 public @interface BenchmarkMode {
 
-    Mode value();
+    Mode[] value();
 
 }
--- a/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Tue Jun 04 14:46:43 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Tue Jun 04 15:01:31 2013 +0400
@@ -117,7 +117,9 @@
                     for (BenchmarkInfo info : benchmarkInfos) {
                         for (String method : info.methodGroups.keySet()) {
                             MethodGroup group = info.methodGroups.get(method);
-                            writer.println(info.userName + "." + method + ", " + info.generatedName + "." + method + ", " + group.getMode());
+                            for (Mode m : group.getModes()) {
+                                writer.println(info.userName + "." + method + ", " + info.generatedName + "." + method + ", " + m);
+                            }
                         }
                     }
                     writer.close();
@@ -201,11 +203,11 @@
 
             BenchmarkMode mbAn = method.getAnnotation(BenchmarkMode.class);
             if (mbAn != null) {
-                group.setMode(mbAn.value());
+                group.addModes(mbAn.value());
             } else {
                 mbAn = method.getEnclosingElement().getAnnotation(BenchmarkMode.class);
                 if (mbAn != null) {
-                    group.setMode(mbAn.value());
+                    group.addModes(mbAn.value());
                 }
             }
 
@@ -216,8 +218,8 @@
 
         // enforce the default value
         for (MethodGroup group : result.values()) {
-            if (group.getMode() == null) {
-                group.setMode(Mode.Throughput);
+            if (group.getModes().isEmpty()) {
+                group.addModes(Mode.Throughput);
             }
         }
 
--- a/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/MethodGroup.java	Tue Jun 04 14:46:43 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/MethodGroup.java	Tue Jun 04 15:01:31 2013 +0400
@@ -29,6 +29,8 @@
 import javax.lang.model.element.Element;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.EnumSet;
 import java.util.LinkedHashSet;
 import java.util.Set;
 
@@ -36,11 +38,12 @@
     private final String name;
     private final Set<MethodInvocation> methods;
     private boolean strictFP;
-    private Mode mode;
+    private EnumSet<Mode> modes;
 
     MethodGroup(String name) {
         this.name = name;
         this.methods = new LinkedHashSet<MethodInvocation>();
+        this.modes = EnumSet.noneOf(Mode.class);
     }
 
     @Override
@@ -106,17 +109,15 @@
         return strictFP;
     }
 
-    public void setMode(Mode eMode) {
-        if (mode == null) {
-            mode = eMode;
-        } else {
-            if (mode != eMode) {
-                throw new IllegalStateException("Clashing benchmark modes: mode = " + mode + ", trying to set = " + eMode);
-            }
-        }
+    public void addModes(Mode eMode) {
+        modes.add(eMode);
     }
 
-    public Mode getMode() {
-        return mode;
+    public void addModes(Mode[] eModes) {
+        Collections.addAll(modes, eModes);
+    }
+
+    public Set<Mode> getModes() {
+        return modes;
     }
 }
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/BenchmarkRecord.java	Tue Jun 04 14:46:43 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/BenchmarkRecord.java	Tue Jun 04 15:01:31 2013 +0400
@@ -62,12 +62,12 @@
 
     @Override
     public int compareTo(BenchmarkRecord o) {
-        int v = userName.compareTo(o.userName);
+        int v = mode.compareTo(o.mode);
         if (v != 0) {
             return v;
         }
 
-        return mode.compareTo(o.mode);
+        return userName.compareTo(o.userName);
     }
 
     public String generatedTarget(Mode type) {
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Tue Jun 04 14:46:43 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Tue Jun 04 15:01:31 2013 +0400
@@ -173,20 +173,21 @@
 
         // override the benchmark types;
         // this may yield new benchmark records
-        List<BenchmarkRecord> newBenchmarks = new ArrayList<BenchmarkRecord>();
         if (options.getBenchModes() != null) {
+            List<BenchmarkRecord> newBenchmarks = new ArrayList<BenchmarkRecord>();
             for (BenchmarkRecord br : benchmarks) {
                 for (Mode m : options.getBenchModes()) {
                     newBenchmarks.add(br.cloneWith(m));
                 }
 
             }
+
+            benchmarks.clear();
+            benchmarks.addAll(newBenchmarks);
         }
 
-        benchmarks.clear();
-        benchmarks.addAll(newBenchmarks);
-
         // clone with all the modes
+        List<BenchmarkRecord> newBenchmarks = new ArrayList<BenchmarkRecord>();
         for (BenchmarkRecord br : benchmarks) {
             if (br.getMode() == Mode.All) {
                 for (Mode mode : Mode.values()) {
--- a/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_02_BenchmarkModes.java	Tue Jun 04 14:46:43 2013 +0400
+++ b/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_02_BenchmarkModes.java	Tue Jun 04 15:01:31 2013 +0400
@@ -56,9 +56,9 @@
      * although you can use the default.
      */
 
-    @OutputTimeUnit(TimeUnit.SECONDS)
     @GenerateMicroBenchmark
     @BenchmarkMode(Mode.Throughput)
+    @OutputTimeUnit(TimeUnit.SECONDS)
     public void measureThroughput() throws InterruptedException {
         TimeUnit.MILLISECONDS.sleep(100);
     }
@@ -69,9 +69,9 @@
      * There are workloads where measuring times is more convenient though.
      */
 
-    @OutputTimeUnit(TimeUnit.MICROSECONDS)
     @GenerateMicroBenchmark
     @BenchmarkMode(Mode.AverageTime)
+    @OutputTimeUnit(TimeUnit.MICROSECONDS)
     public void measureAvgTime() throws InterruptedException {
         TimeUnit.MILLISECONDS.sleep(100);
     }
@@ -86,9 +86,9 @@
      * JMH also tries to auto-adjust sampling frequency: if the method
      * is long enough, you will end up capturing all the samples.
      */
-    @OutputTimeUnit(TimeUnit.MICROSECONDS)
     @GenerateMicroBenchmark
     @BenchmarkMode(Mode.SampleTime)
+    @OutputTimeUnit(TimeUnit.MICROSECONDS)
     public void measureSamples() throws InterruptedException {
         TimeUnit.MILLISECONDS.sleep(100);
     }
@@ -99,14 +99,26 @@
      * do not want to call the benchmark method continuously.
      */
 
-    @OutputTimeUnit(TimeUnit.MICROSECONDS)
     @GenerateMicroBenchmark
     @BenchmarkMode(Mode.SingleShotTime)
+    @OutputTimeUnit(TimeUnit.MICROSECONDS)
     public void measureSingleShot() throws InterruptedException {
         TimeUnit.MILLISECONDS.sleep(100);
     }
 
     /*
+     * We can also ask for multiple benchmark modes at once. All the tests
+     * above can be replaced with just a single test like this:
+     */
+
+    @GenerateMicroBenchmark
+    @BenchmarkMode({Mode.Throughput, Mode.AverageTime, Mode.SampleTime, Mode.SingleShotTime})
+    @OutputTimeUnit(TimeUnit.MICROSECONDS)
+    public void measureAll() throws InterruptedException {
+        TimeUnit.MILLISECONDS.sleep(100);
+    }
+
+    /*
      * HOW TO RUN THIS TEST:
      *
      * You can run this test with: