1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.openjdk.bench.java.util.concurrent;
  24 
  25 import org.openjdk.jmh.annotations.Benchmark;
  26 import org.openjdk.jmh.annotations.BenchmarkMode;
  27 import org.openjdk.jmh.annotations.Mode;
  28 import org.openjdk.jmh.annotations.OutputTimeUnit;
  29 import org.openjdk.jmh.annotations.Param;
  30 import org.openjdk.jmh.annotations.Scope;
  31 import org.openjdk.jmh.annotations.Setup;
  32 import org.openjdk.jmh.annotations.State;
  33 import org.openjdk.jmh.annotations.TearDown;
  34 import org.openjdk.jmh.infra.Blackhole;
  35 
  36 import java.util.concurrent.ArrayBlockingQueue;
  37 import java.util.concurrent.BlockingQueue;
  38 import java.util.concurrent.LinkedBlockingQueue;
  39 import java.util.concurrent.PriorityBlockingQueue;
  40 import java.util.concurrent.TimeUnit;
  41 
  42 /**
  43  * Tests the different blocking queues in the java.util.concurrent package.
  44  * The tests are done with a single producer and a variable number of consumers.
  45  * The tests are created from Doug Lea's concurrent test suite.
  46  */
  47 @BenchmarkMode(Mode.AverageTime)
  48 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  49 @State(Scope.Benchmark)
  50 public class ProducerConsumer {
  51 
  52     @Param("100")
  53     private int capacity;
  54 
  55     @Param
  56     private QueueType type;
  57 
  58     private BlockingQueue<Integer> q;
  59     private Producer prod;
  60 
  61     @Setup
  62     public void prepare() {
  63         switch (type) {
  64             case ABQ_F:
  65                 q = new ArrayBlockingQueue<>(capacity, true);
  66                 break;
  67             case ABQ_NF:
  68                 q = new ArrayBlockingQueue<>(capacity, false);
  69                 break;
  70             case LBQ:
  71                 q = new LinkedBlockingQueue<>(capacity);
  72                 break;
  73             case PBQ:
  74                 q = new PriorityBlockingQueue<>(capacity);
  75                 break;
  76             default:
  77                 throw new RuntimeException();
  78         }
  79 
  80         prod = new Producer(q);
  81         prod.start();
  82     }
  83 
  84     @TearDown
  85     public void teardown() {
  86         prod.halt();
  87     }
  88 
  89     @Benchmark
  90     public void test() {
  91         try {
  92             int last = -1;
  93             int v = q.take();
  94             if (v < last) {
  95                 throw new Error("Out-of-Order transfer");
  96             }
  97             Blackhole.consumeCPU(10);
  98         } catch (Exception ie) {
  99         }
 100     }
 101 
 102     public enum QueueType {
 103         LBQ,
 104         ABQ_NF,
 105         ABQ_F,
 106         PBQ,
 107     }
 108 
 109     private class Producer extends Thread {
 110         private final BlockingQueue<Integer> queue;
 111         private int i = 0;
 112         private volatile boolean running;
 113 
 114         public Producer(BlockingQueue<Integer> queue) {
 115             this.queue = queue;
 116         }
 117 
 118         @Override
 119         public void run() {
 120             running = true;
 121             try {
 122                 while (running) {
 123                     queue.put(i++);
 124                 }
 125             } catch (Exception ie) {
 126             }
 127         }
 128 
 129         public void halt() {
 130             running = false;
 131             this.interrupt();
 132         }
 133     }
 134 }