changeset 327:f227bf756864

@State annotation should always have a value. This will push users to think about state sharing mode before it is too late.
author shade
date Wed, 01 Jan 2014 18:17:34 +0400
parents c2af91629c91
children a020506bbc01
files jmh-core-benchmarks/src/main/java/org/openjdk/jmh/benchmarks/BlackholePipelineBench.java jmh-core-it/src/test/java/org/openjdk/jmh/it/IterationCountAnnTest.java jmh-core-it/src/test/java/org/openjdk/jmh/it/IterationCountCmdTest.java jmh-core-it/src/test/java/org/openjdk/jmh/it/WarmupIterationCountAnnTest.java jmh-core-it/src/test/java/org/openjdk/jmh/it/WarmupIterationCountCmdTest.java jmh-core-it/src/test/java/org/openjdk/jmh/it/fails/FailingForkedBenchTest.java jmh-core-it/src/test/java/org/openjdk/jmh/it/fails/FailingForkedSetupTest.java jmh-core-it/src/test/java/org/openjdk/jmh/it/fails/FailingForkedTearDownTest.java jmh-core/src/main/java/org/openjdk/jmh/annotations/State.java jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_05_StateFixtures.java jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_06_FixtureLevel.java jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_07_FixtureLevelInvocation.java jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_13_RunToRun.java
diffstat 14 files changed, 40 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core-benchmarks/src/main/java/org/openjdk/jmh/benchmarks/BlackholePipelineBench.java	Tue Dec 24 22:32:16 2013 +0400
+++ b/jmh-core-benchmarks/src/main/java/org/openjdk/jmh/benchmarks/BlackholePipelineBench.java	Wed Jan 01 18:17:34 2014 +0400
@@ -29,13 +29,14 @@
 import org.openjdk.jmh.annotations.Mode;
 import org.openjdk.jmh.annotations.OperationsPerInvocation;
 import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Scope;
 import org.openjdk.jmh.annotations.Setup;
 import org.openjdk.jmh.annotations.State;
 import org.openjdk.jmh.logic.BlackHole;
 
 import java.util.concurrent.TimeUnit;
 
-@State
+@State(Scope.Thread)
 @BenchmarkMode(Mode.AverageTime)
 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 public class BlackholePipelineBench {
--- a/jmh-core-it/src/test/java/org/openjdk/jmh/it/IterationCountAnnTest.java	Tue Dec 24 22:32:16 2013 +0400
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/IterationCountAnnTest.java	Wed Jan 01 18:17:34 2014 +0400
@@ -32,6 +32,7 @@
 import org.openjdk.jmh.annotations.Level;
 import org.openjdk.jmh.annotations.Measurement;
 import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.Scope;
 import org.openjdk.jmh.annotations.Setup;
 import org.openjdk.jmh.annotations.State;
 import org.openjdk.jmh.annotations.TearDown;
@@ -49,7 +50,7 @@
  *
  * @author Aleksey Shipilev (aleksey.shipilev@oracle.com)
  */
-@State
+@State(Scope.Thread)
 public class IterationCountAnnTest {
 
     private final AtomicInteger count = new AtomicInteger();
--- a/jmh-core-it/src/test/java/org/openjdk/jmh/it/IterationCountCmdTest.java	Tue Dec 24 22:32:16 2013 +0400
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/IterationCountCmdTest.java	Wed Jan 01 18:17:34 2014 +0400
@@ -31,6 +31,7 @@
 import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
 import org.openjdk.jmh.annotations.Level;
 import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.Scope;
 import org.openjdk.jmh.annotations.Setup;
 import org.openjdk.jmh.annotations.State;
 import org.openjdk.jmh.annotations.TearDown;
@@ -46,7 +47,7 @@
  *
  * @author Aleksey Shipilev (aleksey.shipilev@oracle.com)
  */
-@State
+@State(Scope.Thread)
 public class IterationCountCmdTest {
 
     private final AtomicInteger count = new AtomicInteger();
--- a/jmh-core-it/src/test/java/org/openjdk/jmh/it/WarmupIterationCountAnnTest.java	Tue Dec 24 22:32:16 2013 +0400
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/WarmupIterationCountAnnTest.java	Wed Jan 01 18:17:34 2014 +0400
@@ -32,6 +32,7 @@
 import org.openjdk.jmh.annotations.Level;
 import org.openjdk.jmh.annotations.Measurement;
 import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.Scope;
 import org.openjdk.jmh.annotations.Setup;
 import org.openjdk.jmh.annotations.State;
 import org.openjdk.jmh.annotations.TearDown;
@@ -48,7 +49,7 @@
  *
  * @author Aleksey Shipilev (aleksey.shipilev@oracle.com)
  */
-@State
+@State(Scope.Thread)
 public class WarmupIterationCountAnnTest {
 
     private final AtomicInteger count = new AtomicInteger();
--- a/jmh-core-it/src/test/java/org/openjdk/jmh/it/WarmupIterationCountCmdTest.java	Tue Dec 24 22:32:16 2013 +0400
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/WarmupIterationCountCmdTest.java	Wed Jan 01 18:17:34 2014 +0400
@@ -31,6 +31,7 @@
 import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
 import org.openjdk.jmh.annotations.Level;
 import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.Scope;
 import org.openjdk.jmh.annotations.Setup;
 import org.openjdk.jmh.annotations.State;
 import org.openjdk.jmh.annotations.TearDown;
@@ -47,7 +48,7 @@
  *
  * @author Aleksey Shipilev (aleksey.shipilev@oracle.com)
  */
-@State
+@State(Scope.Thread)
 public class WarmupIterationCountCmdTest {
 
     private final AtomicInteger count = new AtomicInteger();
--- a/jmh-core-it/src/test/java/org/openjdk/jmh/it/fails/FailingForkedBenchTest.java	Tue Dec 24 22:32:16 2013 +0400
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/fails/FailingForkedBenchTest.java	Wed Jan 01 18:17:34 2014 +0400
@@ -31,6 +31,7 @@
 import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
 import org.openjdk.jmh.annotations.Measurement;
 import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.Scope;
 import org.openjdk.jmh.annotations.State;
 import org.openjdk.jmh.annotations.Threads;
 import org.openjdk.jmh.annotations.Warmup;
@@ -46,7 +47,7 @@
  *
  * @author Aleksey Shipilev (aleksey.shipilev@oracle.com)
  */
-@State
+@State(Scope.Thread)
 public class FailingForkedBenchTest {
 
     @GenerateMicroBenchmark
--- a/jmh-core-it/src/test/java/org/openjdk/jmh/it/fails/FailingForkedSetupTest.java	Tue Dec 24 22:32:16 2013 +0400
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/fails/FailingForkedSetupTest.java	Wed Jan 01 18:17:34 2014 +0400
@@ -31,6 +31,7 @@
 import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
 import org.openjdk.jmh.annotations.Measurement;
 import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.Scope;
 import org.openjdk.jmh.annotations.Setup;
 import org.openjdk.jmh.annotations.State;
 import org.openjdk.jmh.annotations.Threads;
@@ -47,7 +48,7 @@
  *
  * @author Aleksey Shipilev (aleksey.shipilev@oracle.com)
  */
-@State
+@State(Scope.Thread)
 public class FailingForkedSetupTest {
 
     @Setup
--- a/jmh-core-it/src/test/java/org/openjdk/jmh/it/fails/FailingForkedTearDownTest.java	Tue Dec 24 22:32:16 2013 +0400
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/fails/FailingForkedTearDownTest.java	Wed Jan 01 18:17:34 2014 +0400
@@ -31,6 +31,7 @@
 import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
 import org.openjdk.jmh.annotations.Measurement;
 import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.Scope;
 import org.openjdk.jmh.annotations.State;
 import org.openjdk.jmh.annotations.TearDown;
 import org.openjdk.jmh.annotations.Threads;
@@ -47,7 +48,7 @@
  *
  * @author Aleksey Shipilev (aleksey.shipilev@oracle.com)
  */
-@State
+@State(Scope.Thread)
 public class FailingForkedTearDownTest {
 
     @TearDown
--- a/jmh-core/src/main/java/org/openjdk/jmh/annotations/State.java	Tue Dec 24 22:32:16 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/annotations/State.java	Wed Jan 01 18:17:34 2014 +0400
@@ -47,6 +47,6 @@
      * State scope.
      * @return state scope
      */
-    Scope value() default Scope.Thread;
+    Scope value();
 
 }
--- a/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Tue Dec 24 22:32:16 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Wed Jan 01 18:17:34 2014 +0400
@@ -66,13 +66,13 @@
 import javax.lang.model.element.TypeElement;
 import javax.lang.model.element.VariableElement;
 import javax.lang.model.util.ElementFilter;
-import javax.lang.model.util.Types;
 import javax.tools.Diagnostic.Kind;
 import javax.tools.FileObject;
 import javax.tools.JavaFileObject;
 import javax.tools.StandardLocation;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.lang.annotation.IncompleteAnnotationException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -193,8 +193,24 @@
             }
         }
 
+        // validate if enclosing class is implicit @State
+        if (clazz.getAnnotation(State.class) != null) {
+            states.add(clazz);
+        }
+
         // validate @State classes
         for (TypeElement state : states) {
+            // Because of https://bugs.openjdk.java.net/browse/JDK-8031122,
+            // we need to preemptively check the annotation value, and
+            // the API can only allow that by catching the exception, argh.
+            try {
+                state.getAnnotation(State.class).value();
+            } catch (IncompleteAnnotationException iae) {
+                throw new GenerationException("The " + State.class.getSimpleName() +
+                        " annotation should have the explicit " + Scope.class.getSimpleName() + " argument",
+                        state);
+            }
+
             if (!state.getModifiers().contains(Modifier.PUBLIC)) {
                 throw new GenerationException("The " + State.class.getSimpleName() +
                         " annotation only supports public classes.", state);
--- a/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_05_StateFixtures.java	Tue Dec 24 22:32:16 2013 +0400
+++ b/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_05_StateFixtures.java	Wed Jan 01 18:17:34 2014 +0400
@@ -25,11 +25,12 @@
 package org.openjdk.jmh.samples;
 
 import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
+import org.openjdk.jmh.annotations.Scope;
 import org.openjdk.jmh.annotations.Setup;
 import org.openjdk.jmh.annotations.State;
 import org.openjdk.jmh.annotations.TearDown;
 
-@State
+@State(Scope.Thread)
 public class JMHSample_05_StateFixtures {
 
     double x;
--- a/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_06_FixtureLevel.java	Tue Dec 24 22:32:16 2013 +0400
+++ b/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_06_FixtureLevel.java	Wed Jan 01 18:17:34 2014 +0400
@@ -26,10 +26,11 @@
 
 import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
 import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Scope;
 import org.openjdk.jmh.annotations.State;
 import org.openjdk.jmh.annotations.TearDown;
 
-@State
+@State(Scope.Thread)
 public class JMHSample_06_FixtureLevel {
 
     double x;
--- a/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_07_FixtureLevelInvocation.java	Tue Dec 24 22:32:16 2013 +0400
+++ b/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_07_FixtureLevelInvocation.java	Wed Jan 01 18:17:34 2014 +0400
@@ -69,7 +69,7 @@
      * it is kept around the same across all iterations.
      */
 
-    @State
+    @State(Scope.Benchmark)
     public static class NormalState {
         ExecutorService service;
 
--- a/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_13_RunToRun.java	Tue Dec 24 22:32:16 2013 +0400
+++ b/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_13_RunToRun.java	Wed Jan 01 18:17:34 2014 +0400
@@ -56,7 +56,7 @@
      * will have the similar behavior, but we do that artificially to make a point.
      */
 
-    @State
+    @State(Scope.Thread)
     public static class SleepyState {
         public long sleepTime;