久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

Netty NIO框架性能壓測之如何實現長鏈接

171次閱讀
沒有評論

共計 4460 個字符,預計需要花費 12 分鐘才能閱讀完成。

這篇文章將為大家詳細講解有關 Netty NIO 框架性能壓測之如何實現長鏈接,丸趣 TV 小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

壓測準備

需要將 ulimit -n 改大,否則 nio 鏈接開不大。

準備 4 臺機器 (1 臺 netty 服務器,3 臺壓測機)

使用 apache 的 ab 做壓測工具

開始干活

壓測代碼:

package org.dueam.sample.netty;
package org.dueam.sample.netty;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Executors;
 
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.DynamicChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.ChannelHandler.Sharable;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
 
public class ChatServer {
 
 public static void main(String[] args) throws Exception {if(args.length  1){args = new String[]{ 9876 , true 
 ChannelFactory factory = new NioServerSocketChannelFactory(Executors
 .newCachedThreadPool(), Executors.newCachedThreadPool());
 
 ServerBootstrap bootstrap = new ServerBootstrap(factory);
 
 ChatServerHandler handler = new ChatServerHandler();
 ChannelPipeline pipeline = bootstrap.getPipeline();
 pipeline.addLast(chat , handler);
 
 bootstrap.setOption(child.tcpNoDelay , true);
 bootstrap.setOption(child.keepAlive , true);
 int port = Integer.valueOf(args[0]);
 bootstrap.bind(new InetSocketAddress(port));
 
 boolean fillChat =  true .equals(args[1]);
 if (fillChat) {ChannelManagerThread cmt = new ChannelManagerThread();
 cmt.start();
 
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 while (true) {String command = br.readLine();
 if (dump .equals(command)) {System.out.println( 當前活著的數量:  + channel.size());
 } else if (help .equals(command)) {
 System.out.println( 命令列表: 
 System.out.println( dump: 打印當前情況 
 System.out.println( help: 幫助文檔 
 
 final static Random random = new Random();
 static int max = 0;
 static class ChannelManagerThread extends Thread {
 @Override
 public void run() {while (true) {
 try {if(max   channel.size()){max = channel.size() ;
 System.out.println(live: +channel.size());
 
 for (Channel s : channel.values()) {if (random.nextInt(100) 70) {ChannelBuffer cb = new DynamicChannelBuffer(256);
 cb.writeBytes(Hey! 有人來找你了! .getBytes());
 s.write(cb);
 sleep(500);
 } catch (InterruptedException e) {
 
 
 final static Map Integer, Channel  channel = new HashMap Integer, Channel 
 
 static void log(String message) {System.out.println(message);
 
 @Sharable
 static class ChatServerHandler extends SimpleChannelHandler {
 @Override
 public void channelConnected(ChannelHandlerContext ctx,
 ChannelStateEvent e) {Channel ch = e.getChannel();
 ChannelBuffer cb = new DynamicChannelBuffer(256);
 cb.writeBytes(Hell! 你來了啊! .getBytes());
 ch.write(cb);
 channel.put(e.getChannel().getId(), e.getChannel());
 
 
 @Override
 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
 
 @Override
 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {e.getCause().printStackTrace();
 channel.remove(e.getChannel().getId());
 log(remove channel by exception! id:  + e.getChannel().getId());
 
 e.getChannel().close();
 
 @Override
 public void channelDisconnected(ChannelHandlerContext ctx,
 ChannelStateEvent e) throws Exception {channel.remove(e.getChannel().getId());
 log(remove channel by exception! id:  + e.getChannel().getId());
 
}

壓測方式:

# 加大超時和并發量,并使用 keep-alive 的方式保持住端口
./ab -n 20000 -c 20000 -k -t 999999999 -r http://192.168.216.30:9876/

壓測結果

內存損耗:

[root@cap216030 ~]# free -k -t -s 10
--  原始內存
 total used free shared buffers cached
Mem: 4149076 189828 3959248 0 13196 95484
-/+ buffers/cache: 81148 4067928
Swap: 2096472 208 2096264
Total: 6245548 190036 6055512
 
--  執行  chat server 之后
 total used free shared buffers cached
Mem: 4149076 207236 3941840 0 13216 96244
-/+ buffers/cache: 97776 4051300
Swap: 2096472 208 2096264
Total: 6245548 207444 6038104
 
-- 59471  個 nio 連接之后
 total used free shared buffers cached
Mem: 4149076 474244 3674832 0 13328 96132
-/+ buffers/cache: 364784 3784292
Swap: 2096472 208 2096264
Total: 6245548 474452 5771096

結論:

Netty nio 可以輕松將鏈接開到 6W,每個線程大概損壞 5k 左右的系統內存

后續壓測方案

編寫 Java 客戶端做內容實時雙向推送

使用 100 臺機器每臺機器起 1000 個線程來模擬客戶端進行壓測

關于“Netty NIO 框架性能壓測之如何實現長鏈接”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-08-16發表,共計4460字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 合肥市| 林口县| 镇宁| 丹江口市| 会同县| 富阳市| 巴林左旗| 日喀则市| 若羌县| 施甸县| 区。| 汪清县| 东乡县| 富裕县| 深水埗区| 和龙市| 福建省| 博乐市| 兴安盟| 辽宁省| 阿拉善盟| 莎车县| 元江| 中宁县| 敖汉旗| 巴东县| 西和县| 清流县| 沙河市| 游戏| 宁德市| 梓潼县| 井陉县| 韶山市| 孟津县| 德安县| 司法| 福海县| 东安县| 黑龙江省| 永靖县|