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
/*
* 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.PerfTestUtil;
import com.lmax.disruptor.support.ValueAdditionEventHandler;
import com.lmax.disruptor.support.ValueEvent;
import org.junit.Assert;
import org.junit.Test;

import java.util.concurrent.*;

/**
* <pre>
* UniCast a series of items between 1 publisher and 1 event processor.
* This test illustrates the benefits of writing batches of 10 events
* for exchange at a time.
*
* +----+ +-----+
* | P1 |--->| EP1 |
* +----+ +-----+
*
*
* Queue Based:
* ============
*
* put take
* +----+ +====+ +-----+
* | P1 |--->| Q1 |<---| EP1 |
* +----+ +====+ +-----+
*
* P1 - Publisher 1
* Q1 - Queue 1
* EP1 - EventProcessor 1
*
*
* Disruptor:
* ==========
* track to prevent wrap
* +------------------+
* | |
* | v
* +----+ +====+ +====+ +-----+
* | P1 |--->| RB |<---| SB | | EP1 |
* +----+ +====+ +====+ +-----+
* claim get ^ |
* | |
* +--------+
* waitFor
*
* P1 - Publisher 1
* RB - RingBuffer
* SB - SequenceBarrier
* EP1 - EventProcessor 1
* </pre>
*/
public final class OnePublisherToOneProcessorUniCastBatchThroughputTest extends AbstractPerfTestQueueVsDisruptor
{
private static final int BUFFER_SIZE = 1024 * 8;
private static final long ITERATIONS = 1000L * 1000L * 100L;
private final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor();
private final long expectedResult = PerfTestUtil.accumulatedAddition(ITERATIONS);

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

private final RingBuffer<ValueEvent> ringBuffer =
new RingBuffer<ValueEvent>(ValueEvent.EVENT_FACTORY,
new SingleThreadedClaimStrategy(BUFFER_SIZE),
new YieldingWaitStrategy());
private final SequenceBarrier sequenceBarrier = ringBuffer.newBarrier();
private final ValueAdditionEventHandler handler = new ValueAdditionEventHandler();
private final BatchEventProcessor<ValueEvent> batchEventProcessor = new BatchEventProcessor<ValueEvent>(ringBuffer, sequenceBarrier, handler);
{
ringBuffer.setGatingSequences(batchEventProcessor.getSequence());
}

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

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

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

@Override
protected long runQueuePass() throws InterruptedException
{
// Same expected results as UniCast scenario
return 0L;
}

@Override
protected long runDisruptorPass() throws InterruptedException
{
final CountDownLatch latch = new CountDownLatch(1);
handler.reset(latch, batchEventProcessor.getSequence().get() + ITERATIONS);
EXECUTOR.submit(batchEventProcessor);

final int batchSize = 10;
final BatchDescriptor batchDescriptor = ringBuffer.newBatchDescriptor(batchSize);

long start = System.currentTimeMillis();

long offset = 0;
for (long i = 0; i < ITERATIONS; i += batchSize)
{
ringBuffer.next(batchDescriptor);
for (long c = batchDescriptor.getStart(), end = batchDescriptor.getEnd(); c <= end; c++)
{
ringBuffer.get(c).setValue(offset++);
}
ringBuffer.publish(batchDescriptor);
}

latch.await();

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

Assert.assertEquals(expectedResult, handler.getValue());

return opsPerSecond;
}
}

Change log

r469 by mjpt777 on Nov 2, 2011   Diff
Opened up claim and wait strategies so
user defined ones can be applied.
Go to: 
Project members, sign in to write a code review

Older revisions

r457 by mjpt777 on Oct 29, 2011   Diff
Added code for checking if sufficient
processors exist for running the
performance tests.
r443 by mjpt777 on Oct 17, 2011   Diff
Drop iteration count to 100 million
for per runs.  Seems to be sufficient.
r440 by mjpt777 on Oct 2, 2011   Diff
Reworked performance tests to use a
CountDownLatch to signal end of test
run for threads.
All revisions of this file

File info

Size: 4620 bytes, 144 lines
Powered by Google Project Hosting