压测准备
- 需要将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个线程来模拟客户端进行压测
原文转自 http://dueam.org/
相关推荐
184 C10K问题及NIO精讲和IO模型性能压测 地址 185 网络编程之多路复用器及Epoll精讲 地址 187 网络编程java API 实战多路复用器开发 地址 189 全手写急速理解Netty模型及IO模型应用实战 地址 191 Netty之IO模型...
Dubbo 在此之上提供了高效的序列化、网络通信库 Netty 和异步 I/O 模型 (BIO、NIO、AIO) 的选择,以优化性能。 **4. Dubbo框架 (2.7 版本)** Dubbo 2.7 版本增强了服务治理和运维能力,例如节点角色的定义、调用...
**7.20 NIO技术之-Netty** - **NIO基本概念及BIO、AIO的对比分析**:理解NIO、BIO和AIO之间的区别。 - **Netty实现IM聊天系统**:基于Netty开发即时通讯系统。 **7.21 分布式缓存技术-Redis** - **Redis的安装及...
python教程学习
tdac034.pdf
veclibm111111111111
nssm工具,进行2次解压后即可
视讯镜头优化,操作不走
【数据结构】的资源,包括书籍、在线课程和网站
北京大学-DeepSeek系列-提示词工程和落地场景.pdf.png
【财信国际经济研究院-2025研报】2025年2月CPI和PPI数据点评:通胀低于预期,内需亟待提振.pdf
【毕业设计】java-springboot+vue精品在线试题库系统源码(完整前后端+mysql+说明文档+LunW).zip
python安装-25.求 a 的 b 次方——有点不好意思哈.py
代码实现了自适应IMM(交互式多模型)算法,专注于对目标状态进行估计,并结合了匀速(CV)和匀加速(CA)运动模型。它使用自适应观测噪声估计和扩展卡尔曼滤波器(EKF)作为滤波主体,并在最后提供误差统计特性输出。
中产品工业物联网联网的基础功能,是企业开启数字化转型的入门之选。它能帮助企业快速搭建起基础的设备数据采集与监控体系,以较低成本实
北京大学DeepSeek系列-DeepSeek与AIGC应用.pdf.png
【毕业设计-java】springboot-vue基于多维分类的知识管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
## 一、数据介绍 本数据参考C刊《管理评论》佟岩(2024)老师的做法,使用各年度省级政府工作报告中“减碳”关键词的词频总数来测度地方政府对“减碳”的重视程度,“减碳”关键词包括二氧化碳、低碳、减排、节能、能耗、环境保护(环保)、生态、绿色。 本数据包含:原始数据、参考文献、代码do文件、最终结果。 ## 二、参考文献: 佟岩,李鑫,徐国铨.企业集团碳减排压力与债务分布-来自高耗能上市公司的经验证据[J].管理评论,2024,36(02):210-221.DOI:10.14120/j.cnki.cn11-5057/f.2024.02.014 ## 三、相关数据:城市、城市编码、年份、减碳重视程度词频。
python教程学习
jfinal-undertow 用于开发、部署由 jfinal 开发的 web 项目