jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_30_Interrupts.java

Print this page




  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.openjdk.jmh.samples;
  26 
  27 import org.openjdk.jmh.annotations.Benchmark;
  28 import org.openjdk.jmh.annotations.BenchmarkMode;
  29 import org.openjdk.jmh.annotations.Group;
  30 import org.openjdk.jmh.annotations.Mode;

  31 import org.openjdk.jmh.annotations.Scope;
  32 import org.openjdk.jmh.annotations.Setup;
  33 import org.openjdk.jmh.annotations.State;
  34 import org.openjdk.jmh.runner.Runner;
  35 import org.openjdk.jmh.runner.RunnerException;
  36 import org.openjdk.jmh.runner.options.Options;
  37 import org.openjdk.jmh.runner.options.OptionsBuilder;

  38 
  39 import java.util.concurrent.ArrayBlockingQueue;
  40 import java.util.concurrent.BlockingQueue;

  41 
  42 @BenchmarkMode(Mode.AverageTime)

  43 @State(Scope.Group)
  44 public class JMHSample_30_Interrupts {
  45 
  46     /*
  47      * JMH can also detect when threads are stuck in the benchmarks, and try
  48      * to forcefully interrupt the benchmark thread. JMH tries to do that
  49      * when it is arguably sure it would not affect the measurement.
  50      */
  51 
  52     /*
  53      * In this example, we want to measure the simple performance characteristics
  54      * of the ArrayBlockingQueue. Unfortunately, doing that without a harness
  55      * support will deadlock one of the threads, because the executions of
  56      * take/put are not paired perfectly. Fortunately for us, both methods react
  57      * to interrupts well, and therefore we can rely on JMH to terminate the
  58      * measurement for us. JMH will notify users about the interrupt actions
  59      * nevertheless, so users can see if those interrupts affected the measurement.


  60      *
  61      * This is a variant of org.openjdk.jmh.samples.JMHSample_18_Control, but without
  62      * the explicit control objects. This example is suitable for the methods which
  63      * react to interrupts gracefully.
  64      */
  65 
  66     private BlockingQueue<Integer> q;
  67 
  68     @Setup
  69     public void setup() {
  70         q = new ArrayBlockingQueue<Integer>(1);
  71     }
  72 
  73     @Group("Q")
  74     @Benchmark
  75     public Integer take() throws InterruptedException {
  76         return q.take();
  77     }
  78 
  79     @Group("Q")
  80     @Benchmark
  81     public void put() throws InterruptedException {
  82         q.put(42);
  83     }
  84 
  85     /*
  86      * ============================== HOW TO RUN THIS TEST: ====================================
  87      *
  88      * You can run this test:
  89      *
  90      * a) Via the command line:
  91      *    $ mvn clean install
  92      *    $ java -jar target/benchmarks.jar JMHSample_30 -wi 5 -i 5 -t 2 -f 5
  93      *    (we requested 5 warmup iterations, 5 iterations, 2 threads, and 5 forks)
  94      *
  95      * b) Via the Java API:
  96      *    (see the JMH homepage for possible caveats when running from IDE:
  97      *      http://openjdk.java.net/projects/code-tools/jmh/)
  98      */
  99 
 100     public static void main(String[] args) throws RunnerException {
 101         Options opt = new OptionsBuilder()
 102                 .include(JMHSample_30_Interrupts.class.getSimpleName())
 103                 .warmupIterations(5)
 104                 .measurementIterations(5)
 105                 .threads(2)
 106                 .forks(5)

 107                 .build();
 108 
 109         new Runner(opt).run();
 110     }
 111 
 112 }


  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.openjdk.jmh.samples;
  26 
  27 import org.openjdk.jmh.annotations.Benchmark;
  28 import org.openjdk.jmh.annotations.BenchmarkMode;
  29 import org.openjdk.jmh.annotations.Group;
  30 import org.openjdk.jmh.annotations.Mode;
  31 import org.openjdk.jmh.annotations.OutputTimeUnit;
  32 import org.openjdk.jmh.annotations.Scope;
  33 import org.openjdk.jmh.annotations.Setup;
  34 import org.openjdk.jmh.annotations.State;
  35 import org.openjdk.jmh.runner.Runner;
  36 import org.openjdk.jmh.runner.RunnerException;
  37 import org.openjdk.jmh.runner.options.Options;
  38 import org.openjdk.jmh.runner.options.OptionsBuilder;
  39 import org.openjdk.jmh.runner.options.TimeValue;
  40 
  41 import java.util.concurrent.ArrayBlockingQueue;
  42 import java.util.concurrent.BlockingQueue;
  43 import java.util.concurrent.TimeUnit;
  44 
  45 @BenchmarkMode(Mode.AverageTime)
  46 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  47 @State(Scope.Group)
  48 public class JMHSample_30_Interrupts {
  49 
  50     /*
  51      * JMH can also detect when threads are stuck in the benchmarks, and try
  52      * to forcefully interrupt the benchmark thread. JMH tries to do that
  53      * when it is arguably sure it would not affect the measurement.
  54      */
  55 
  56     /*
  57      * In this example, we want to measure the simple performance characteristics
  58      * of the ArrayBlockingQueue. Unfortunately, doing that without a harness
  59      * support will deadlock one of the threads, because the executions of
  60      * take/put are not paired perfectly. Fortunately for us, both methods react
  61      * to interrupts well, and therefore we can rely on JMH to terminate the
  62      * measurement for us. JMH will notify users about the interrupt actions
  63      * nevertheless, so users can see if those interrupts affected the measurement.
  64      * JMH will start issuing interrupts after the default or user-specified timeout
  65      * had been reached.
  66      *
  67      * This is a variant of org.openjdk.jmh.samples.JMHSample_18_Control, but without
  68      * the explicit control objects. This example is suitable for the methods which
  69      * react to interrupts gracefully.
  70      */
  71 
  72     private BlockingQueue<Integer> q;
  73 
  74     @Setup
  75     public void setup() {
  76         q = new ArrayBlockingQueue<Integer>(1);
  77     }
  78 
  79     @Group("Q")
  80     @Benchmark
  81     public Integer take() throws InterruptedException {
  82         return q.take();
  83     }
  84 
  85     @Group("Q")
  86     @Benchmark
  87     public void put() throws InterruptedException {
  88         q.put(42);
  89     }
  90 
  91     /*
  92      * ============================== HOW TO RUN THIS TEST: ====================================
  93      *
  94      * You can run this test:
  95      *
  96      * a) Via the command line:
  97      *    $ mvn clean install
  98      *    $ java -jar target/benchmarks.jar JMHSample_30 -wi 5 -i 5 -t 2 -f 5 -to 5
  99      *    (we requested 5 warmup iterations, 5 iterations, 2 threads, 5 forks, and 5 sec timeout)
 100      *
 101      * b) Via the Java API:
 102      *    (see the JMH homepage for possible caveats when running from IDE:
 103      *      http://openjdk.java.net/projects/code-tools/jmh/)
 104      */
 105 
 106     public static void main(String[] args) throws RunnerException {
 107         Options opt = new OptionsBuilder()
 108                 .include(JMHSample_30_Interrupts.class.getSimpleName())
 109                 .warmupIterations(5)
 110                 .measurementIterations(5)
 111                 .threads(2)
 112                 .forks(5)
 113                 .timeout(TimeValue.seconds(5))
 114                 .build();
 115 
 116         new Runner(opt).run();
 117     }
 118 
 119 }