1 |
jsr166 |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
import java.util.concurrent.*; |
8 |
|
|
import java.util.concurrent.atomic.*; |
9 |
|
|
|
10 |
|
|
/** |
11 |
|
|
* A simple test for evaluating different implementations of |
12 |
|
|
* CopyOnWriteArrayList.addIfAbsent. |
13 |
|
|
* |
14 |
|
|
* This benchmark differs from COWALAddIfAbsentLoops mainly by having |
15 |
|
|
* a more expensive element equals method. |
16 |
|
|
*/ |
17 |
|
|
public class COWALAddIfAbsentStringLoops { |
18 |
|
|
|
19 |
|
|
static final int SIZE = Integer.getInteger("size", 25000); |
20 |
|
|
|
21 |
|
|
/** |
22 |
|
|
* Set to 1 for 0% cache hit ratio (every addIfAbsent a cache miss). |
23 |
|
|
* Set to 2 for 50% cache hit ratio. |
24 |
|
|
*/ |
25 |
|
|
static final int CACHE_HIT_FACTOR = Integer.getInteger("cache.hit.factor", 1); |
26 |
|
|
|
27 |
jsr166 |
1.2 |
static final int MAX_STRIPES = Integer.getInteger("max.stripes", 4); |
28 |
jsr166 |
1.1 |
|
29 |
|
|
static final int REPS = Integer.getInteger("reps", 3); |
30 |
|
|
|
31 |
|
|
public static void main(String[] args) throws Exception { |
32 |
|
|
for (int reps = 0; reps < REPS; ++reps) { |
33 |
|
|
for (int i = 1; i <= MAX_STRIPES; ++i) |
34 |
|
|
test(i); |
35 |
|
|
} |
36 |
|
|
} |
37 |
|
|
|
38 |
|
|
static final AtomicInteger result = new AtomicInteger(); |
39 |
|
|
|
40 |
|
|
public static void test(int n) throws Exception { |
41 |
|
|
result.set(0); |
42 |
|
|
Thread[] ts = new Thread[CACHE_HIT_FACTOR*n]; |
43 |
|
|
Phaser started = new Phaser(ts.length + 1); |
44 |
|
|
CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>(); |
45 |
|
|
for (int i = 0; i < ts.length; ++i) |
46 |
|
|
(ts[i] = new Thread(new Task(i%n, n, list, started))).start(); |
47 |
|
|
long p = started.arriveAndAwaitAdvance(); |
48 |
|
|
long st = System.nanoTime(); |
49 |
|
|
for (Thread thread : ts) |
50 |
|
|
thread.join(); |
51 |
|
|
double secs = ((double)System.nanoTime() - st) / (1000L * 1000 * 1000); |
52 |
|
|
System.out.println("Threads: " + n + " Time: " + secs); |
53 |
|
|
if (result.get() != SIZE) |
54 |
|
|
throw new Error(); |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
static final class Task implements Runnable { |
58 |
|
|
final int id, stride; |
59 |
|
|
final CopyOnWriteArrayList<String> list; |
60 |
|
|
final Phaser started; |
61 |
|
|
Task(int id, int stride, |
62 |
|
|
CopyOnWriteArrayList<String> list, Phaser started) { |
63 |
|
|
this.id = id; |
64 |
|
|
this.stride = stride; |
65 |
|
|
this.list = list; |
66 |
|
|
this.started = started; |
67 |
|
|
} |
68 |
|
|
public void run() { |
69 |
|
|
final CopyOnWriteArrayList<String> list = this.list; |
70 |
|
|
int origin = id, inc = stride, adds = 0; |
71 |
|
|
started.arriveAndAwaitAdvance(); |
72 |
|
|
for (int i = origin; i < SIZE; i += inc) { |
73 |
|
|
if (list.addIfAbsent("asjdklfjsdfjsdjf" + i)) |
74 |
|
|
++adds; |
75 |
|
|
} |
76 |
|
|
result.getAndAdd(adds); |
77 |
|
|
} |
78 |
|
|
} |
79 |
|
|
} |