【发布时间】:2018-05-09 03:27:47
【问题描述】:
我创建了一个具有多个工作线程的 netty 服务器,以检查线程数量的增加如何改变吞吐量。 这是我使用的代码。它是Writing and echo server 的略微修改版本,可以在 Netty 网站上找到。
回声服务器计算
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class EchoServerCompute {
private int port;
public EchoServerCompute(int port) {
this.port = port;
}
public void run(int threadCount) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup(threadCount);
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoServerComputeHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
int port = 8080;
new EchoServerCompute(port).run(Integer.parseInt(args[0]));
}
}
EchoServerComputeHandler
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.lang.Math;
import java.math.BigInteger;
public class EchoServerComputeHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
BigInteger result = BigInteger.ONE;
for (int i=0; i<2000; i++)
result = result.multiply(BigInteger.valueOf(i));
ctx.write(msg);
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
我使用 5 个工作线程和 50 个工作线程运行此服务器,并使用 JMeter 和 1000 个用户对其进行测试。但我在这两种情况下收到的吞吐量几乎相同。
我希望在使用更多工作线程时看到吞吐量增加。如果我在这里做错了什么,有人可以告诉我吗?
编辑
我运行它的测试环境有 2 个节点,分别指定为服务器和客户端。 Server 节点运行 netty 程序,Client 节点运行 JMeter。该服务器具有 Intel Xeon 5160 CPU 和 16GB RAM。客户端有一个 Intel Xeon E5506 CPU 和 8GB RAM。它们之间的链路是 1Gbps。
【问题讨论】:
-
你弄清楚发生这种情况的原因了吗?
标签: java multithreading netty