changeset 95:9d5036308216

Support legacy mode.
author shade
date Fri, 14 Jun 2013 22:43:17 +0400
parents a481d1e64cda
children e5e0d6488ec6
files jmh-core/src/main/java/org/openjdk/jmh/annotations/Mode.java jmh-core/src/main/java/org/openjdk/jmh/processor/internal/BenchmarkInfo.java jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java jmh-core/src/main/java/org/openjdk/jmh/processor/internal/MicroBenchmarkProcessor.java jmh-core/src/main/java/org/openjdk/jmh/runner/BenchmarkRecord.java jmh-core/src/main/java/org/openjdk/jmh/runner/LoopMicroBenchmarkHandler.java jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java
diffstat 7 files changed, 89 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/annotations/Mode.java	Tue Jun 11 19:26:51 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/annotations/Mode.java	Fri Jun 14 22:43:17 2013 +0400
@@ -65,6 +65,12 @@
      */
     All("all", "TEST MODE"),
 
+    /**
+     * Legacy mode: don't use directly.
+     * This is used notably to support legacy {@link MicroBenchmark} annotation.
+     */
+    Legacy("legacy", "Legacy mode")
+
     ;
     private final String shortLabel;
     private final String longLabel;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/BenchmarkInfo.java	Fri Jun 14 22:43:17 2013 +0400
@@ -0,0 +1,43 @@
+/**
+ * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.openjdk.jmh.processor.internal;
+
+import java.util.Map;
+
+public class BenchmarkInfo {
+    public final String userName;
+    public final String generatedName;
+    public final String generatedPackageName;
+    public final String generatedClassName;
+    public final Map<String, MethodGroup> methodGroups;
+
+    public BenchmarkInfo(String userName, String generatedPackageName, String generatedClassName, Map<String, MethodGroup> methods) {
+        this.userName = userName;
+        this.generatedPackageName = generatedPackageName;
+        this.generatedClassName = generatedClassName;
+        this.generatedName = generatedPackageName + "." + generatedClassName;
+        this.methodGroups = methods;
+    }
+}
--- a/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Tue Jun 11 19:26:51 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Fri Jun 14 22:43:17 2013 +0400
@@ -78,22 +78,6 @@
         return Collections.singleton(GenerateMicroBenchmark.class.getName());
     }
 
-    public static class BenchmarkInfo {
-        public final String userName;
-        public final String generatedName;
-        public final String generatedPackageName;
-        public final String generatedClassName;
-        public final Map<String, MethodGroup> methodGroups;
-
-        public BenchmarkInfo(String userName, String generatedPackageName, String generatedClassName, Map<String, MethodGroup> methods) {
-            this.userName = userName;
-            this.generatedPackageName = generatedPackageName;
-            this.generatedClassName = generatedClassName;
-            this.generatedName = generatedPackageName + "." + generatedClassName;
-            this.methodGroups = methods;
-        }
-    }
-
     @Override
     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
         try {
@@ -124,6 +108,12 @@
                             }
                         }
                     }
+
+                    // hack: collect @Microbenchmarks as well
+                    for (String bench : MicroBenchmarkProcessor.COLLECTED_MICROBENCHMARKS) {
+                        writer.println(bench + ", " + bench + ", " + Mode.Legacy);
+                    }
+
                     writer.close();
                 } catch (IOException ex) {
                     processingEnv.getMessager().printMessage(Kind.ERROR, "Error writing MicroBenchmark list " + ex);
@@ -513,6 +503,8 @@
      * @param methodGroup
      */
     private void generateMethod(Mode benchmarkKind, PrintWriter writer, MethodGroup methodGroup, StateObjectHandler states) {
+        if (benchmarkKind == Mode.Legacy) return;
+
         writer.println();
         for (String ann : generateMethodAnnotations(benchmarkKind, methodGroup)) {
             writer.println("    " + ann);
--- a/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/MicroBenchmarkProcessor.java	Tue Jun 11 19:26:51 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/MicroBenchmarkProcessor.java	Fri Jun 14 22:43:17 2013 +0400
@@ -25,32 +25,24 @@
 package org.openjdk.jmh.processor.internal;
 
 import org.openjdk.jmh.annotations.MicroBenchmark;
-import org.openjdk.jmh.logic.Loop;
-import org.openjdk.jmh.logic.results.Result;
-import org.openjdk.jmh.runner.MicroBenchmarkList;
 
 import javax.annotation.processing.AbstractProcessor;
 import javax.annotation.processing.RoundEnvironment;
 import javax.annotation.processing.SupportedSourceVersion;
 import javax.lang.model.SourceVersion;
 import javax.lang.model.element.Element;
-import javax.lang.model.element.ExecutableElement;
 import javax.lang.model.element.TypeElement;
-import javax.lang.model.element.VariableElement;
 import javax.tools.Diagnostic.Kind;
-import javax.tools.FileObject;
-import javax.tools.StandardLocation;
-import java.io.IOException;
-import java.io.PrintWriter;
 import java.util.Collections;
-import java.util.List;
+import java.util.HashSet;
 import java.util.Set;
-import java.util.TreeSet;
 
 /** @author staffan.friberg@oracle.com */
 @SupportedSourceVersion(SourceVersion.RELEASE_6)
 public class MicroBenchmarkProcessor extends AbstractProcessor {
 
+    public static final Set<String> COLLECTED_MICROBENCHMARKS = new HashSet<String>();
+
     @Override
     public Set<String> getSupportedAnnotationTypes() {
         return Collections.singleton(MicroBenchmark.class.getName());
@@ -70,8 +62,10 @@
                     for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
                         processingEnv.getMessager().printMessage(Kind.MANDATORY_WARNING,
                                 "The " + MicroBenchmark.class.getSimpleName()
-                                        + " is detected. This is not the supported API anymore."
+                                        + " is detected. This is not the supported API anymore. \n"
                                         + element.getEnclosingElement() + '.' + element.toString());
+                        TypeElement klass = (TypeElement) element.getEnclosingElement();
+                        COLLECTED_MICROBENCHMARKS.add(klass.getQualifiedName().toString() + "." + element.getSimpleName().toString());
                     }
                 }
             }
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/BenchmarkRecord.java	Tue Jun 11 19:26:51 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/BenchmarkRecord.java	Fri Jun 14 22:43:17 2013 +0400
@@ -71,7 +71,11 @@
     }
 
     public String generatedTarget(Mode type) {
-        return generatedName + "_" + type;
+        if (mode != Mode.Legacy) {
+            return generatedName + "_" + type;
+        } else {
+            return generatedName;
+        }
     }
 
     public String generatedTarget() {
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/LoopMicroBenchmarkHandler.java	Tue Jun 11 19:26:51 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/LoopMicroBenchmarkHandler.java	Fri Jun 14 22:43:17 2013 +0400
@@ -64,6 +64,8 @@
     /* output options */
     private final boolean shouldFailOnError;
 
+    private final Mode mode;
+
     /**
      * Constructor
      *
@@ -78,6 +80,7 @@
         this.method = method;
         this.shouldSynchIterations = (microbenchmark.getMode() != Mode.SingleShotTime) && executionParams.shouldSynchIterations();
         this.shouldFailOnError = options.shouldFailOnError();
+        this.mode = microbenchmark.getMode();
     }
 
     /**
@@ -104,22 +107,24 @@
             resultList.add(executor.submit(runner));
         }
 
-        // wait for all threads to start executing
-        try {
-            preSetupBarrier.await();
-        } catch (InterruptedException ex) {
-            log(ex);
+        // legacy mode has no knowledge about setup/teardown barriers
+        if (mode != Mode.Legacy) {
+            // wait for all threads to start executing
+            try {
+                preSetupBarrier.await();
+            } catch (InterruptedException ex) {
+                log(ex);
+            }
+            startProfilers();
+
+            // wait for all threads to stop executing
+            try {
+                preTearDownBarrier.await();
+            } catch (InterruptedException ex) {
+                log(ex);
+            }
+            stopProfilers(iterationResults);
         }
-        startProfilers();
-
-
-        // wait for all threads to stop executing
-        try {
-            preTearDownBarrier.await();
-        } catch (InterruptedException ex) {
-            log(ex);
-        }
-        stopProfilers(iterationResults);
 
         // wait for the result, continuously polling the worker threads.
         //
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Tue Jun 11 19:26:51 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Fri Jun 14 22:43:17 2013 +0400
@@ -193,6 +193,7 @@
             if (br.getMode() == Mode.All) {
                 for (Mode mode : Mode.values()) {
                     if (mode == Mode.All) continue;
+                    if (mode == Mode.Legacy) continue;
                     newBenchmarks.add(br.cloneWith(mode));
                 }
             } else {