--- jsr166/src/test/loops/OfferPollLoops.java 2009/10/23 19:57:06 1.1 +++ jsr166/src/test/loops/OfferPollLoops.java 2015/08/10 03:13:33 1.9 @@ -1,7 +1,7 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ import java.util.*; @@ -34,14 +34,14 @@ public class OfferPollLoops { static final int POOL_MASK = POOL_SIZE-1; static final Integer[] intPool = new Integer[POOL_SIZE]; static { - for (int i = 0; i < POOL_SIZE; ++i) + for (int i = 0; i < POOL_SIZE; ++i) intPool[i] = Integer.valueOf(i); } // Number of puts by producers or takes by consumers static final int ITERS = 1 << 20; - // max lag between a producer and consumer to avoid + // max lag between a producer and consumer to avoid // this becoming a GC test rather than queue test. // Used only per-pair to lessen impact on queue sync static final int LAG_MASK = (1 << 12) - 1; @@ -49,20 +49,19 @@ public class OfferPollLoops { public static void main(String[] args) throws Exception { int maxN = NCPUS * 3 / 2; - if (args.length > 0) + if (args.length > 0) maxN = Integer.parseInt(args[0]); warmup(); print = true; - int k = 1; - for (int i = 1; i <= maxN;) { + for (int k = 1, i = 1; i <= maxN;) { System.out.println("Pairs:" + i); oneTest(i, ITERS); if (i == k) { k = i << 1; i = i + (i >>> 1); - } - else + } + else i = k; } pool.shutdown(); @@ -73,14 +72,14 @@ public class OfferPollLoops { System.out.print("Warmup "); int it = 2000; for (int j = 5; j > 0; --j) { - oneTest(j, it); + oneTest(j, it); System.out.print("."); it += 1000; } System.gc(); it = 20000; for (int j = 5; j > 0; --j) { - oneTest(j, it); + oneTest(j, it); System.out.print("."); it += 10000; } @@ -103,6 +102,11 @@ public class OfferPollLoops { Thread.sleep(100); // System.gc(); if (print) + System.out.print("ConcurrentLinkedDeque "); + oneRun(new ConcurrentLinkedDeque(), n, iters); + + Thread.sleep(100); // System.gc(); + if (print) System.out.print("LinkedBlockingQueue "); oneRun(new LinkedBlockingQueue(), n, iters); @@ -115,13 +119,12 @@ public class OfferPollLoops { if (print) System.out.print("LinkedBlockingDeque "); oneRun(new LinkedBlockingDeque(), n, iters); - + Thread.sleep(100); // System.gc(); if (print) System.out.print("ArrayBlockingQueue "); oneRun(new ArrayBlockingQueue(POOL_SIZE), n, iters); - Thread.sleep(100); // System.gc(); if (print) System.out.print("PriorityBlockingQueue "); @@ -131,17 +134,15 @@ public class OfferPollLoops { if (print) System.out.print("ArrayBlockingQueue(fair)"); oneRun(new ArrayBlockingQueue(POOL_SIZE, true), n, fairIters); - } - - static abstract class Stage implements Runnable { + + abstract static class Stage implements Runnable { final int iters; final Queue queue; final CyclicBarrier barrier; final Phaser lagPhaser; - Stage (Queue q, CyclicBarrier b, Phaser s, - int iters) { - queue = q; + Stage(Queue q, CyclicBarrier b, Phaser s, int iters) { + queue = q; barrier = b; lagPhaser = s; this.iters = iters; @@ -171,21 +172,21 @@ public class OfferPollLoops { break; if ((i & LAG_MASK) == LAG_MASK) lagPhaser.arriveAndAwaitAdvance(); - } + } } addProducerSum(ps); barrier.await(); } - catch (Exception ie) { - ie.printStackTrace(); - return; + catch (Exception ie) { + ie.printStackTrace(); + return; } } } static class Consumer extends Stage { Consumer(Queue q, CyclicBarrier b, Phaser s, - int iters) { + int iters) { super(q, b, s, iters); } @@ -209,9 +210,9 @@ public class OfferPollLoops { addConsumerSum(cs); barrier.await(); } - catch (Exception ie) { - ie.printStackTrace(); - return; + catch (Exception ie) { + ie.printStackTrace(); + return; } } @@ -233,6 +234,4 @@ public class OfferPollLoops { System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * n)) + " ns per transfer"); } - } -