changeset 409:433a268f4a26

@Param should be able to accept blank values, and fail gracefully if parameter is not set.
author shade
date Tue, 18 Feb 2014 19:26:04 +0400
parents 5740a1770694
children e1a5e5460654
files jmh-core-it/src/test/java/org/openjdk/jmh/it/compile/params/ParamBlankTest.java jmh-core-it/src/test/java/org/openjdk/jmh/it/compile/params/ParamTest.java jmh-core-it/src/test/java/org/openjdk/jmh/it/compile/params/ParamValuedTest.java jmh-core/src/main/java/org/openjdk/jmh/annotations/Param.java jmh-core/src/main/java/org/openjdk/jmh/logic/InfraControl.java jmh-core/src/main/java/org/openjdk/jmh/processor/internal/MethodGroup.java jmh-core/src/main/java/org/openjdk/jmh/processor/internal/ParamValidationProcessor.java jmh-core/src/main/java/org/openjdk/jmh/util/internal/Optional.java
diffstat 8 files changed, 221 insertions(+), 106 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/compile/params/ParamBlankTest.java	Tue Feb 18 19:26:04 2014 +0400
@@ -0,0 +1,91 @@
+/*
+ * 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.compile.params;
+
+import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+
+@State(Scope.Benchmark)
+public class ParamBlankTest {
+
+    @Param
+    public boolean param_boolean;
+
+    @Param
+    public byte param_byte;
+
+    @Param
+    public short param_short;
+
+    @Param
+    public char param_char;
+
+    @Param
+    public int param_int;
+
+    @Param
+    public float param_float;
+
+    @Param
+    public long param_long;
+
+    @Param
+    public double param_double;
+
+    @Param
+    public Boolean param_Boolean;
+
+    @Param
+    public Byte param_Byte;
+
+    @Param
+    public Short param_Short;
+
+    @Param
+    public Character param_Char;
+
+    @Param
+    public Integer param_Int;
+
+    @Param
+    public Float param_Float;
+
+    @Param
+    public Long param_Long;
+
+    @Param
+    public Double param_Double;
+
+    @Param
+    public String param_String;
+
+    @GenerateMicroBenchmark
+    public void test() {
+
+    }
+
+}
--- a/jmh-core-it/src/test/java/org/openjdk/jmh/it/compile/params/ParamTest.java	Tue Feb 18 18:00:36 2014 +0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,91 +0,0 @@
-/*
- * 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.compile.params;
-
-import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
-import org.openjdk.jmh.annotations.Param;
-import org.openjdk.jmh.annotations.Scope;
-import org.openjdk.jmh.annotations.State;
-
-@State(Scope.Benchmark)
-public class ParamTest {
-
-    @Param("true")
-    public boolean param_boolean;
-
-    @Param("0")
-    public byte param_byte;
-
-    @Param("0")
-    public short param_short;
-
-    @Param("0")
-    public char param_char;
-
-    @Param("0")
-    public int param_int;
-
-    @Param("0")
-    public float param_float;
-
-    @Param("0")
-    public long param_long;
-
-    @Param("0")
-    public double param_double;
-
-    @Param("true")
-    public Boolean param_Boolean;
-
-    @Param("0")
-    public Byte param_Byte;
-
-    @Param("0")
-    public Short param_Short;
-
-    @Param("0")
-    public Character param_Char;
-
-    @Param("0")
-    public Integer param_Int;
-
-    @Param("0")
-    public Float param_Float;
-
-    @Param("0")
-    public Long param_Long;
-
-    @Param("0")
-    public Double param_Double;
-
-    @Param("null")
-    public String param_String;
-
-    @GenerateMicroBenchmark
-    public void test() {
-
-    }
-
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/compile/params/ParamValuedTest.java	Tue Feb 18 19:26:04 2014 +0400
@@ -0,0 +1,91 @@
+/*
+ * 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.compile.params;
+
+import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+
+@State(Scope.Benchmark)
+public class ParamValuedTest {
+
+    @Param("true")
+    public boolean param_boolean;
+
+    @Param("0")
+    public byte param_byte;
+
+    @Param("0")
+    public short param_short;
+
+    @Param("0")
+    public char param_char;
+
+    @Param("0")
+    public int param_int;
+
+    @Param("0")
+    public float param_float;
+
+    @Param("0")
+    public long param_long;
+
+    @Param("0")
+    public double param_double;
+
+    @Param("true")
+    public Boolean param_Boolean;
+
+    @Param("0")
+    public Byte param_Byte;
+
+    @Param("0")
+    public Short param_Short;
+
+    @Param("0")
+    public Character param_Char;
+
+    @Param("0")
+    public Integer param_Int;
+
+    @Param("0")
+    public Float param_Float;
+
+    @Param("0")
+    public Long param_Long;
+
+    @Param("0")
+    public Double param_Double;
+
+    @Param("null")
+    public String param_String;
+
+    @GenerateMicroBenchmark
+    public void test() {
+
+    }
+
+}
--- a/jmh-core/src/main/java/org/openjdk/jmh/annotations/Param.java	Tue Feb 18 18:00:36 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/annotations/Param.java	Tue Feb 18 19:26:04 2014 +0400
@@ -51,10 +51,12 @@
 @Inherited
 public @interface Param {
 
+    public static final String BLANK_ARGS = "blank_blank_blank_2014";
+
     /**
      * Default values for the parameter.
      * @return values sequence to follow.
      */
-    String[] value();
+    String[] value() default { BLANK_ARGS };
 
 }
--- a/jmh-core/src/main/java/org/openjdk/jmh/logic/InfraControl.java	Tue Feb 18 18:00:36 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/logic/InfraControl.java	Tue Feb 18 19:26:04 2014 +0400
@@ -226,7 +226,7 @@
 
     public String getParam(String name) {
         if (!params.containsKey(name)) {
-            throw new IllegalStateException("Querying the non-existing parameter: " + name);
+            throw new IllegalStateException("The value for the parameter \"" + name + "\" is not set.");
         }
         return params.get(name);
     }
--- a/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/MethodGroup.java	Tue Feb 18 18:00:36 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/MethodGroup.java	Tue Feb 18 19:26:04 2014 +0400
@@ -29,6 +29,7 @@
 import org.openjdk.jmh.annotations.Mode;
 import org.openjdk.jmh.annotations.OperationsPerInvocation;
 import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
 import org.openjdk.jmh.annotations.Threads;
 import org.openjdk.jmh.annotations.Warmup;
 import org.openjdk.jmh.runner.parameters.TimeValue;
@@ -253,10 +254,21 @@
     }
 
     public Optional<Map<String, String[]>> getParams() {
+        Map<String, String[]> map = new TreeMap<String, String[]>();
+
+        for (String key : params.keySet()) {
+            String[] values = params.get(key);
+            if (values.length == 1 && values[0].equalsIgnoreCase(Param.BLANK_ARGS)) {
+                map.put(key, new String[0]);
+            } else {
+                map.put(key, values);
+            }
+        }
+
         if (params.isEmpty()) {
             return Optional.none();
         } else {
-            return Optional.of(params);
+            return Optional.of(map);
         }
     }
 
--- a/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/ParamValidationProcessor.java	Tue Feb 18 18:00:36 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/ParamValidationProcessor.java	Tue Feb 18 19:26:04 2014 +0400
@@ -73,14 +73,16 @@
                 VariableElement ve = (VariableElement) element;
                 String[] values = element.getAnnotation(Param.class).value();
 
-                String type = ve.asType().toString();
-                for (String val : values) {
-                    if (!isConforming(val, type)) {
-                        processingEnv.getMessager().printMessage(Kind.ERROR,
-                                "Some @" + Param.class.getSimpleName() + " values can not be converted to target type: " +
-                                "\"" + val + "\" can not be converted to " + type,
-                                element
-                        );
+                if (values.length >= 1 && !values[0].equalsIgnoreCase(Param.BLANK_ARGS)) {
+                    String type = ve.asType().toString();
+                    for (String val : values) {
+                        if (!isConforming(val, type)) {
+                            processingEnv.getMessager().printMessage(Kind.ERROR,
+                                    "Some @" + Param.class.getSimpleName() + " values can not be converted to target type: " +
+                                    "\"" + val + "\" can not be converted to " + type,
+                                    element
+                            );
+                        }
                     }
                 }
 
--- a/jmh-core/src/main/java/org/openjdk/jmh/util/internal/Optional.java	Tue Feb 18 18:00:36 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/util/internal/Optional.java	Tue Feb 18 19:26:04 2014 +0400
@@ -192,7 +192,11 @@
             String[] pairs = s.split("===PAIR-SEP===");
             for (String pair : pairs) {
                 String[] kv = pair.split("===SEP-K===");
-                map.put(kv[0], kv[1].split("===SEP-V==="));
+                if (kv[1].equalsIgnoreCase("===EMPTY===")) {
+                    map.put(kv[0], new String[0]);
+                } else {
+                    map.put(kv[0], kv[1].split("===SEP-V==="));
+                }
             }
             return map;
         }
@@ -205,9 +209,13 @@
             for (String s : src.keySet()) {
                 sb.append(s);
                 sb.append("===SEP-K===");
-                for (String v : src.get(s)) {
-                    sb.append(v);
-                    sb.append("===SEP-V===");
+                if (src.get(s).length == 0) {
+                    sb.append("===EMPTY===");
+                } else {
+                    for (String v : src.get(s)) {
+                        sb.append(v);
+                        sb.append("===SEP-V===");
+                    }
                 }
                 sb.append("===PAIR-SEP===");
             }