--- jsr166/src/test/loops/COWALAddIfAbsentLoops.java 2013/04/05 12:15:07 1.1 +++ jsr166/src/test/loops/COWALAddIfAbsentLoops.java 2013/04/06 20:12:23 1.4 @@ -7,35 +7,44 @@ import java.util.concurrent.*; import java.util.concurrent.atomic.*; -/* +/** * A simple test for evaluating different implementations of * CopyOnWriteArrayList.addIfAbsent. */ - public class COWALAddIfAbsentLoops { - static final int SIZE = 25000; + static final int SIZE = Integer.getInteger("size", 35000); + + /** + * Set to 1 for 0% cache hit ratio (every addIfAbsent a cache miss). + * Set to 2 for 50% cache hit ratio. + */ + static final int CACHE_HIT_FACTOR = Integer.getInteger("cache.hit.factor", 1); + + static final int MAX_STRIPES = Integer.getInteger("max.stripes", 4); + + static final int REPS = Integer.getInteger("reps", 3); public static void main(String[] args) throws Exception { - for (int reps = 0; reps < 4; ++reps) { - for (int i = 1; i <= 4; ++i) + for (int reps = 0; reps < REPS; ++reps) { + for (int i = 1; i <= MAX_STRIPES; ++i) test(i); } } - static AtomicInteger result = new AtomicInteger(); + static final AtomicInteger result = new AtomicInteger(); public static void test(int n) throws Exception { result.set(0); - Phaser started = new Phaser(n + 1); + Thread[] ts = new Thread[CACHE_HIT_FACTOR*n]; + Phaser started = new Phaser(ts.length + 1); CopyOnWriteArrayList list = new CopyOnWriteArrayList(); - Thread[] ts = new Thread[n]; - for (int i = 0; i < n; ++i) - (ts[i] = new Thread(new Task(i, n, list, started))).start(); + for (int i = 0; i < ts.length; ++i) + (ts[i] = new Thread(new Task(i%n, n, list, started))).start(); long p = started.arriveAndAwaitAdvance(); long st = System.nanoTime(); - for (int i = 0; i < n; ++i) - ts[i].join(); + for (Thread thread : ts) + thread.join(); double secs = ((double)System.nanoTime() - st) / (1000L * 1000 * 1000); System.out.println("Threads: " + n + " Time: " + secs); if (result.get() != SIZE)