changeset 99:7385f6cf96be

@Thread(0) annotation cause incorrect microbenchmark generation Summary: Handle the zero corner case. Contributed-by: Sergey Kuksenko <sergey.kuksenko@oracle.com>
author shade
date Sat, 06 Jul 2013 23:17:38 +0400
parents 347e6fa7d172
children 1f2690d966ec
files jmh-core-it/src/test/java/org/openjdk/jmh/it/threads/OneThreadCountTest.java jmh-core-it/src/test/java/org/openjdk/jmh/it/threads/TwoThreadCountTest.java jmh-core-it/src/test/java/org/openjdk/jmh/it/threads/ZeroThreadCountTest.java jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java
diffstat 4 files changed, 266 insertions(+), 5 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/OneThreadCountTest.java	Sat Jul 06 23:17:38 2013 +0400
@@ -0,0 +1,87 @@
+/*
+ * 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 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 OneThreadCountTest {
+
+    private Set<Thread> threads = Collections.synchronizedSet(new HashSet<Thread>());
+
+    @TearDown(Level.Iteration)
+    public void tearDown() {
+        Assert.assertEquals("amount of threads should be 1", threads.size(), 1);
+    }
+
+    @GenerateMicroBenchmark
+    @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+    @Warmup(iterations = 0)
+    @Threads(1)
+    public void test1() {
+        threads.add(Thread.currentThread());
+    }
+
+    @GenerateMicroBenchmark
+    @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+    @Warmup(iterations = 0)
+    public void test2() {
+        threads.add(Thread.currentThread());
+    }
+
+    @Test
+    public void invoke1() {
+        Main.testMain(Fixtures.getTestMask(this.getClass())+".*test1" + " -foe");
+    }
+
+    @Test
+    public void invoke2() {
+        Main.testMain(Fixtures.getTestMask(this.getClass())+ ".*test2" + " -foe -t 1");
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/threads/TwoThreadCountTest.java	Sat Jul 06 23:17:38 2013 +0400
@@ -0,0 +1,87 @@
+/*
+ * 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 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 TwoThreadCountTest {
+
+    private Set<Thread> threads = Collections.synchronizedSet(new HashSet<Thread>());
+
+    @TearDown(Level.Iteration)
+    public void tearDown() {
+        Assert.assertEquals("amount of threads should be 2", threads.size(), 2);
+    }
+
+    @GenerateMicroBenchmark
+    @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+    @Warmup(iterations = 0)
+    @Threads(2)
+    public void test1() {
+        threads.add(Thread.currentThread());
+    }
+
+    @GenerateMicroBenchmark
+    @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+    @Warmup(iterations = 0)
+    public void test2() {
+        threads.add(Thread.currentThread());
+    }
+
+    @Test
+    public void invoke1() {
+        Main.testMain(Fixtures.getTestMask(this.getClass())+".*test1" + " -foe");
+    }
+
+    @Test
+    public void invoke2() {
+        Main.testMain(Fixtures.getTestMask(this.getClass())+ ".*test2" + " -foe -t 2");
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/threads/ZeroThreadCountTest.java	Sat Jul 06 23:17:38 2013 +0400
@@ -0,0 +1,87 @@
+/*
+ * 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 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());
+    }
+
+    @GenerateMicroBenchmark
+    @Measurement(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+    @Warmup(iterations = 0)
+    public void test2() {
+        threads.add(Thread.currentThread());
+    }
+
+    @Test
+    public void invoke1() {
+        Main.testMain(Fixtures.getTestMask(this.getClass())+".*test1" + " -foe");
+    }
+
+    @Test
+    public void invoke2() {
+        Main.testMain(Fixtures.getTestMask(this.getClass())+ ".*test2" + " -foe -t 0");
+    }
+
+}
--- a/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Mon Jul 01 12:57:43 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Sat Jul 06 23:17:38 2013 +0400
@@ -706,7 +706,7 @@
         for (Element method : methodGroup.methods()) {
 
             // determine the sibling bounds
-            int threads = methodGroup.getMethodThreads(method);
+            int threads = Math.max(1, methodGroup.getMethodThreads(method));
             int loId = threadTally;
             int hiId = threadTally + threads;
             threadTally = hiId;
@@ -795,7 +795,7 @@
         for (Element method : methodGroup.methods()) {
 
             // determine the sibling bounds
-            int threads = methodGroup.getMethodThreads(method);
+            int threads = Math.max(1, methodGroup.getMethodThreads(method));
             int loId = threadTally;
             int hiId = threadTally + threads;
             threadTally = hiId;
@@ -880,7 +880,7 @@
         writer.println(ident(2) + "    threadId_inited = true;");
         writer.println(ident(2) + "}");
 
-        writer.println(ident(2) + "int groupThreadCount = " + methodGroup.getTotalThreadCount() + ";");
+        writer.println(ident(2) + "int groupThreadCount = " + Math.max(1, methodGroup.getTotalThreadCount()) + ";");
         writer.println(ident(2) + "int groupId = threadId / groupThreadCount;");
         writer.println(ident(2) + "int siblingId = threadId % groupThreadCount;");
         writer.println();
@@ -905,7 +905,7 @@
         int threadTally = 0;
         for (Element method : methodGroup.methods()) {
             // determine the sibling bounds
-            int threads = methodGroup.getMethodThreads(method);
+            int threads = Math.max(1, methodGroup.getMethodThreads(method));
             int loId = threadTally;
             int hiId = threadTally + threads;
             threadTally = hiId;
@@ -1010,7 +1010,7 @@
         for (Element method : methodGroup.methods()) {
 
             // determine the sibling bounds
-            int threads = methodGroup.getMethodThreads(method);
+            int threads = Math.max(1, methodGroup.getMethodThreads(method));
             int loId = threadTally;
             int hiId = threadTally + threads;
             threadTally = hiId;