changeset 411:b396331bc871

jvmArgs should not replace jvmArgsAppend/Prepend
author shade
date Wed, 19 Feb 2014 18:22:23 +0400
parents e1a5e5460654
children 2af6736ccb7b
files jmh-core-it/src/test/java/org/openjdk/jmh/it/fork/ForkedJvmAppendPrependArgs1_Test.java jmh-core-it/src/test/java/org/openjdk/jmh/it/fork/ForkedJvmAppendPrependArgs2_Test.java jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java jmh-core/src/main/java/org/openjdk/jmh/runner/options/ChainedOptionsBuilder.java jmh-core/src/main/java/org/openjdk/jmh/runner/options/CommandLineOptions.java jmh-core/src/main/java/org/openjdk/jmh/runner/options/Options.java jmh-core/src/main/java/org/openjdk/jmh/runner/options/OptionsBuilder.java jmh-core/src/test/java/org/openjdk/jmh/runner/options/TestOptions.java jmh-core/src/test/java/org/openjdk/jmh/runner/options/TestParentOptions.java
diffstat 9 files changed, 216 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core-it/src/test/java/org/openjdk/jmh/it/fork/ForkedJvmAppendPrependArgs1_Test.java	Wed Feb 19 15:50:56 2014 +0400
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/fork/ForkedJvmAppendPrependArgs1_Test.java	Wed Feb 19 18:22:23 2014 +0400
@@ -50,12 +50,12 @@
     @GenerateMicroBenchmark
     @Warmup(iterations = 0)
     @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
-    @Fork(jvmArgs = "-Dreplaced", jvmArgsAppend = "-Dappended", jvmArgsPrepend = "-Dprepended")
+    @Fork(jvmArgs = "-Dmiddle", 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.assertNotNull(System.getProperty("middle"));
+        Assert.assertNotNull(System.getProperty("appended"));
+        Assert.assertNotNull(System.getProperty("prepended"));
     }
 
     @GenerateMicroBenchmark
@@ -64,7 +64,7 @@
     @Fork(jvmArgsAppend = "-Dappended", jvmArgsPrepend = "-Dprepended")
     public void test2() {
         Fixtures.work();
-        Assert.assertNull(System.getProperty("replaced"));
+        Assert.assertNull(System.getProperty("middle"));
         Assert.assertNotNull(System.getProperty("appended"));
         Assert.assertNotNull(System.getProperty("prepended"));
     }
@@ -75,7 +75,7 @@
     @Fork(jvmArgsPrepend = "-Dprepended")
     public void test3() {
         Fixtures.work();
-        Assert.assertNull(System.getProperty("replaced"));
+        Assert.assertNull(System.getProperty("middle"));
         Assert.assertNull(System.getProperty("appended"));
         Assert.assertNotNull(System.getProperty("prepended"));
     }
@@ -86,7 +86,7 @@
     @Fork(jvmArgsAppend = "-Dappended")
     public void test4() {
         Fixtures.work();
-        Assert.assertNull(System.getProperty("replaced"));
+        Assert.assertNull(System.getProperty("middle"));
         Assert.assertNotNull(System.getProperty("appended"));
         Assert.assertNull(System.getProperty("prepended"));
     }
--- a/jmh-core-it/src/test/java/org/openjdk/jmh/it/fork/ForkedJvmAppendPrependArgs2_Test.java	Wed Feb 19 15:50:56 2014 +0400
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/fork/ForkedJvmAppendPrependArgs2_Test.java	Wed Feb 19 18:22:23 2014 +0400
@@ -51,12 +51,12 @@
     @GenerateMicroBenchmark
     @Warmup(iterations = 0)
     @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
-    @Fork(jvmArgs = "-Dreplaced", jvmArgsAppend = "-Dappended", jvmArgsPrepend = "-Dprepended")
+    @Fork(jvmArgs = "-Dmiddle", 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.assertNotNull(System.getProperty("middle"));
+        Assert.assertNotNull(System.getProperty("appended"));
+        Assert.assertNotNull(System.getProperty("prepended"));
         Assert.assertNull(System.getProperty("appendedUp"));
         Assert.assertNull(System.getProperty("prependedUp"));
     }
@@ -67,7 +67,7 @@
     @Fork(jvmArgsAppend = "-Dappended", jvmArgsPrepend = "-Dprepended")
     public void test2() {
         Fixtures.work();
-        Assert.assertNull(System.getProperty("replaced"));
+        Assert.assertNull(System.getProperty("middle"));
         Assert.assertNotNull(System.getProperty("appended"));
         Assert.assertNotNull(System.getProperty("prepended"));
         Assert.assertNull(System.getProperty("appendedUp"));
@@ -80,7 +80,7 @@
     @Fork(jvmArgsPrepend = "-Dprepended")
     public void test3() {
         Fixtures.work();
-        Assert.assertNull(System.getProperty("replaced"));
+        Assert.assertNull(System.getProperty("middle"));
         Assert.assertNull(System.getProperty("appended"));
         Assert.assertNotNull(System.getProperty("prepended"));
         Assert.assertNull(System.getProperty("appendedUp"));
@@ -92,7 +92,7 @@
     @Fork(jvmArgsAppend = "-Dappended")
     public void test4() {
         Fixtures.work();
-        Assert.assertNull(System.getProperty("replaced"));
+        Assert.assertNull(System.getProperty("middle"));
         Assert.assertNotNull(System.getProperty("appended"));
         Assert.assertNull(System.getProperty("prepended"));
         Assert.assertNull(System.getProperty("appendedUp"));
@@ -105,7 +105,7 @@
     @Fork
     public void test5() {
         Fixtures.work();
-        Assert.assertNull(System.getProperty("replaced"));
+        Assert.assertNull(System.getProperty("middle"));
         Assert.assertNull(System.getProperty("appended"));
         Assert.assertNull(System.getProperty("prepended"));
         Assert.assertNull(System.getProperty("appendedUp"));
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Wed Feb 19 15:50:56 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Wed Feb 19 18:22:23 2014 +0400
@@ -364,7 +364,7 @@
             BenchmarkRecord benchmark = actionPlan.getMeasurementActions().get(0).getBenchmark();
 
             String[] commandString = getSeparateExecutionCommand(benchmark, server.getHost(), server.getPort());
-            String opts = merge(options.getJvmArgs().orElse(benchmark.getJvmArgs().orElse(getDefaultJvmArgs(benchmark))));
+            String opts = merge(getJvmArgs(benchmark));
             if (opts.trim().isEmpty()) {
                 opts = "<none>";
             }
@@ -478,7 +478,7 @@
         command.add(options.getJvm().orElse(getDefaultJvm()));
 
         // use supplied jvm args, if given
-        command.addAll(options.getJvmArgs().orElse(benchmark.getJvmArgs().orElse(getDefaultJvmArgs(benchmark))));
+        command.addAll(getJvmArgs(benchmark));
 
         // add any compiler oracle hints
         command.add("-XX:CompileCommandFile=" + CompilerHints.hintsFile());
@@ -516,11 +516,11 @@
         return javaExecutable.toString();
     }
 
-    private Collection<String> getDefaultJvmArgs(BenchmarkRecord benchmark) {
+    private Collection<String> getJvmArgs(BenchmarkRecord benchmark) {
         Collection<String> res = new ArrayList<String>();
-        res.addAll(benchmark.getJvmArgsPrepend().orElse(Collections.<String>emptyList()));
-        res.addAll(ManagementFactory.getRuntimeMXBean().getInputArguments());
-        res.addAll(benchmark.getJvmArgsAppend().orElse(Collections.<String>emptyList()));
+        res.addAll(options.getJvmArgsPrepend().orElse(benchmark.getJvmArgsPrepend().orElse(Collections.<String>emptyList())));
+        res.addAll(options.getJvmArgs().orElse(benchmark.getJvmArgs().orElse(ManagementFactory.getRuntimeMXBean().getInputArguments())));
+        res.addAll(options.getJvmArgsAppend().orElse(benchmark.getJvmArgsAppend().orElse(Collections.<String>emptyList())));
         return res;
     }
 
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/options/ChainedOptionsBuilder.java	Wed Feb 19 15:50:56 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/options/ChainedOptionsBuilder.java	Wed Feb 19 18:22:23 2014 +0400
@@ -240,6 +240,26 @@
     ChainedOptionsBuilder jvmArgs(String... value);
 
     /**
+     * Append forked JVM arguments:
+     * These options go after other options.
+     * (can be used multiple times)
+     *
+     * @param value arguments to add to the run
+     * @return builder
+     */
+    ChainedOptionsBuilder jvmArgsAppend(String... value);
+
+    /**
+     * Prepend forked JVM arguments:
+     * These options go before any other options.
+     * (can be used multiple times)
+     *
+     * @param value arguments to add to the run
+     * @return builder
+     */
+    ChainedOptionsBuilder jvmArgsPrepend(String... value);
+
+    /**
      * Autodetect forked JVM arguments from the parent VM.
      * @return builder
      */
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/options/CommandLineOptions.java	Wed Feb 19 15:50:56 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/options/CommandLineOptions.java	Wed Feb 19 18:22:23 2014 +0400
@@ -81,6 +81,8 @@
     private final Optional<ResultFormatType> resultFormat;
     private final Optional<String> jvm;
     private final Optional<Collection<String>> jvmArgs;
+    private final Optional<Collection<String>> jvmArgsAppend;
+    private final Optional<Collection<String>> jvmArgsPrepend;
     private final List<String> excludes = new ArrayList<String>();
     private final Optional<WarmupMode> warmupMode;
     private final List<String> warmupMicros = new ArrayList<String>();
@@ -167,6 +169,12 @@
         OptionSpec<String> optJvmArgs = parser.accepts("jvmArgs", "Custom JVM args to use when forking.")
                 .withRequiredArg().ofType(String.class).describedAs("string");
 
+        OptionSpec<String> optJvmArgsAppend = parser.accepts("jvmArgsAppend", "Custom JVM args to use when forking (append these)")
+                .withRequiredArg().ofType(String.class).describedAs("string");
+
+        OptionSpec<String> optJvmArgsPrepend = parser.accepts("jvmArgsPrepend", "Custom JVM args to use when forking (prepend these)")
+                .withRequiredArg().ofType(String.class).describedAs("string");
+
         OptionSpec<String> optTU = parser.accepts("tu", "Output time unit. Available time units are: [m, s, ms, us, ns].")
                 .withRequiredArg().ofType(String.class).describedAs("TU");
 
@@ -394,6 +402,18 @@
                 jvmArgs = Optional.none();
             }
 
+            if (set.hasArgument(optJvmArgsAppend)) {
+                jvmArgsAppend = Optional.<Collection<String>>of(Arrays.asList(optJvmArgsAppend.value(set).trim().split("[ ]+")));
+            } else {
+                jvmArgsAppend = Optional.none();
+            }
+
+            if (set.hasArgument(optJvmArgsPrepend)) {
+                jvmArgsPrepend = Optional.<Collection<String>>of(Arrays.asList(optJvmArgsPrepend.value(set).trim().split("[ ]+")));
+            } else {
+                jvmArgsPrepend = Optional.none();
+            }
+
             if (set.hasArgument(optParams)) {
                 for (String p : optParams.values(set)) {
                     String[] keys = p.split("=");
@@ -523,6 +543,16 @@
     }
 
     @Override
+    public Optional<Collection<String>> getJvmArgsAppend() {
+        return jvmArgsAppend;
+    }
+
+    @Override
+    public Optional<Collection<String>> getJvmArgsPrepend() {
+        return jvmArgsPrepend;
+    }
+
+    @Override
     public Optional<Collection<String>> getParameter(String name) {
         Collection<String> list = params.get(name);
         if (list == null || list.isEmpty()){
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/options/Options.java	Wed Feb 19 15:50:56 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/options/Options.java	Wed Feb 19 18:22:23 2014 +0400
@@ -194,6 +194,20 @@
     Optional<Collection<String>> getJvmArgs();
 
     /**
+     * JVM parameters to use with forks (these options will be appended
+     * after any other JVM option)
+     * @return JVM parameters
+     */
+    Optional<Collection<String>> getJvmArgsAppend();
+
+    /**
+     * JVM parameters to use with forks (these options will be prepended
+     * before any other JVM option)
+     * @return JVM parameters
+     */
+    Optional<Collection<String>> getJvmArgsPrepend();
+
+    /**
      * The overridden value of the parameter.
      * @param name parameter name
      * @return parameter
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/options/OptionsBuilder.java	Wed Feb 19 15:50:56 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/options/OptionsBuilder.java	Wed Feb 19 18:22:23 2014 +0400
@@ -555,7 +555,7 @@
             jvmArgs.get().addAll(Arrays.asList(value));
         } else {
             Collection<String> vals = new ArrayList<String>(Arrays.asList(value));
-            jvmArgs = Optional.<Collection<String>>of(vals);
+            jvmArgs = Optional.of(vals);
         }
         return this;
     }
@@ -576,6 +576,70 @@
         }
     }
 
+    // ---------------------------------------------------------------------------
+
+    private Optional<Collection<String>> jvmArgsAppend = Optional.none();
+
+    @Override
+    public ChainedOptionsBuilder jvmArgsAppend(String... value) {
+        if (jvmArgsAppend.hasValue()) {
+            jvmArgsAppend.get().addAll(Arrays.asList(value));
+        } else {
+            Collection<String> vals = new ArrayList<String>(Arrays.asList(value));
+            jvmArgsAppend = Optional.of(vals);
+        }
+        return this;
+    }
+
+    @Override
+    public Optional<Collection<String>> getJvmArgsAppend() {
+        Collection<String> result = new ArrayList<String>();
+
+        if (otherOptions != null) {
+            result.addAll(otherOptions.getJvmArgsAppend().orElse(Collections.<String>emptyList()));
+        }
+        result.addAll(jvmArgsAppend.orElse(Collections.<String>emptyList()));
+
+        if (result.isEmpty()) {
+            return Optional.none();
+        } else {
+            return Optional.of(result);
+        }
+    }
+
+    // ---------------------------------------------------------------------------
+
+    private Optional<Collection<String>> jvmArgsPrepend = Optional.none();
+
+    @Override
+    public ChainedOptionsBuilder jvmArgsPrepend(String... value) {
+        if (jvmArgsPrepend.hasValue()) {
+            jvmArgsPrepend.get().addAll(Arrays.asList(value));
+        } else {
+            Collection<String> vals = new ArrayList<String>(Arrays.asList(value));
+            jvmArgsPrepend = Optional.of(vals);
+        }
+        return this;
+    }
+
+    @Override
+    public Optional<Collection<String>> getJvmArgsPrepend() {
+        Collection<String> result = new ArrayList<String>();
+
+        if (otherOptions != null) {
+            result.addAll(otherOptions.getJvmArgsPrepend().orElse(Collections.<String>emptyList()));
+        }
+        result.addAll(jvmArgsPrepend.orElse(Collections.<String>emptyList()));
+
+        if (result.isEmpty()) {
+            return Optional.none();
+        } else {
+            return Optional.of(result);
+        }
+    }
+
+    // ---------------------------------------------------------------------------
+
     @Override
     public ChainedOptionsBuilder detectJvmArgs() {
         List<String> inputArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
--- a/jmh-core/src/test/java/org/openjdk/jmh/runner/options/TestOptions.java	Wed Feb 19 15:50:56 2014 +0400
+++ b/jmh-core/src/test/java/org/openjdk/jmh/runner/options/TestOptions.java	Wed Feb 19 18:22:23 2014 +0400
@@ -457,6 +457,30 @@
     }
 
     @Test
+    public void testJvmArgsAppend() throws Exception {
+        CommandLineOptions cmdLine = new CommandLineOptions("--jvmArgsAppend", "sample.jar");
+        Options builder = new OptionsBuilder().jvmArgsAppend("sample.jar").build();
+        Assert.assertEquals(builder.getJvmArgsAppend(), cmdLine.getJvmArgsAppend());
+    }
+
+    @Test
+    public void testJvmArgsAppend_Default() throws Exception {
+        Assert.assertEquals(EMPTY_BUILDER.getJvmArgsAppend(), EMPTY_CMDLINE.getJvmArgsAppend());
+    }
+
+    @Test
+    public void testJvmArgsPrepend() throws Exception {
+        CommandLineOptions cmdLine = new CommandLineOptions("--jvmArgsPrepend", "sample.jar");
+        Options builder = new OptionsBuilder().jvmArgsPrepend("sample.jar").build();
+        Assert.assertEquals(builder.getJvmArgsPrepend(), cmdLine.getJvmArgsPrepend());
+    }
+
+    @Test
+    public void testJvmArgsPrepend_Default() throws Exception {
+        Assert.assertEquals(EMPTY_BUILDER.getJvmArgsPrepend(), EMPTY_CMDLINE.getJvmArgsPrepend());
+    }
+
+    @Test
     public void testBatchSize() throws Exception {
         CommandLineOptions cmdLine = new CommandLineOptions("-bs", "42");
         Options builder = new OptionsBuilder().measurementBatchSize(42).build();
--- a/jmh-core/src/test/java/org/openjdk/jmh/runner/options/TestParentOptions.java	Wed Feb 19 15:50:56 2014 +0400
+++ b/jmh-core/src/test/java/org/openjdk/jmh/runner/options/TestParentOptions.java	Wed Feb 19 18:22:23 2014 +0400
@@ -207,6 +207,48 @@
     }
 
     @Test
+    public void testJVMArgsAppend_Empty() throws Exception {
+        Options parent = new OptionsBuilder().build();
+        Options builder = new OptionsBuilder().parent(parent).build();
+        Assert.assertFalse(builder.getJvmArgsAppend().hasValue());
+    }
+
+    @Test
+    public void testJVMArgsAppend_Parent() throws Exception {
+        Options parent = new OptionsBuilder().jvmArgsAppend("opt1", "opt2").build();
+        Options builder = new OptionsBuilder().parent(parent).build();
+        Assert.assertEquals(Arrays.asList("opt1", "opt2"), builder.getJvmArgsAppend().get());
+    }
+
+    @Test
+    public void testJVMArgsAppend_Merge() throws Exception {
+        Options parent = new OptionsBuilder().jvmArgsAppend("opt1", "opt2").build();
+        Options builder = new OptionsBuilder().parent(parent).jvmArgsAppend("opt3", "opt4").build();
+        Assert.assertEquals(Arrays.asList("opt1", "opt2", "opt3", "opt4"), builder.getJvmArgsAppend().get());
+    }
+
+    @Test
+    public void testJVMArgsPrepend_Empty() throws Exception {
+        Options parent = new OptionsBuilder().build();
+        Options builder = new OptionsBuilder().parent(parent).build();
+        Assert.assertFalse(builder.getJvmArgsPrepend().hasValue());
+    }
+
+    @Test
+    public void testJVMArgsPrepend_Parent() throws Exception {
+        Options parent = new OptionsBuilder().jvmArgsPrepend("opt1", "opt2").build();
+        Options builder = new OptionsBuilder().parent(parent).build();
+        Assert.assertEquals(Arrays.asList("opt1", "opt2"), builder.getJvmArgsPrepend().get());
+    }
+
+    @Test
+    public void testJVMArgsPrepend_Merge() throws Exception {
+        Options parent = new OptionsBuilder().jvmArgsPrepend("opt1", "opt2").build();
+        Options builder = new OptionsBuilder().parent(parent).jvmArgsPrepend("opt3", "opt4").build();
+        Assert.assertEquals(Arrays.asList("opt1", "opt2", "opt3", "opt4"), builder.getJvmArgsPrepend().get());
+    }
+
+    @Test
     public void testOutput_Empty() throws Exception {
         Options parent = new OptionsBuilder().build();
         Options builder = new OptionsBuilder().parent(parent).build();