forked from netty/netty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefaultChannelPipelineDuplexHandlerBenchmark.java
109 lines (100 loc) · 3.82 KB
/
DefaultChannelPipelineDuplexHandlerBenchmark.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
/*
* Copyright 2022 The Netty Project
*
* The Netty Project licenses this file to you 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:
*
* https://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 io.netty.microbench.channel;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.microbench.util.AbstractMicrobenchmark;
import io.netty.util.internal.PlatformDependent;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.profile.LinuxPerfAsmProfiler;
import org.openjdk.jmh.profile.LinuxPerfC2CProfiler;
import org.openjdk.jmh.runner.options.ChainedOptionsBuilder;
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 5, time = 1)
@State(Scope.Thread)
public class DefaultChannelPipelineDuplexHandlerBenchmark extends AbstractMicrobenchmark {
private ChannelPipeline pipeline;
private EmbeddedChannel channel;
@Setup
public void setup() {
channel = new EmbeddedChannel() {
@Override
public void runPendingTasks() {
// NO-OP to reduce noise on flush
}
};
// disabling auto-read to reduce noise on flush
channel.config().setAutoRead(false);
pipeline = channel.pipeline();
pipeline.addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelReadComplete(final ChannelHandlerContext ctx) {
ctx.fireChannelReadComplete();
}
});
// Duplex handlers implements both out/in interfaces causing a scalability issue
// see https://bugs.openjdk.org/browse/JDK-8180450
pipeline.addLast(new ChannelDuplexHandler() {
@Override
public void channelReadComplete(final ChannelHandlerContext ctx) {
ctx.fireChannelReadComplete();
}
@Override
public void flush(final ChannelHandlerContext ctx) {
ctx.flush();
}
});
pipeline.addLast(new ChannelDuplexHandler() {
@Override
public void channelReadComplete(final ChannelHandlerContext ctx) {
ctx.flush();
}
});
}
@TearDown
public void tearDown() {
pipeline.channel().close();
}
@Benchmark
public void propagateEvent(Blackhole hole) {
hole.consume(pipeline.fireChannelReadComplete());
}
@Benchmark
@Threads(4)
public void parallelPropagateEvent(Blackhole hole) {
hole.consume(pipeline.fireChannelReadComplete());
}
@Override
protected ChainedOptionsBuilder newOptionsBuilder() throws Exception {
if (PlatformDependent.isOsx() || PlatformDependent.isWindows()) {
return super.newOptionsBuilder();
}
return super.newOptionsBuilder()
.addProfiler(LinuxPerfAsmProfiler.class)
.addProfiler(LinuxPerfC2CProfiler.class);
}
}