--- jsr166/src/test/loops/TimeoutProducerConsumerLoops.java 2007/02/19 00:46:06 1.2 +++ jsr166/src/test/loops/TimeoutProducerConsumerLoops.java 2009/10/23 19:57:07 1.3 @@ -5,10 +5,23 @@ */ import java.util.concurrent.*; +//import jsr166y.*; public class TimeoutProducerConsumerLoops { - static final int CAPACITY = 100; + static final int NCPUS = Runtime.getRuntime().availableProcessors(); static final ExecutorService pool = Executors.newCachedThreadPool(); + + // Number of elements passed around -- must be power of two + // Elements are reused from pool to minimize alloc impact + static final int POOL_SIZE = 1 << 8; + 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) + intPool[i] = Integer.valueOf(i); + } + + static boolean print = false; static int producerSum; static int consumerSum; @@ -27,22 +40,13 @@ public class TimeoutProducerConsumerLoop } public static void main(String[] args) throws Exception { - int maxPairs = 100; - int iters = 100000; + int maxPairs = NCPUS * 3 / 2; + int iters = 1000000; if (args.length > 0) maxPairs = Integer.parseInt(args[0]); - print = false; - System.out.println("Warmup..."); - oneTest(1, 10000); - Thread.sleep(100); - oneTest(2, 10000); - Thread.sleep(100); - oneTest(2, 10000); - Thread.sleep(100); print = true; - int k = 1; for (int i = 1; i <= maxPairs;) { System.out.println("Pairs:" + i); @@ -58,35 +62,46 @@ public class TimeoutProducerConsumerLoop pool.shutdown(); } - static void oneTest(int pairs, int iters) throws Exception { - int fairIters = iters/20; + static void oneTest(int n, int iters) throws Exception { + if (print) + System.out.print("LinkedTransferQueue "); + oneRun(new LinkedTransferQueue(), n, iters); + + if (print) + System.out.print("LinkedTransferQueue(xfer)"); + oneRun(new LTQasSQ(), n, iters); + if (print) - System.out.print("ArrayBlockingQueue "); - oneRun(new ArrayBlockingQueue(CAPACITY), pairs, iters); + System.out.print("LinkedBlockingQueue "); + oneRun(new LinkedBlockingQueue(), n, iters); if (print) - System.out.print("LinkedBlockingQueue "); - oneRun(new LinkedBlockingQueue(CAPACITY), pairs, iters); + System.out.print("LinkedBlockingQueue(cap) "); + oneRun(new LinkedBlockingQueue(POOL_SIZE), n, iters); if (print) - System.out.print("LinkedBlockingDeque "); - oneRun(new LinkedBlockingDeque(CAPACITY), pairs, iters); + System.out.print("ArrayBlockingQueue(cap) "); + oneRun(new ArrayBlockingQueue(POOL_SIZE), n, iters); if (print) - System.out.print("SynchronousQueue "); - oneRun(new SynchronousQueue(), pairs, iters); + System.out.print("LinkedBlockingDeque "); + oneRun(new LinkedBlockingDeque(), n, iters); if (print) - System.out.print("SynchronousQueue(fair) "); - oneRun(new SynchronousQueue(true), pairs, fairIters); + System.out.print("SynchronousQueue "); + oneRun(new SynchronousQueue(), n, iters); if (print) - System.out.print("PriorityBlockingQueue "); - oneRun(new PriorityBlockingQueue(), pairs, fairIters); + System.out.print("SynchronousQueue(fair) "); + oneRun(new SynchronousQueue(true), n, iters); if (print) - System.out.print("ArrayBlockingQueue(fair)"); - oneRun(new ArrayBlockingQueue(CAPACITY, true), pairs, fairIters); + System.out.print("PriorityBlockingQueue "); + oneRun(new PriorityBlockingQueue(), n, iters / 16); + + if (print) + System.out.print("ArrayBlockingQueue(fair) "); + oneRun(new ArrayBlockingQueue(POOL_SIZE, true), n, iters/16); } @@ -112,12 +127,12 @@ public class TimeoutProducerConsumerLoop int s = 0; int l = hashCode(); int i = 0; - long timeout = 1; + long timeout = 1000; while (i < iters) { l = LoopHelpers.compute4(l); - if (queue.offer(new Integer(l), - timeout, TimeUnit.NANOSECONDS)) { - s += LoopHelpers.compute4(l); + Integer v = intPool[l & POOL_MASK]; + if (queue.offer(v, timeout, TimeUnit.NANOSECONDS)) { + s += LoopHelpers.compute4(v.intValue()); ++i; if (timeout > 1) timeout--; @@ -146,7 +161,7 @@ public class TimeoutProducerConsumerLoop int l = 0; int s = 0; int i = 0; - long timeout = 1; + long timeout = 1000; while (i < iters) { Integer e = queue.poll(timeout, TimeUnit.NANOSECONDS); @@ -182,8 +197,22 @@ public class TimeoutProducerConsumerLoop barrier.await(); long time = timer.getTime(); checkSum(); + q.clear(); if (print) System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * npairs)) + " ns per transfer"); } + static final class LTQasSQ extends LinkedTransferQueue { + LTQasSQ() { super(); } + public void put(T x) { + try { super.transfer(x); + } catch (InterruptedException ex) { throw new Error(); } + } + + public boolean offer(T x, long timeout, TimeUnit unit) { + return super.offer(x, timeout, unit); + } + + } + }