-
Notifications
You must be signed in to change notification settings - Fork 42
/
PerfTest.java
226 lines (188 loc) · 6.63 KB
/
PerfTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import java.util.Arrays;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
public class PerfTest
{
private static final long TEST_COOL_OFF_MS = 10;
private static final ExecutorService EXECUTOR = Executors.newCachedThreadPool();
private static final Spaceship[] SPACESHIPS =
{
new SynchronizedSpaceship(),
new ReadWriteLockSpaceShip(),
new ReentrantLockSpaceship(),
new StampedLockSpaceship(),
new StampedLockWithRetriesSpaceship(),
new LockFreeSpaceship(),
};
private static int NUM_WRITERS;
private static int NUM_READERS;
private static long TEST_DURATION_MS;
public static void main(final String[] args) throws Exception
{
NUM_READERS = Integer.parseInt(args[0]);
NUM_WRITERS = Integer.parseInt(args[1]);
TEST_DURATION_MS = Long.parseLong(args[2]);
for (int i = 0; i < 5; i++)
{
System.out.println("*** Run - " + i);
for (final Spaceship spaceship : SPACESHIPS)
{
System.gc();
Thread.sleep(TEST_COOL_OFF_MS);
perfRun(spaceship);
}
}
EXECUTOR.shutdown();
}
private static void perfRun(final Spaceship spaceship) throws Exception
{
final Results results = new Results();
final CyclicBarrier startBarrier = new CyclicBarrier(NUM_READERS + NUM_WRITERS + 1);
final CountDownLatch finishLatch = new CountDownLatch(NUM_READERS + NUM_WRITERS);
final AtomicBoolean runningFlag = new AtomicBoolean(true);
for (int i = 0; i < NUM_WRITERS; i++)
{
EXECUTOR.execute(new WriterRunner(i, results, spaceship, runningFlag, startBarrier, finishLatch));
}
for (int i = 0; i < NUM_READERS; i++)
{
EXECUTOR.execute(new ReaderRunner(i, results, spaceship, runningFlag, startBarrier, finishLatch));
}
awaitBarrier(startBarrier);
Thread.sleep(TEST_DURATION_MS);
runningFlag.set(false);
finishLatch.await();
System.out.format(
"%d readers %d writers %22s %s%n",
NUM_READERS, NUM_WRITERS,
spaceship.getClass().getSimpleName(),
results);
}
private static void awaitBarrier(final CyclicBarrier barrier)
{
try
{
barrier.await();
}
catch (final Exception ex)
{
throw new RuntimeException(ex);
}
}
static class Results
{
final long[] reads = new long[NUM_READERS];
final long[] moves = new long[NUM_WRITERS];
final long[] readAttempts = new long[NUM_READERS];
final long[] observedMoves = new long[NUM_READERS];
final long[] moveAttempts = new long[NUM_WRITERS];
public String toString()
{
long totalReads = 0;
for (final long v : reads)
{
totalReads += v;
}
final String readsSummary = String.format("%,d:", totalReads);
long totalMoves = 0;
for (final long v : moves)
{
totalMoves += v;
}
final String movesSummary = String.format("%,d:", totalMoves);
return
"reads=" + readsSummary + Arrays.toString(reads) +
" moves=" + movesSummary + Arrays.toString(moves) +
" readAttempts=" + Arrays.toString(readAttempts) +
" moveAttempts=" + Arrays.toString(moveAttempts) +
" observedMoves=" + Arrays.toString(observedMoves);
}
}
static class WriterRunner implements Runnable
{
private final int id;
private final Results results;
private final Spaceship spaceship;
private final AtomicBoolean runningFlag;
private final CyclicBarrier barrier;
private final CountDownLatch latch;
WriterRunner(
final int id,
final Results results,
final Spaceship spaceship,
final AtomicBoolean runningFlag,
final CyclicBarrier barrier,
final CountDownLatch latch)
{
this.id = id;
this.results = results;
this.spaceship = spaceship;
this.runningFlag = runningFlag;
this.barrier = barrier;
this.latch = latch;
}
public void run()
{
awaitBarrier(barrier);
long movesCounter = 0;
long movedAttemptsCount = 0;
while (runningFlag.get())
{
movedAttemptsCount += spaceship.move(1, 1);
++movesCounter;
}
results.moveAttempts[id] = movedAttemptsCount;
results.moves[id] = movesCounter;
latch.countDown();
}
}
static class ReaderRunner implements Runnable
{
private final int id;
private final Results results;
private final Spaceship spaceship;
private final AtomicBoolean runningFlag;
private final CyclicBarrier barrier;
private final CountDownLatch latch;
ReaderRunner(
final int id,
final Results results,
final Spaceship spaceship,
final AtomicBoolean runningFlag,
final CyclicBarrier barrier,
final CountDownLatch latch)
{
this.id = id;
this.results = results;
this.spaceship = spaceship;
this.runningFlag = runningFlag;
this.barrier = barrier;
this.latch = latch;
}
public void run()
{
awaitBarrier(barrier);
int[] currentCoordinates = new int[]{0, 0};
int[] lastCoordinates = new int[]{0, 0};
long readsCount = 0;
long readAttemptsCount = 0;
long observedMoves = 0;
while (runningFlag.get())
{
readAttemptsCount += spaceship.readPosition(currentCoordinates);
if (lastCoordinates[0] != currentCoordinates[0] ||
lastCoordinates[1] != currentCoordinates[1])
{
++observedMoves;
lastCoordinates[0] = currentCoordinates[0];
lastCoordinates[1] = currentCoordinates[1];
}
++readsCount;
}
results.reads[id] = readsCount;
results.readAttempts[id] = readAttemptsCount;
results.observedMoves[id] = observedMoves;
latch.countDown();
}
}
}