-
Notifications
You must be signed in to change notification settings - Fork 554
/
SpscChannelThroughputTest.java
120 lines (108 loc) · 3.72 KB
/
SpscChannelThroughputTest.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
/*
* 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 org.jctools.jmh.throughput.channels;
import org.jctools.channels.*;
import org.jctools.channels.spsc.SpscChannel;
import org.jctools.util.PortableJvmInfo;
import org.jctools.util.Pow2;
import org.jctools.util.UnsafeDirectByteBuffer;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.infra.Control;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
/**
* JMH Test to measure throughput for a tiny message with an SPSC Channel.
*
* To run this benchmark:
*
* java -jar target/microbenchmarks.jar ".*.SpscPingPongThroughputTest.*"
*/
@State(Scope.Group)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Threads(2)
@Fork(1)
@Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
public class SpscChannelThroughputTest {
@Param({"32000"})
private int capacity;
private ByteBuffer buffer;
private Channel<Ping> channel;
private ChannelProducer<Ping> producer;
private ChannelConsumer consumer;
private ChannelReceiver<Ping> receiver;
// Deliberately not a local to avoid constant folding.
private long writeValue = 1L;
@Setup
public void setup(final Blackhole blackhole) {
receiver = new ChannelReceiver<Ping>() {
@Override
public void accept(Ping element) {
blackhole.consume(element.getValue());
}
};
int requiredBufferSize = OffHeapFixedMessageSizeRingBuffer.getRequiredBufferSize(capacity, PortableJvmInfo.CACHE_LINE_SIZE);
buffer = UnsafeDirectByteBuffer.allocateAlignedByteBuffer(requiredBufferSize, PortableJvmInfo.CACHE_LINE_SIZE);
channel = new SpscChannel<Ping>(buffer, capacity, Ping.class);
producer = channel.producer();
consumer = channel.consumer(receiver);
}
@Benchmark
@Group("busy")
@GroupThreads(1)
public void writeBusy(Control cnt) {
ChannelProducer<Ping> lProducer = producer;
while (!lProducer.claim()) {
if (cnt.stopMeasurement) {
return;// drop out of spinning if the benchmark iteration is done
}
}
Ping element = lProducer.currentElement();
element.setValue(writeValue);
lProducer.commit();
}
@Benchmark
@Group("busy")
@GroupThreads(1)
public void readBusy(Control cnt) {
while (!consumer.read() && !cnt.stopMeasurement) {
}
}
@Benchmark
@Group("backoffOneNano")
@GroupThreads(1)
public void write(Control cnt) {
ChannelProducer<Ping> lProducer = producer;
while (!lProducer.claim()) {
if (cnt.stopMeasurement) {
return;// drop out of spinning if the benchmark iteration is done
}
LockSupport.parkNanos(1L);
}
Ping element = lProducer.currentElement();
element.setValue(writeValue);
lProducer.commit();
}
@Benchmark
@Group("backoffOneNano")
@GroupThreads(1)
public void read(Control cnt) {
while (!consumer.read() && !cnt.stopMeasurement) {
LockSupport.parkNanos(1L);
}
}
}