jdk/test/java/util/concurrent/ConcurrentQueues/GCRetention.java
changeset 3414 cdf768813b4d
child 3708 f838f712922e
equal deleted inserted replaced
3330:04c1ec47b42e 3414:cdf768813b4d
       
     1 /*
       
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     3  *
       
     4  * This code is free software; you can redistribute it and/or modify it
       
     5  * under the terms of the GNU General Public License version 2 only, as
       
     6  * published by the Free Software Foundation.
       
     7  *
       
     8  * This code is distributed in the hope that it will be useful, but WITHOUT
       
     9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    11  * version 2 for more details (a copy is included in the LICENSE file that
       
    12  * accompanied this code).
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License version
       
    15  * 2 along with this work; if not, write to the Free Software Foundation,
       
    16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    17  *
       
    18  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    19  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    20  * have any questions.
       
    21  */
       
    22 
       
    23 /*
       
    24  * This file is available under and governed by the GNU General Public
       
    25  * License version 2 only, as published by the Free Software Foundation.
       
    26  * However, the following notice accompanied the original version of this
       
    27  * file:
       
    28  *
       
    29  * Written by Doug Lea with assistance from members of JCP JSR-166
       
    30  * Expert Group and released to the public domain, as explained at
       
    31  * http://creativecommons.org/licenses/publicdomain
       
    32  */
       
    33 /*
       
    34  * @test
       
    35  * @bug 6785442
       
    36  * @summary Benchmark that tries to GC-tenure head, followed by
       
    37  * many add/remove operations.
       
    38  * @run main GCRetention 12345
       
    39  */
       
    40 
       
    41 import java.util.concurrent.ArrayBlockingQueue;
       
    42 import java.util.concurrent.ConcurrentHashMap;
       
    43 import java.util.concurrent.ConcurrentLinkedQueue;
       
    44 import java.util.concurrent.LinkedBlockingQueue;
       
    45 import java.util.concurrent.LinkedBlockingDeque;
       
    46 import java.util.concurrent.PriorityBlockingQueue;
       
    47 import java.util.LinkedList;
       
    48 import java.util.PriorityQueue;
       
    49 import java.util.ArrayList;
       
    50 import java.util.Collection;
       
    51 import java.util.Collections;
       
    52 import java.util.List;
       
    53 import java.util.Queue;
       
    54 import java.util.Map;
       
    55 
       
    56 public class GCRetention {
       
    57     // Suitable for benchmarking.  Overriden by args[0] for testing.
       
    58     int count = 1024 * 1024;
       
    59 
       
    60     final Map<String,String> results = new ConcurrentHashMap<String,String>();
       
    61 
       
    62     Collection<Queue<Boolean>> queues() {
       
    63         List<Queue<Boolean>> queues = new ArrayList<Queue<Boolean>>();
       
    64         queues.add(new ConcurrentLinkedQueue<Boolean>());
       
    65         queues.add(new ArrayBlockingQueue<Boolean>(count, false));
       
    66         queues.add(new ArrayBlockingQueue<Boolean>(count, true));
       
    67         queues.add(new LinkedBlockingQueue<Boolean>());
       
    68         queues.add(new LinkedBlockingDeque<Boolean>());
       
    69         queues.add(new PriorityBlockingQueue<Boolean>());
       
    70         queues.add(new PriorityQueue<Boolean>());
       
    71         queues.add(new LinkedList<Boolean>());
       
    72 
       
    73         try {
       
    74             queues.add((Queue<Boolean>)
       
    75                        Class.forName("java.util.concurrent.LinkedTransferQueue")
       
    76                        .newInstance());
       
    77         } catch (IllegalAccessException e) {
       
    78         } catch (InstantiationException e) {
       
    79         } catch (ClassNotFoundException e) {
       
    80             // OK; not yet added to JDK
       
    81         }
       
    82 
       
    83         // Following additional implementations are available from:
       
    84         // http://gee.cs.oswego.edu/dl/concurrency-interest/index.html
       
    85         // queues.add(new LinkedTransferQueue<Boolean>());
       
    86         // queues.add(new SynchronizedLinkedListQueue<Boolean>());
       
    87 
       
    88         // Avoid "first fast, second slow" benchmark effect.
       
    89         Collections.shuffle(queues);
       
    90         return queues;
       
    91     }
       
    92 
       
    93     void prettyPrintResults() {
       
    94         List<String> classNames = new ArrayList<String>(results.keySet());
       
    95         Collections.sort(classNames);
       
    96         int maxClassNameLength = 0;
       
    97         int maxNanosLength = 0;
       
    98         for (String name : classNames) {
       
    99             if (maxClassNameLength < name.length())
       
   100                 maxClassNameLength = name.length();
       
   101             if (maxNanosLength < results.get(name).length())
       
   102                 maxNanosLength = results.get(name).length();
       
   103         }
       
   104         String format = String.format("%%%ds %%%ds nanos/item%%n",
       
   105                                       maxClassNameLength, maxNanosLength);
       
   106         for (String name : classNames)
       
   107             System.out.printf(format, name, results.get(name));
       
   108     }
       
   109 
       
   110     void test(String[] args) {
       
   111         if (args.length > 0)
       
   112             count = Integer.valueOf(args[0]);
       
   113         // Warmup
       
   114         for (Queue<Boolean> queue : queues())
       
   115             test(queue);
       
   116         results.clear();
       
   117         for (Queue<Boolean> queue : queues())
       
   118             test(queue);
       
   119 
       
   120         prettyPrintResults();
       
   121     }
       
   122 
       
   123     void test(Queue<Boolean> q) {
       
   124         long t0 = System.nanoTime();
       
   125         for (int i = 0; i < count; i++)
       
   126             check(q.add(Boolean.TRUE));
       
   127         System.gc();
       
   128         System.gc();
       
   129         Boolean x;
       
   130         while ((x = q.poll()) != null)
       
   131             equal(x, Boolean.TRUE);
       
   132         check(q.isEmpty());
       
   133 
       
   134         for (int i = 0; i < 10 * count; i++) {
       
   135             for (int k = 0; k < 3; k++)
       
   136                 check(q.add(Boolean.TRUE));
       
   137             for (int k = 0; k < 3; k++)
       
   138                 if (q.poll() != Boolean.TRUE)
       
   139                     fail();
       
   140         }
       
   141         check(q.isEmpty());
       
   142 
       
   143         String className = q.getClass().getSimpleName();
       
   144         long elapsed = System.nanoTime() - t0;
       
   145         int nanos = (int) ((double) elapsed / (10 * 3 * count));
       
   146         results.put(className, String.valueOf(nanos));
       
   147     }
       
   148 
       
   149     //--------------------- Infrastructure ---------------------------
       
   150     volatile int passed = 0, failed = 0;
       
   151     void pass() {passed++;}
       
   152     void fail() {failed++; Thread.dumpStack();}
       
   153     void fail(String msg) {System.err.println(msg); fail();}
       
   154     void unexpected(Throwable t) {failed++; t.printStackTrace();}
       
   155     void check(boolean cond) {if (cond) pass(); else fail();}
       
   156     void equal(Object x, Object y) {
       
   157         if (x == null ? y == null : x.equals(y)) pass();
       
   158         else fail(x + " not equal to " + y);}
       
   159     public static void main(String[] args) throws Throwable {
       
   160         new GCRetention().instanceMain(args);}
       
   161     public void instanceMain(String[] args) throws Throwable {
       
   162         try {test(args);} catch (Throwable t) {unexpected(t);}
       
   163         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
       
   164         if (failed > 0) throw new AssertionError("Some tests failed");}
       
   165 }