changeset 185:32a5a7addff6

Make Threads.MAX magic value; "0" is not special value anymore.
author shade
date Thu, 26 Sep 2013 19:12:01 +0400
parents 07d522190f84
children 78de3267fb5a
files jmh-core-it/src/test/java/org/openjdk/jmh/it/threads/MaxThreadCountTest.java jmh-core-it/src/test/java/org/openjdk/jmh/it/threads/ZeroThreadCountTest.java jmh-core/src/main/java/org/openjdk/jmh/annotations/Threads.java jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java jmh-core/src/main/java/org/openjdk/jmh/runner/options/CommandLineOptions.java jmh-core/src/main/java/org/openjdk/jmh/runner/options/OptionsBuilder.java jmh-core/src/main/java/org/openjdk/jmh/runner/options/handlers/ThreadsOptionHandler.java jmh-core/src/main/java/org/openjdk/jmh/runner/parameters/BenchmarkParams.java jmh-core/src/test/java/org/openjdk/jmh/runner/options/TestOptions.java
diffstat 9 files changed, 129 insertions(+), 121 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/threads/MaxThreadCountTest.java	Thu Sep 26 19:12:01 2013 +0400
@@ -0,0 +1,111 @@
+/*
+ * 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.threads;
+
+import junit.framework.Assert;
+import org.junit.Test;
+import org.openjdk.jmh.Main;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
+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.State;
+import org.openjdk.jmh.annotations.TearDown;
+import org.openjdk.jmh.annotations.Threads;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.it.Fixtures;
+import org.openjdk.jmh.runner.Runner;
+import org.openjdk.jmh.runner.RunnerException;
+import org.openjdk.jmh.runner.options.Options;
+import org.openjdk.jmh.runner.options.OptionsBuilder;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+/**
+ *
+ * @author Sergey Kuksenko (sergey.kuksenko@oracle.com)
+ */
+@BenchmarkMode(Mode.All)
+@State(Scope.Benchmark)
+public class MaxThreadCountTest {
+
+    private Set<Thread> threads = Collections.synchronizedSet(new HashSet<Thread>());
+
+    @TearDown(Level.Iteration)
+    public void tearDown() {
+        Assert.assertEquals("amount of threads should be Runtime.getRuntime().availableProcessors()", threads.size(), Runtime.getRuntime().availableProcessors());
+    }
+
+    @GenerateMicroBenchmark
+    @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+    @Warmup(iterations = 0)
+    @Threads(Threads.MAX)
+    public void test1() {
+        threads.add(Thread.currentThread());
+        Fixtures.work();
+    }
+
+    @GenerateMicroBenchmark
+    @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+    @Warmup(iterations = 0)
+    public void test2() {
+        threads.add(Thread.currentThread());
+        Fixtures.work();
+    }
+
+    @Test
+    public void invokeCLI_1() {
+        Main.testMain(Fixtures.getTestMask(this.getClass())+".*test1" + " -foe");
+    }
+
+    @Test
+    public void invokeCLI_2() {
+        Main.testMain(Fixtures.getTestMask(this.getClass())+ ".*test2" + " -foe -t max");
+    }
+
+    @Test
+    public void invokeAPI_1() throws RunnerException {
+        Options opt = new OptionsBuilder()
+                .include(Fixtures.getTestMask(this.getClass())+".*test1")
+                .failOnError(true)
+                .build();
+        new Runner(opt).run();
+    }
+
+    @Test
+    public void invokeAPI_2() throws RunnerException {
+        Options opt = new OptionsBuilder()
+                .include(Fixtures.getTestMask(this.getClass())+".*test2")
+                .failOnError(true)
+                .threads(Threads.MAX)
+                .build();
+        new Runner(opt).run();
+    }
+}
--- a/jmh-core-it/src/test/java/org/openjdk/jmh/it/threads/ZeroThreadCountTest.java	Thu Sep 26 12:19:37 2013 +0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,111 +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.threads;
-
-import junit.framework.Assert;
-import org.junit.Test;
-import org.openjdk.jmh.Main;
-import org.openjdk.jmh.annotations.BenchmarkMode;
-import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
-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.State;
-import org.openjdk.jmh.annotations.TearDown;
-import org.openjdk.jmh.annotations.Threads;
-import org.openjdk.jmh.annotations.Warmup;
-import org.openjdk.jmh.it.Fixtures;
-import org.openjdk.jmh.runner.Runner;
-import org.openjdk.jmh.runner.RunnerException;
-import org.openjdk.jmh.runner.options.Options;
-import org.openjdk.jmh.runner.options.OptionsBuilder;
-
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Set;
-import java.util.concurrent.TimeUnit;
-
-/**
- *
- * @author Sergey Kuksenko (sergey.kuksenko@oracle.com)
- */
-@BenchmarkMode(Mode.All)
-@State(Scope.Benchmark)
-public class ZeroThreadCountTest {
-
-    private Set<Thread> threads = Collections.synchronizedSet(new HashSet<Thread>());
-
-    @TearDown(Level.Iteration)
-    public void tearDown() {
-        Assert.assertEquals("amount of threads should be Runtime.getRuntime().availableProcessors()", threads.size(), Runtime.getRuntime().availableProcessors());
-    }
-
-    @GenerateMicroBenchmark
-    @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
-    @Warmup(iterations = 0)
-    @Threads(0)
-    public void test1() {
-        threads.add(Thread.currentThread());
-        Fixtures.work();
-    }
-
-    @GenerateMicroBenchmark
-    @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
-    @Warmup(iterations = 0)
-    public void test2() {
-        threads.add(Thread.currentThread());
-        Fixtures.work();
-    }
-
-    @Test
-    public void invokeCLI_1() {
-        Main.testMain(Fixtures.getTestMask(this.getClass())+".*test1" + " -foe");
-    }
-
-    @Test
-    public void invokeCLI_2() {
-        Main.testMain(Fixtures.getTestMask(this.getClass())+ ".*test2" + " -foe -t 0");
-    }
-
-    @Test
-    public void invokeAPI_1() throws RunnerException {
-        Options opt = new OptionsBuilder()
-                .include(Fixtures.getTestMask(this.getClass())+".*test1")
-                .failOnError(true)
-                .build();
-        new Runner(opt).run();
-    }
-
-    @Test
-    public void invokeAPI_2() throws RunnerException {
-        Options opt = new OptionsBuilder()
-                .include(Fixtures.getTestMask(this.getClass())+".*test2")
-                .failOnError(true)
-                .threads(0)
-                .build();
-        new Runner(opt).run();
-    }
-}
--- a/jmh-core/src/main/java/org/openjdk/jmh/annotations/Threads.java	Thu Sep 26 12:19:37 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/annotations/Threads.java	Thu Sep 26 19:12:01 2013 +0400
@@ -42,6 +42,12 @@
 @Inherited
 public @interface Threads {
 
+    /**
+     * The magic value for MAX threads.
+     *  This means Runtime.getRuntime().availableProcessors() threads.
+     */
+    public static int MAX = -1;
+
     /** Amount of threads */
     int value() default 1;
 
--- a/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Thu Sep 26 12:19:37 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Thu Sep 26 19:12:01 2013 +0400
@@ -586,11 +586,11 @@
 
     private static int getThreads(Element method) {
         Threads tAnnotation = method.getAnnotation(Threads.class);
-        if (tAnnotation != null && tAnnotation.value() >= 0) {
+        if (tAnnotation != null && tAnnotation.value() > Integer.MIN_VALUE) {
             return tAnnotation.value();
         }
         tAnnotation = method.getEnclosingElement().getAnnotation(Threads.class);
-        if (tAnnotation != null && tAnnotation.value() >= 0) {
+        if (tAnnotation != null && tAnnotation.value() > Integer.MIN_VALUE) {
             return tAnnotation.value();
         }
         return 1;
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/options/CommandLineOptions.java	Thu Sep 26 12:19:37 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/options/CommandLineOptions.java	Thu Sep 26 19:12:01 2013 +0400
@@ -89,8 +89,8 @@
     @Option(name = "-bm", aliases = {"--mode"}, multiValued = false, metaVar = "MODE", usage = "Benchmark mode", handler = BenchmarkModeTypeOptionHandler.class)
     protected List<Mode> benchMode = null;
 
-    @Option(name = "-t", aliases = {"--threads"}, usage = "Number of threads to run the microbenchmark with. Special value \"max\" or 0 will use Runtime.availableProcessors()", handler = ThreadsOptionHandler.class)
-    protected int threads = -1;
+    @Option(name = "-t", aliases = {"--threads"}, usage = "Number of threads to run the microbenchmark with. Special value \"max\" will use Runtime.availableProcessors()", handler = ThreadsOptionHandler.class)
+    protected int threads = Integer.MIN_VALUE;
 
     @Option(name = "-si", aliases = {"--synciterations"}, usage = "Should the harness continue to load each thread with work untill all threads are done with their measured work? Default is " + Defaults.SHOULD_SYNCH_ITERATIONS, handler = BooleanOptionHandler.class)
     protected Boolean synchIterations = null; // true
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/options/OptionsBuilder.java	Thu Sep 26 12:19:37 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/options/OptionsBuilder.java	Thu Sep 26 19:12:01 2013 +0400
@@ -190,7 +190,7 @@
 
     // ---------------------------------------------------------------------------
 
-    private int threads = -1;
+    private int threads = Integer.MIN_VALUE;
 
     @Override
     public ChainedOptionsBuilder threads(int count) {
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/options/handlers/ThreadsOptionHandler.java	Thu Sep 26 12:19:37 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/options/handlers/ThreadsOptionHandler.java	Thu Sep 26 19:12:01 2013 +0400
@@ -30,6 +30,7 @@
 import org.kohsuke.args4j.spi.IntOptionHandler;
 import org.kohsuke.args4j.spi.Parameters;
 import org.kohsuke.args4j.spi.Setter;
+import org.openjdk.jmh.annotations.Threads;
 
 /**
  * OptionHandler for the -t/--threads option. Will parse the special value "max" and assign
@@ -54,7 +55,7 @@
     @Override
     public int parseArguments(Parameters params) throws CmdLineException {
         if ("max".equals(params.getParameter(0))) {
-            setter.addValue(0);
+            setter.addValue(Threads.MAX);
             return 1;
         } else {
             return super.parseArguments(params);
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/parameters/BenchmarkParams.java	Thu Sep 26 12:19:37 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/parameters/BenchmarkParams.java	Thu Sep 26 19:12:01 2013 +0400
@@ -41,7 +41,7 @@
         boolean shouldSynchIterations = getBoolean(options.getSynchIterations(), Defaults.SHOULD_SYNCH_ITERATIONS);
 
         int threads = getThreads(options, method);
-        if (threads == 0) {
+        if (threads == Threads.MAX) {
             threads = Runtime.getRuntime().availableProcessors();
         }
 
@@ -107,8 +107,9 @@
 
     private static int getThreads(Options options, Method method) {
         Threads threadsAnn = method.getAnnotation(Threads.class);
-        return getInteger(options.getThreads(), (threadsAnn == null) ? -1 : threadsAnn.value(), Defaults.THREADS);
-
+        return options.getThreads() > Integer.MIN_VALUE ?
+                options.getThreads() :
+                (threadsAnn != null ? threadsAnn.value() : Defaults.THREADS);
     }
 
     private final boolean synchIterations;
--- a/jmh-core/src/test/java/org/openjdk/jmh/runner/options/TestOptions.java	Thu Sep 26 12:19:37 2013 +0400
+++ b/jmh-core/src/test/java/org/openjdk/jmh/runner/options/TestOptions.java	Thu Sep 26 19:12:01 2013 +0400
@@ -55,7 +55,7 @@
     public void testThreadsMax() throws Exception {
         String[] argv = {"-t", "max"};
         CommandLineOptions options = getOptions(argv);
-        assertEquals(0, options.getThreads());
+        assertEquals(Threads.MAX, options.getThreads());
     }
 
     @Test