My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Copyright 2011 LMAX Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lmax.disruptor;

import com.lmax.disruptor.support.FizzBuzzEvent;
import com.lmax.disruptor.support.FizzBuzzEventHandler;
import com.lmax.disruptor.support.FizzBuzzQueueProcessor;
import com.lmax.disruptor.support.FizzBuzzStep;
import org.junit.Assert;
import org.junit.Test;

import java.util.concurrent.*;

/**
* <pre>
* Produce an event replicated to two event processors and fold back to a single third event processor.
*
* +-----+
* +----->| EP1 |------+
* | +-----+ |
* | v
* +----+ +-----+
* | P1 | | EP3 |
* +----+ +-----+
* | ^
* | +-----+ |
* +----->| EP2 |------+
* +-----+
*
*
* Queue Based:
* ============
* take put
* put +====+ +-----+ +====+ take
* +----->| Q1 |<---| EP1 |--->| Q3 |<------+
* | +====+ +-----+ +====+ |
* | |
* +----+ +====+ +-----+ +====+ +-----+
* | P1 |--->| Q2 |<---| EP2 |--->| Q4 |<---| EP3 |
* +----+ +====+ +-----+ +====+ +-----+
*
* P1 - Publisher 1
* Q1 - Queue 1
* Q2 - Queue 2
* Q3 - Queue 3
* Q4 - Queue 4
* EP1 - EventProcessor 1
* EP2 - EventProcessor 2
* EP3 - EventProcessor 3
*
*
* Disruptor:
* ==========
* track to prevent wrap
* +-------------------------------+
* | |
* | v
* +----+ +====+ +=====+ +-----+
* | P1 |--->| RB |<--------------| SB2 |<---| EP3 |
* +----+ +====+ +=====+ +-----+
* claim ^ get | waitFor
* | |
* +=====+ +-----+ |
* | SB1 |<---| EP1 |<-----+
* +=====+ +-----+ |
* ^ |
* | +-----+ |
* +-------| EP2 |<-----+
* waitFor +-----+
*
* P1 - Publisher 1
* RB - RingBuffer
* SB1 - SequenceBarrier 1
* EP1 - EventProcessor 1
* EP2 - EventProcessor 2
* SB2 - SequenceBarrier 2
* EP3 - EventProcessor 3
*
* </pre>
*/
public final class OnePublisherToThreeProcessorDiamondThroughputTest extends AbstractPerfTestQueueVsDisruptor
{
private static final int NUM_EVENT_PROCESSORS = 3;
private static final int BUFFER_SIZE = 1024 * 8;
private static final long ITERATIONS = 1000L * 1000L * 100L;
private final ExecutorService EXECUTOR = Executors.newFixedThreadPool(NUM_EVENT_PROCESSORS);

private final long expectedResult;
{
long temp = 0L;

for (long i = 0; i < ITERATIONS; i++)
{
boolean fizz = 0 == (i % 3L);
boolean buzz = 0 == (i % 5L);

if (fizz && buzz)
{
++temp;
}
}

expectedResult = temp;
}

///////////////////////////////////////////////////////////////////////////////////////////////

private final BlockingQueue<Long> fizzInputQueue = new LinkedBlockingQueue<Long>(BUFFER_SIZE);
private final BlockingQueue<Long> buzzInputQueue = new LinkedBlockingQueue<Long>(BUFFER_SIZE);
private final BlockingQueue<Boolean> fizzOutputQueue = new LinkedBlockingQueue<Boolean>(BUFFER_SIZE);
private final BlockingQueue<Boolean> buzzOutputQueue = new LinkedBlockingQueue<Boolean>(BUFFER_SIZE);

private final FizzBuzzQueueProcessor fizzQueueProcessor =
new FizzBuzzQueueProcessor(FizzBuzzStep.FIZZ, fizzInputQueue, buzzInputQueue, fizzOutputQueue, buzzOutputQueue, ITERATIONS - 1);

private final FizzBuzzQueueProcessor buzzQueueProcessor =
new FizzBuzzQueueProcessor(FizzBuzzStep.BUZZ, fizzInputQueue, buzzInputQueue, fizzOutputQueue, buzzOutputQueue, ITERATIONS - 1);

private final FizzBuzzQueueProcessor fizzBuzzQueueProcessor =
new FizzBuzzQueueProcessor(FizzBuzzStep.FIZZ_BUZZ, fizzInputQueue, buzzInputQueue, fizzOutputQueue, buzzOutputQueue, ITERATIONS - 1);

///////////////////////////////////////////////////////////////////////////////////////////////

private final RingBuffer<FizzBuzzEvent> ringBuffer =
new RingBuffer<FizzBuzzEvent>(FizzBuzzEvent.EVENT_FACTORY,
new SingleThreadedClaimStrategy(BUFFER_SIZE),
new YieldingWaitStrategy());

private final SequenceBarrier sequenceBarrier = ringBuffer.newBarrier();

private final FizzBuzzEventHandler fizzHandler = new FizzBuzzEventHandler(FizzBuzzStep.FIZZ);
private final BatchEventProcessor<FizzBuzzEvent> batchProcessorFizz =
new BatchEventProcessor<FizzBuzzEvent>(ringBuffer, sequenceBarrier, fizzHandler);

private final FizzBuzzEventHandler buzzHandler = new FizzBuzzEventHandler(FizzBuzzStep.BUZZ);
private final BatchEventProcessor<FizzBuzzEvent> batchProcessorBuzz =
new BatchEventProcessor<FizzBuzzEvent>(ringBuffer, sequenceBarrier, buzzHandler);

private final SequenceBarrier sequenceBarrierFizzBuzz =
ringBuffer.newBarrier(batchProcessorFizz.getSequence(), batchProcessorBuzz.getSequence());

private final FizzBuzzEventHandler fizzBuzzHandler = new FizzBuzzEventHandler(FizzBuzzStep.FIZZ_BUZZ);
private final BatchEventProcessor<FizzBuzzEvent> batchProcessorFizzBuzz =
new BatchEventProcessor<FizzBuzzEvent>(ringBuffer, sequenceBarrierFizzBuzz, fizzBuzzHandler);
{
ringBuffer.setGatingSequences(batchProcessorFizzBuzz.getSequence());
}

///////////////////////////////////////////////////////////////////////////////////////////////

@Override
protected int getRequiredProcessorCount()
{
return 4;
}

@Test
@Override
public void shouldCompareDisruptorVsQueues() throws Exception
{
testImplementations();
}

@Override
protected long runQueuePass() throws Exception
{
final CountDownLatch latch = new CountDownLatch(1);
fizzBuzzQueueProcessor.reset(latch);

Future<?>[] futures = new Future[NUM_EVENT_PROCESSORS];
futures[0] = EXECUTOR.submit(fizzQueueProcessor);
futures[1] = EXECUTOR.submit(buzzQueueProcessor);
futures[2] = EXECUTOR.submit(fizzBuzzQueueProcessor);

long start = System.currentTimeMillis();

for (long i = 0; i < ITERATIONS; i++)
{
Long value = Long.valueOf(i);
fizzInputQueue.put(value);
buzzInputQueue.put(value);
}

latch.await();
long opsPerSecond = (ITERATIONS * 1000L) / (System.currentTimeMillis() - start);

fizzQueueProcessor.halt();
buzzQueueProcessor.halt();
fizzBuzzQueueProcessor.halt();

for (Future<?> future : futures)
{
future.cancel(true);
}

Assert.assertEquals(expectedResult, fizzBuzzQueueProcessor.getFizzBuzzCounter());

return opsPerSecond;
}

@Override
protected long runDisruptorPass() throws Exception
{
CountDownLatch latch = new CountDownLatch(1);
fizzBuzzHandler.reset(latch, batchProcessorFizzBuzz.getSequence().get() + ITERATIONS);

EXECUTOR.submit(batchProcessorFizz);
EXECUTOR.submit(batchProcessorBuzz);
EXECUTOR.submit(batchProcessorFizzBuzz);

long start = System.currentTimeMillis();

for (long i = 0; i < ITERATIONS; i++)
{
long sequence = ringBuffer.next();
ringBuffer.get(sequence).setValue(i);
ringBuffer.publish(sequence);
}

latch.await();
long opsPerSecond = (ITERATIONS * 1000L) / (System.currentTimeMillis() - start);

batchProcessorFizz.halt();
batchProcessorBuzz.halt();
batchProcessorFizzBuzz.halt();

Assert.assertEquals(expectedResult, fizzBuzzHandler.getFizzBuzzCounter());

return opsPerSecond;
}
}

Change log

r543 by mikeb01 on Apr 19, 2012   Diff
Fix some generics warnings
Go to: 
Project members, sign in to write a code review

Older revisions

r469 by mjpt777 on Nov 2, 2011   Diff
Opened up claim and wait strategies so
user defined ones can be applied.
r463 by mjpt777 on Oct 31, 2011   Diff
Migrated perf tests from using
ArrayBlockingQueue to
LinkedBlockingQueue because of better
performance on more recent processors.
r457 by mjpt777 on Oct 29, 2011   Diff
Added code for checking if sufficient
processors exist for running the
performance tests.
All revisions of this file

File info

Size: 8620 bytes, 244 lines
Powered by Google Project Hosting