changeset 84:ec2dc3520cd1

Fixing the JVM parameters handling in @Fork annotation, overrides the values correctly now.
author shade
date Mon, 10 Jun 2013 18:24:29 +0400
parents 53422f2791fe
children 005b4d1a4c80
files jmh-core-it/src/test/java/org/openjdk/jmh/it/fork/ForkedJvmAppendPrependArgsTest2.java jmh-core-it/src/test/java/org/openjdk/jmh/it/fork/ForkedJvmArgsTest2.java jmh-core/src/main/java/org/openjdk/jmh/annotations/Fork.java jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java jmh-core/src/main/java/org/openjdk/jmh/util/internal/CollectionUtils.java
diffstat 6 files changed, 245 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/fork/ForkedJvmAppendPrependArgsTest2.java	Mon Jun 10 18:24:29 2013 +0400
@@ -0,0 +1,121 @@
+/**
+ * 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.it.fork;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.openjdk.jmh.Main;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.it.Fixtures;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ *
+ * @author Sergey Kuksenko (sergey.kuksenko@oracle.com)
+ */
+@BenchmarkMode(Mode.All)
+@Fork(jvmArgsAppend = "-DappendedUp", jvmArgsPrepend = "-DprependedUp")
+public class ForkedJvmAppendPrependArgsTest2 {
+
+    @GenerateMicroBenchmark
+    @Warmup(iterations = 0)
+    @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+    @Fork(jvmArgs = "-Dreplaced", jvmArgsAppend = "-Dappended", jvmArgsPrepend = "-Dprepended")
+    public void test1() {
+        Fixtures.work();
+        Assert.assertNotNull(System.getProperty("replaced"));
+        Assert.assertNull(System.getProperty("appended"));
+        Assert.assertNull(System.getProperty("prepended"));
+        Assert.assertNull(System.getProperty("appendedUp"));
+        Assert.assertNull(System.getProperty("prependedUp"));
+    }
+
+    @GenerateMicroBenchmark
+    @Warmup(iterations = 0)
+    @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+    @Fork(jvmArgsAppend = "-Dappended", jvmArgsPrepend = "-Dprepended")
+    public void test2() {
+        Fixtures.work();
+        Assert.assertNull(System.getProperty("replaced"));
+        Assert.assertNotNull(System.getProperty("appended"));
+        Assert.assertNotNull(System.getProperty("prepended"));
+        Assert.assertNull(System.getProperty("appendedUp"));
+        Assert.assertNull(System.getProperty("prependedUp"));
+    }
+
+    @GenerateMicroBenchmark
+    @Warmup(iterations = 0)
+    @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+    @Fork(jvmArgsPrepend = "-Dprepended")
+    public void test3() {
+        Fixtures.work();
+        Assert.assertNull(System.getProperty("replaced"));
+        Assert.assertNull(System.getProperty("appended"));
+        Assert.assertNotNull(System.getProperty("prepended"));
+        Assert.assertNotNull(System.getProperty("appendedUp"));
+        Assert.assertNull(System.getProperty("prependedUp"));
+
+    }
+
+    @GenerateMicroBenchmark
+    @Warmup(iterations = 0)
+    @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+    @Fork(jvmArgsAppend = "-Dappended")
+    public void test4() {
+        Fixtures.work();
+        Assert.assertNull(System.getProperty("replaced"));
+        Assert.assertNotNull(System.getProperty("appended"));
+        Assert.assertNull(System.getProperty("prepended"));
+        Assert.assertNull(System.getProperty("appendedUp"));
+        Assert.assertNotNull(System.getProperty("prependedUp"));
+
+    }
+
+    @GenerateMicroBenchmark
+    @Warmup(iterations = 0)
+    @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+    @Fork()
+    public void test5() {
+        Fixtures.work();
+        Assert.assertNull(System.getProperty("replaced"));
+        Assert.assertNull(System.getProperty("appended"));
+        Assert.assertNull(System.getProperty("prepended"));
+        Assert.assertNotNull(System.getProperty("appendedUp"));
+        Assert.assertNotNull(System.getProperty("prependedUp"));
+
+    }
+
+    @Test
+    public void invoke() {
+        Main.testMain(Fixtures.getTestMask(this.getClass()) + " -foe");
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/fork/ForkedJvmArgsTest2.java	Mon Jun 10 18:24:29 2013 +0400
@@ -0,0 +1,96 @@
+/**
+ * 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.it.fork;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.openjdk.jmh.Main;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.it.Fixtures;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ *
+ * @author Sergey Kuksenko (sergey.kuksenko@oracle.com)
+ */
+@BenchmarkMode(Mode.All)
+@Fork(jvmArgs = "-DtestUpper")
+public class ForkedJvmArgsTest2 {
+
+    @GenerateMicroBenchmark
+    @Warmup(iterations = 0)
+    @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+    @Fork(jvmArgs = "-Dtest1")
+    public void test1() {
+        Fixtures.work();
+        Assert.assertNotNull(System.getProperty("test1"));
+        Assert.assertNull(System.getProperty("test2"));
+        Assert.assertNull(System.getProperty("testUpper"));
+    }
+
+    @GenerateMicroBenchmark
+    @Warmup(iterations = 0)
+    @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+    @Fork(jvmArgs = "-Dtest2")
+    public void test2() {
+        Fixtures.work();
+        Assert.assertNull(System.getProperty("test1"));
+        Assert.assertNotNull(System.getProperty("test2"));
+        Assert.assertNull(System.getProperty("testUpper"));
+    }
+
+    @GenerateMicroBenchmark
+    @Warmup(iterations = 0)
+    @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+    @Fork
+    public void testUpper() {
+        Fixtures.work();
+        Assert.assertNull(System.getProperty("test1"));
+        Assert.assertNull(System.getProperty("test2"));
+        Assert.assertNotNull(System.getProperty("testUpper"));
+    }
+
+    @GenerateMicroBenchmark
+    @Warmup(iterations = 0)
+    @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+    public void testNone() {
+        Fixtures.work();
+        Assert.assertNull(System.getProperty("test1"));
+        Assert.assertNull(System.getProperty("test2"));
+        Assert.assertNotNull(System.getProperty("testUpper"));
+    }
+
+    @Test
+    public void invoke() {
+        Main.testMain(Fixtures.getTestMask(this.getClass()) + " -foe");
+    }
+
+}
--- a/jmh-core/src/main/java/org/openjdk/jmh/annotations/Fork.java	Wed Jun 05 17:18:24 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/annotations/Fork.java	Mon Jun 10 18:24:29 2013 +0400
@@ -24,6 +24,8 @@
  */
 package org.openjdk.jmh.annotations;
 
+import org.openjdk.jmh.util.AnnotationUtils;
+
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
@@ -40,19 +42,17 @@
 @Retention(RetentionPolicy.RUNTIME)
 public @interface Fork {
 
-    static final String PARAM_NOT_SET = "NOT_SET_20122012"; // random String marking the not set value
-
     /** specifies number of times harness should fork, zero means "no fork" */
     public int value() default 1;
 
     /** enforce strict JVM args, replaces any implicit jvm args */
-    public String jvmArgs() default PARAM_NOT_SET;
+    public String jvmArgs() default AnnotationUtils.PARAM_NOT_SET;
 
     /** prepend these arguments in the command line */
-    public String jvmArgsPrepend() default PARAM_NOT_SET;
+    public String jvmArgsPrepend() default AnnotationUtils.PARAM_NOT_SET;
 
     /** append these arguments in the command line */
-    public String jvmArgsAppend() default PARAM_NOT_SET;
+    public String jvmArgsAppend() default AnnotationUtils.PARAM_NOT_SET;
 
     /** ignore results first warmups forks */
     public int warmups() default 0;
--- a/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Wed Jun 05 17:18:24 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Mon Jun 10 18:24:29 2013 +0400
@@ -34,6 +34,7 @@
 import org.openjdk.jmh.logic.results.SampleTimePerOp;
 import org.openjdk.jmh.logic.results.SingleShotTime;
 import org.openjdk.jmh.runner.MicroBenchmarkList;
+import org.openjdk.jmh.util.AnnotationUtils;
 import org.openjdk.jmh.util.internal.CollectionUtils;
 import org.openjdk.jmh.util.internal.SampleBuffer;
 
@@ -643,9 +644,9 @@
 
     private static Map<String, String> forkToMap(Map<String, String> map, Fork fAnnotation) {
         if (fAnnotation != null) {
-            map = CollectionUtils.conditionalPutAndCreateTreeMapIfAbsent(map, !fAnnotation.jvmArgs().trim().isEmpty(), "jvmArgs", fAnnotation.jvmArgs().trim());
-            map = CollectionUtils.conditionalPutAndCreateTreeMapIfAbsent(map, !fAnnotation.jvmArgsAppend().trim().isEmpty(), "jvmArgsAppend", fAnnotation.jvmArgsAppend().trim());
-            map = CollectionUtils.conditionalPutAndCreateTreeMapIfAbsent(map, !fAnnotation.jvmArgsPrepend().trim().isEmpty(), "jvmArgsPrepend", fAnnotation.jvmArgsPrepend().trim());
+            map = CollectionUtils.conditionalPutAndCreateTreeMapIfAbsentAndQuote(map, AnnotationUtils.isSet(fAnnotation.jvmArgs()),        "jvmArgs", fAnnotation.jvmArgs());
+            map = CollectionUtils.conditionalPutAndCreateTreeMapIfAbsentAndQuote(map, AnnotationUtils.isSet(fAnnotation.jvmArgsAppend()),  "jvmArgsAppend", fAnnotation.jvmArgsAppend());
+            map = CollectionUtils.conditionalPutAndCreateTreeMapIfAbsentAndQuote(map, AnnotationUtils.isSet(fAnnotation.jvmArgsPrepend()), "jvmArgsPrepend", fAnnotation.jvmArgsPrepend());
             map = CollectionUtils.conditionalPutAndCreateTreeMapIfAbsent(map, fAnnotation.value() != 1, "value", Integer.toString(fAnnotation.value()));
             map = CollectionUtils.conditionalPutAndCreateTreeMapIfAbsent(map, fAnnotation.warmups() > 0, "warmups", Integer.toString(fAnnotation.warmups()));
         }
@@ -661,18 +662,6 @@
             if (map == null || map.isEmpty()) {
                 return "@" + Fork.class.getSimpleName();
             }
-            if (map.containsKey("value")) {
-                map.put("value", map.get("value"));
-            }
-            if (map.containsKey("jvmArgs")) {
-                map.put("jvmArgs", "\"" + map.get("jvmArgs") + "\"");
-            }
-            if (map.containsKey("jvmArgsAppend")) {
-                map.put("jvmArgsAppend", "\"" + map.get("jvmArgsAppend") + "\"");
-            }
-            if (map.containsKey("jvmArgsPrepend")) {
-                map.put("jvmArgsPrepend", "\"" + map.get("jvmArgsPrepend") + "\"");
-            }
             return "@" + Fork.class.getSimpleName() + "(" + annotationMapToString(map) + ")";
         }
         return null;
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Wed Jun 05 17:18:24 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Mon Jun 10 18:24:29 2013 +0400
@@ -36,6 +36,7 @@
 import org.openjdk.jmh.runner.parameters.MicroBenchmarkParameters;
 import org.openjdk.jmh.runner.parameters.MicroBenchmarkParametersFactory;
 import org.openjdk.jmh.runner.parameters.ThreadIterationParams;
+import org.openjdk.jmh.util.AnnotationUtils;
 import org.openjdk.jmh.util.ClassUtils;
 import org.openjdk.jmh.util.InputStreamDrainer;
 
@@ -347,17 +348,17 @@
         Fork forkAnnotation = benchmarkMethod.getAnnotation(Fork.class);
 
         String annJvmArgs = null;
-        if (forkAnnotation != null && !forkAnnotation.jvmArgs().equals(Fork.PARAM_NOT_SET)) {
+        if (forkAnnotation != null && AnnotationUtils.isSet(forkAnnotation.jvmArgs())) {
             annJvmArgs = forkAnnotation.jvmArgs().trim();
         }
 
         String annJvmArgsAppend = null;
-        if (forkAnnotation != null && !forkAnnotation.jvmArgsAppend().equals(Fork.PARAM_NOT_SET)) {
+        if (forkAnnotation != null && AnnotationUtils.isSet(forkAnnotation.jvmArgsAppend())) {
             annJvmArgsAppend = forkAnnotation.jvmArgsAppend().trim();
         }
 
         String annJvmArgsPrepend = null;
-        if (forkAnnotation != null && !forkAnnotation.jvmArgsPrepend().equals(Fork.PARAM_NOT_SET)) {
+        if (forkAnnotation != null && AnnotationUtils.isSet(forkAnnotation.jvmArgsPrepend())) {
             annJvmArgsPrepend = forkAnnotation.jvmArgsPrepend().trim();
         }
 
@@ -365,7 +366,7 @@
 
         int forkCount = decideForks(options.getForkCount(), benchForks(benchmark));
         int warmupForkCount = forkAnnotation != null ? forkAnnotation.warmups() : 0;
-        if( warmupForkCount>0) {
+        if (warmupForkCount > 0) {
             String[] warmupForkCheat = concat(commandString, new String[]{"-wi", "1", "-i", "0"});
             if (warmupForkCount == 1) {
                 outputHandler.verbosePrintln("Warmup forking using command: " + Arrays.toString(warmupForkCheat));
--- a/jmh-core/src/main/java/org/openjdk/jmh/util/internal/CollectionUtils.java	Wed Jun 05 17:18:24 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/util/internal/CollectionUtils.java	Mon Jun 10 18:24:29 2013 +0400
@@ -24,6 +24,8 @@
  */
 package org.openjdk.jmh.util.internal;
 
+import org.openjdk.jmh.util.AnnotationUtils;
+
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -83,6 +85,18 @@
         return map;
     }
 
+    public static <K> Map<K, String> conditionalPutAndCreateTreeMapIfAbsentAndQuote(Map<K, String> map, boolean cond, K key, String value) {
+        if (cond) {
+            if (map == null) {
+                map = new TreeMap<K, String>();
+                map.put(key, "\"" + value + "\"");
+            } else if (!map.containsKey(key)) {
+                map.put(key, "\"" + value + "\"");
+            }
+        }
+        return map;
+    }
+
     public static <T> List<T> addIfNotNull(List<T> list, T value) {
         if (value != null) {
             list.add(value);