我正在使用netty从服务器下载大型文件。在使用starts运行性能测试时,我发现我的服务器提供了非常高的吞吐量,直到150个并发用户,但是当并发用户数量增加时,它就开始下降,在500个并发用户上几乎减半。
NettyServer -
ServerBootstrap b = new ServerBootstrap();
EpollEventLoopGroup bossGroup = new EpollEventLoopGroup();
EpollEventLoopGroup workerGroup = new EpollEventLoopGroup();
try {
b.group(bossGroup, workerGroup).channel(EpollServerSocketChannel.class).handler(new LoggingHandler(LogLevel.DEBUG)).childHandler(new FileServerInitializer());
Channel ch = b.bind(port).sync().channel();
ch.closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
FileServerHandler.threadPool.shutdownNow();
}FileServerInitializer -
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
pipeline.addLast("encoder", new HttpResponseEncoder());
pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
pipeline.addLast("handler", (ChannelHandler) new FileServerHandler());
}FileServerHandler -
RandomAccessFile raf;
try {
raf = new RandomAccessFile(file, "r");
} catch (FileNotFoundException ignore) {
sendError(ctx, HttpResponseStatus.NOT_FOUND);
return;
}
response = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK);
response.headers().set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileNameType);
response.headers().set(HttpHeaders.CONTENT_LENGTH, String.valueOf(size));
ctx.write(response);
ChannelFuture sendFileFuture;
ChannelFuture lastContentFuture;
sendFileFuture = ctx.writeAndFlush(new HttpChunkedInput(new ChunkedFile(raf, 0, size, 8192)),ctx.newProgressivePromise());
lastContentFuture = sendFileFuture;
sendFileFuture.addListener(new ChannelProgressiveFutureListener() {
public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) {
if (total < 0) {
System.out.println(future.channel() + " Transfer progress: " + progress);
}else {
System.out.println(future.channel() + " Transfer progress: " + progress + " / " + total);
}
}
public void operationComplete(ChannelProgressiveFuture future) {
System.out.println(future.channel() + " Transfer complete.");
}
});
lastContentFuture.addListener(ChannelFutureListener.CLOSE);有人能告诉我为什么会这样吗?
发布于 2017-01-18 18:30:14
你也可以试着:
当然也会看到其他的建议
https://stackoverflow.com/questions/41697296
复制相似问题