Программисты JAVA общаются здесь
Anonymous
Рабочие потоки и пропускная способность Netty
Сообщение
Anonymous » 26 июл 2024, 03:27
Я создал сервер Netty с несколькими рабочими потоками, чтобы проверить, как увеличение количества потоков меняет пропускную способность.
Это код, который я использовал. Это слегка измененная версия сервера записи и эха, которую можно найти на веб-сайте Netty.
EchoServerCompute
Код: Выделить всё
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() {
@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
Подробнее здесь: [url]https://stackoverflow.com/questions/50245088/netty-worker-threads-and-throughput[/url]
1721953651
Anonymous
Я создал сервер Netty с несколькими рабочими потоками, чтобы проверить, как увеличение количества потоков меняет пропускную способность.[b]Это код, который я использовал. Это слегка измененная версия сервера записи и эха, которую можно найти на веб-сайте Netty. EchoServerCompute [code]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() { @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])); } } [/code] EchoServerComputeHandler [code]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 Подробнее здесь: [url]https://stackoverflow.com/questions/50245088/netty-worker-threads-and-throughput[/url]