为什么Netty提供EpollEventLoopGroup?

3

经过我的测试,Netty的NioEventLoopGroupEpollEventLoopGroup在性能上没有区别。那么为什么Netty还提供EpollEventLoopGroup呢?

当服务器维护1000个TCP连接时,服务器的CPU和内存之间没有区别。以下是我的代码:

public class EpollServer {
        public static void main(String[] args) throws Exception {
            logger.info("port:"+System.getProperty("port", "8007"));
            logger.info("isUseEpoll:"+System.getProperty("isUserEpoll","true"));
            EventLoopGroup bossGroup;
            EventLoopGroup workerGroup;
            Class          clazz;
    
            if (useEpoll()) {
                bossGroup = new EpollEventLoopGroup(1);
                workerGroup = new EpollEventLoopGroup();
                clazz = EpollServerSocketChannel.class;
            }else{
                bossGroup = new NioEventLoopGroup(1);
                workerGroup = new NioEventLoopGroup();
                clazz = NioServerSocketChannel.class;
            }
    
            final EpollServerHandler epollHandler = new EpollServerHandler();
            try {
                ServerBootstrap b = new ServerBootstrap();
                logger.info("[socket Type]: {}", clazz.getSimpleName());
                b.group(bossGroup, workerGroup)
                        .channel(clazz)
                        .option(ChannelOption.SO_BACKLOG, 10001)
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            public void initChannel(SocketChannel ch) throws Exception {
                                ChannelPipeline p = ch.pipeline();
                                p.addLast(new LengthFieldBasedFrameDecoder(1024,0,2,0,2));
                                p.addLast(new SimpleChannelInboundHandler(){
                                    @Override
                                    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
                                        ByteBuf content = (ByteBuf) msg;
                                        ctx.channel().writeAndFlush(content.retain());
                                    }
                                });
    
                            }
                        });
   
                ChannelFuture f = b.bind(PORT).sync();
                f.channel().closeFuture().sync();
            } finally {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
        }


    private static boolean useEpoll() {
        if(isUseEpoll){
            return Epoll.isAvailable();
        }else{
            return false;
        }
    }
}
1个回答

0
我可以告诉你,在“高负载”情况下,以下方面绝对存在性能差异:
  • CPU使用率
  • 内存使用量
  • 分配数量
此外,Native epoll传输还支持许多NIO本身没有暴露的功能。

我阅读了官方维基:Native-transports。维基没有提到性能提升了多少,我也进行了测试。我该如何测试以展示epolleventloopgroup的好处呢?谢谢。 - JaneWen Chan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接