2018-08-31 22:25:07 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2018 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:
|
|
|
|
*
|
2020-10-23 14:44:18 +02:00
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
2018-08-31 22:25:07 +02:00
|
|
|
*
|
|
|
|
* 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.epoll;
|
|
|
|
|
|
|
|
import io.netty.bootstrap.Bootstrap;
|
|
|
|
import io.netty.bootstrap.ServerBootstrap;
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.channel.Channel;
|
2019-03-28 10:28:27 +01:00
|
|
|
import io.netty.channel.ChannelHandler;
|
2018-08-31 22:25:07 +02:00
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
import io.netty.channel.ChannelInitializer;
|
2019-01-23 08:32:05 +01:00
|
|
|
import io.netty.channel.EventLoopGroup;
|
|
|
|
import io.netty.channel.MultithreadEventLoopGroup;
|
|
|
|
import io.netty.channel.epoll.EpollHandler;
|
2018-08-31 22:25:07 +02:00
|
|
|
import io.netty.channel.epoll.EpollServerSocketChannel;
|
|
|
|
import io.netty.channel.epoll.EpollSocketChannel;
|
|
|
|
import io.netty.microbench.util.AbstractMicrobenchmark;
|
2021-08-25 14:12:33 +02:00
|
|
|
import io.netty.util.concurrent.Future;
|
Clean up Future/Promises API (#11575)
Motivation:
The generics for the existing futures, promises, and listeners are too complicated.
This complication comes from the existence of `ChannelPromise` and `ChannelFuture`, which forces listeners to care about the particular _type_ of future being listened on.
Modification:
* Add a `FutureContextListener` which can take a context object as an additional argument. This allows our listeners to have the channel piped through to them, so they don't need to rely on the `ChannelFuture.channel()` method.
* Make the `FutureListener`, along with the `FutureContextListener` sibling, the default listener API, retiring the `GenericFutureListener` since we no longer need to abstract over the type of the future.
* Change all uses of `ChannelPromise` to `Promise<Void>`.
* Change all uses of `ChannelFuture` to `Future<Void>`.
* Change all uses of `GenericFutureListener` to either `FutureListener` or `FutureContextListener` as needed.
* Remove `ChannelFutureListener` and `GenericFutureListener`.
* Introduce a `ChannelFutureListeners` enum to house the constants that previously lived in `ChannelFutureListener`. These constants now implement `FutureContextListener` and take the `Channel` as a context.
* Remove `ChannelPromise` and `ChannelFuture` — all usages now rely on the plain `Future` and `Promise` APIs.
* Add static factory methods to `DefaultPromise` that allow us to create promises that are initialised as successful or failed.
* Remove `CompleteFuture`, `SucceededFuture`, `FailedFuture`, `CompleteChannelFuture`, `SucceededChannelFuture`, and `FailedChannelFuture`.
* Remove `ChannelPromiseNotifier`.
Result:
Cleaner generics and more straight forward code.
2021-08-20 09:55:16 +02:00
|
|
|
import io.netty.util.concurrent.Promise;
|
2018-08-31 22:25:07 +02:00
|
|
|
import org.openjdk.jmh.annotations.Benchmark;
|
2019-06-04 14:17:23 +02:00
|
|
|
import org.openjdk.jmh.annotations.GroupThreads;
|
2018-08-31 22:25:07 +02:00
|
|
|
import org.openjdk.jmh.annotations.Setup;
|
|
|
|
import org.openjdk.jmh.annotations.TearDown;
|
|
|
|
|
Clean up Future/Promises API (#11575)
Motivation:
The generics for the existing futures, promises, and listeners are too complicated.
This complication comes from the existence of `ChannelPromise` and `ChannelFuture`, which forces listeners to care about the particular _type_ of future being listened on.
Modification:
* Add a `FutureContextListener` which can take a context object as an additional argument. This allows our listeners to have the channel piped through to them, so they don't need to rely on the `ChannelFuture.channel()` method.
* Make the `FutureListener`, along with the `FutureContextListener` sibling, the default listener API, retiring the `GenericFutureListener` since we no longer need to abstract over the type of the future.
* Change all uses of `ChannelPromise` to `Promise<Void>`.
* Change all uses of `ChannelFuture` to `Future<Void>`.
* Change all uses of `GenericFutureListener` to either `FutureListener` or `FutureContextListener` as needed.
* Remove `ChannelFutureListener` and `GenericFutureListener`.
* Introduce a `ChannelFutureListeners` enum to house the constants that previously lived in `ChannelFutureListener`. These constants now implement `FutureContextListener` and take the `Channel` as a context.
* Remove `ChannelPromise` and `ChannelFuture` — all usages now rely on the plain `Future` and `Promise` APIs.
* Add static factory methods to `DefaultPromise` that allow us to create promises that are initialised as successful or failed.
* Remove `CompleteFuture`, `SucceededFuture`, `FailedFuture`, `CompleteChannelFuture`, `SucceededChannelFuture`, and `FailedChannelFuture`.
* Remove `ChannelPromiseNotifier`.
Result:
Cleaner generics and more straight forward code.
2021-08-20 09:55:16 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2018-08-31 22:25:07 +02:00
|
|
|
public class EpollSocketChannelBenchmark extends AbstractMicrobenchmark {
|
2020-01-30 09:28:24 +01:00
|
|
|
private static final Runnable runnable = () -> { };
|
2018-08-31 22:25:07 +02:00
|
|
|
|
2019-01-23 08:32:05 +01:00
|
|
|
private EventLoopGroup group;
|
2018-08-31 22:25:07 +02:00
|
|
|
private Channel serverChan;
|
|
|
|
private Channel chan;
|
|
|
|
private ByteBuf abyte;
|
2021-08-31 16:06:34 +02:00
|
|
|
private Future<?> future;
|
2018-08-31 22:25:07 +02:00
|
|
|
|
|
|
|
@Setup
|
|
|
|
public void setup() throws Exception {
|
2019-01-23 08:32:05 +01:00
|
|
|
group = new MultithreadEventLoopGroup(1, EpollHandler.newFactory());
|
2018-08-31 22:25:07 +02:00
|
|
|
|
|
|
|
// add an arbitrary timeout to make the timer reschedule
|
2019-01-29 14:06:05 +01:00
|
|
|
future = group.schedule((Runnable) () -> {
|
|
|
|
throw new AssertionError();
|
2018-08-31 22:25:07 +02:00
|
|
|
}, 5, TimeUnit.MINUTES);
|
|
|
|
serverChan = new ServerBootstrap()
|
|
|
|
.channel(EpollServerSocketChannel.class)
|
|
|
|
.group(group)
|
|
|
|
.childHandler(new ChannelInitializer<Channel>() {
|
|
|
|
@Override
|
|
|
|
protected void initChannel(Channel ch) {
|
2019-03-28 10:28:27 +01:00
|
|
|
ch.pipeline().addLast(new ChannelHandler() {
|
2018-08-31 22:25:07 +02:00
|
|
|
@Override
|
|
|
|
public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
|
|
|
if (msg instanceof ByteBuf) {
|
2021-06-08 14:22:16 +02:00
|
|
|
ctx.writeAndFlush(msg);
|
2018-08-31 22:25:07 +02:00
|
|
|
} else {
|
|
|
|
throw new AssertionError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.bind(0)
|
2021-08-03 19:43:38 +02:00
|
|
|
.get();
|
2018-08-31 22:25:07 +02:00
|
|
|
chan = new Bootstrap()
|
|
|
|
.channel(EpollSocketChannel.class)
|
|
|
|
.handler(new ChannelInitializer<Channel>() {
|
|
|
|
@Override
|
|
|
|
protected void initChannel(Channel ch) {
|
2019-03-28 10:28:27 +01:00
|
|
|
ch.pipeline().addLast(new ChannelHandler() {
|
2018-08-31 22:25:07 +02:00
|
|
|
|
Clean up Future/Promises API (#11575)
Motivation:
The generics for the existing futures, promises, and listeners are too complicated.
This complication comes from the existence of `ChannelPromise` and `ChannelFuture`, which forces listeners to care about the particular _type_ of future being listened on.
Modification:
* Add a `FutureContextListener` which can take a context object as an additional argument. This allows our listeners to have the channel piped through to them, so they don't need to rely on the `ChannelFuture.channel()` method.
* Make the `FutureListener`, along with the `FutureContextListener` sibling, the default listener API, retiring the `GenericFutureListener` since we no longer need to abstract over the type of the future.
* Change all uses of `ChannelPromise` to `Promise<Void>`.
* Change all uses of `ChannelFuture` to `Future<Void>`.
* Change all uses of `GenericFutureListener` to either `FutureListener` or `FutureContextListener` as needed.
* Remove `ChannelFutureListener` and `GenericFutureListener`.
* Introduce a `ChannelFutureListeners` enum to house the constants that previously lived in `ChannelFutureListener`. These constants now implement `FutureContextListener` and take the `Channel` as a context.
* Remove `ChannelPromise` and `ChannelFuture` — all usages now rely on the plain `Future` and `Promise` APIs.
* Add static factory methods to `DefaultPromise` that allow us to create promises that are initialised as successful or failed.
* Remove `CompleteFuture`, `SucceededFuture`, `FailedFuture`, `CompleteChannelFuture`, `SucceededChannelFuture`, and `FailedChannelFuture`.
* Remove `ChannelPromiseNotifier`.
Result:
Cleaner generics and more straight forward code.
2021-08-20 09:55:16 +02:00
|
|
|
private Promise<Void> lastWritePromise;
|
2018-08-31 22:25:07 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
|
|
|
if (msg instanceof ByteBuf) {
|
|
|
|
|
|
|
|
ByteBuf buf = (ByteBuf) msg;
|
|
|
|
try {
|
|
|
|
if (buf.readableBytes() == 1) {
|
Clean up Future/Promises API (#11575)
Motivation:
The generics for the existing futures, promises, and listeners are too complicated.
This complication comes from the existence of `ChannelPromise` and `ChannelFuture`, which forces listeners to care about the particular _type_ of future being listened on.
Modification:
* Add a `FutureContextListener` which can take a context object as an additional argument. This allows our listeners to have the channel piped through to them, so they don't need to rely on the `ChannelFuture.channel()` method.
* Make the `FutureListener`, along with the `FutureContextListener` sibling, the default listener API, retiring the `GenericFutureListener` since we no longer need to abstract over the type of the future.
* Change all uses of `ChannelPromise` to `Promise<Void>`.
* Change all uses of `ChannelFuture` to `Future<Void>`.
* Change all uses of `GenericFutureListener` to either `FutureListener` or `FutureContextListener` as needed.
* Remove `ChannelFutureListener` and `GenericFutureListener`.
* Introduce a `ChannelFutureListeners` enum to house the constants that previously lived in `ChannelFutureListener`. These constants now implement `FutureContextListener` and take the `Channel` as a context.
* Remove `ChannelPromise` and `ChannelFuture` — all usages now rely on the plain `Future` and `Promise` APIs.
* Add static factory methods to `DefaultPromise` that allow us to create promises that are initialised as successful or failed.
* Remove `CompleteFuture`, `SucceededFuture`, `FailedFuture`, `CompleteChannelFuture`, `SucceededChannelFuture`, and `FailedChannelFuture`.
* Remove `ChannelPromiseNotifier`.
Result:
Cleaner generics and more straight forward code.
2021-08-20 09:55:16 +02:00
|
|
|
lastWritePromise.trySuccess(null);
|
2018-08-31 22:25:07 +02:00
|
|
|
lastWritePromise = null;
|
|
|
|
} else {
|
|
|
|
throw new AssertionError();
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
buf.release();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new AssertionError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-25 14:12:33 +02:00
|
|
|
public Future<Void> write(ChannelHandlerContext ctx, Object msg) {
|
2018-08-31 22:25:07 +02:00
|
|
|
if (lastWritePromise != null) {
|
|
|
|
throw new IllegalStateException();
|
|
|
|
}
|
2021-08-25 14:12:33 +02:00
|
|
|
lastWritePromise = ctx.newPromise();
|
|
|
|
return ctx.write(msg);
|
2018-08-31 22:25:07 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.group(group)
|
|
|
|
.connect(serverChan.localAddress())
|
2021-08-03 19:43:38 +02:00
|
|
|
.get();
|
2018-08-31 22:25:07 +02:00
|
|
|
|
|
|
|
abyte = chan.alloc().directBuffer(1);
|
|
|
|
abyte.writeByte('a');
|
|
|
|
}
|
|
|
|
|
|
|
|
@TearDown
|
|
|
|
public void tearDown() throws Exception {
|
|
|
|
chan.close().sync();
|
|
|
|
serverChan.close().sync();
|
|
|
|
future.cancel(true);
|
|
|
|
group.shutdownGracefully(0, 0, TimeUnit.SECONDS).sync();
|
|
|
|
abyte.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Benchmark
|
|
|
|
public Object pingPong() throws Exception {
|
|
|
|
return chan.pipeline().writeAndFlush(abyte.retainedSlice()).sync();
|
|
|
|
}
|
2019-06-04 14:17:23 +02:00
|
|
|
|
|
|
|
@Benchmark
|
|
|
|
public Object executeSingle() throws Exception {
|
2021-08-25 18:31:24 +02:00
|
|
|
return chan.executor().submit(runnable).get();
|
2019-06-04 14:17:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Benchmark
|
|
|
|
@GroupThreads(3)
|
|
|
|
public Object executeMulti() throws Exception {
|
2021-08-25 18:31:24 +02:00
|
|
|
return chan.executor().submit(runnable).get();
|
2019-06-04 14:17:23 +02:00
|
|
|
}
|
2018-08-31 22:25:07 +02:00
|
|
|
}
|