MINA 并发数 性能测

mina使用jmeter压力测试:500并发没有问题,但是500以上,机器就卡的不行,一启动,反应半天才运行,并发测试没法继续下去了。

测试mina最大连接数:

package com.blazefire.client;

import java.net.InetSocketAddress;
import java.util.Arrays;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import com.blazefire.bean.BaseMessage;
import com.blazefire.bean.MessageBean;
import com.blazefire.util.BeanUtil;
import com.blazefire.util.MathProtocolCodecFactory;

public class Client {
	
	public IoConnector creatClient(){
		IoConnector connector=new NioSocketConnector(); 
		connector.setConnectTimeoutMillis(30000); 
		connector.getFilterChain().addLast("codec", 
		new ProtocolCodecFilter(new MathProtocolCodecFactory(false)));
		connector.setHandler(new ClientHandler());
		return connector;
	}
	public IoSession getIOSession(IoConnector connector){
		ConnectFuture future = connector.connect(new InetSocketAddress("192.168.1.112", 9123)); 
		// 等待是否连接成功,相当于是转异步执行为同步执行。 
		future.awaitUninterruptibly(); 
		// 连接成功后获取会话对象。 如果没有上面的等待, 由于connect()方法是异步的, session可能会无法获取。 
		IoSession session = null;
		try{
			session = future.getSession();
		}catch(Exception e){
			e.printStackTrace();
		}
		return session;
	}
	public void sendMsg(IoSession session,String msg){
		BaseMessage baseMessage = new BaseMessage();
		MessageBean msgBean = new MessageBean();
		msgBean.setMessage(msg);
		baseMessage.setDataType(BeanUtil.MSG);
		baseMessage.setData(msgBean);
		session.write(baseMessage);
	}
//	public void sendByte(IoSession session,byte[] byteArray,String timeMsg){
//		BaseMessage baseMessage = new BaseMessage();
//		MessageBean msgBean = new MessageBean();
//		msgBean.setMsgByte(byteArray);
//		baseMessage.setDataType(BeanUtil.MSG);
//		baseMessage.setData(msgBean);
//		session.write(baseMessage);
//	}
	public static void main(String[] args) {
		
					// TODO Auto-generated method stub
//		Client  client = new Client();
//		IoConnector connector = client.creatClient();
//		for(int i=0;i<20000;i++){
//			IoSession session = client.getIOSession(connector);
//		}
		for(int i=0;i<4000;i++){
			Client  client = new Client();
			IoConnector connector = client.creatClient();
			IoSession session = client.getIOSession(connector);
			client.sendMsg(session,Arrays.toString(new byte[1000])+":"+System.currentTimeMillis());
			i++;
		}
//		Client  client = new Client();
//		IoConnector connector = client.creatClient();
//		for(int i=0;i<16000;i++){
//			IoSession session = client.getIOSession(connector);
//		}
	}
//		Client  client = new Client();
//		IoConnector connector = client.creatClient();
//		for(int i=0;i<10000;i++){
//			IoSession session = client.getIOSession(connector);
//		}
//		IoSession session = client.getIOSession(connector);
//		client.sendMsg( session,System.currentTimeMillis()+"");
	}

只创建一次IoConnector,循环获取IoSession,

自己的程序只能运行到:16000的连接,再上去就报错。

官网的例子可以运行50000多的连接。http://mina.apache.org/report/trunk/xref/org/apache/mina/example/sumup/

      然后模拟真实情况:

		for(int i=0;i<4000;i++){
			Client  client = new Client();
			IoConnector connector = client.creatClient();
			IoSession session = client.getIOSession(connector);
			client.sendMsg(session,Arrays.toString(new byte[1000])+":"+System.currentTimeMillis());
			i++;
		}
发现自己的电脑运行上面那段程序只能开启2000多个连接,再开连接就报错。原因应该是操作系统线程数有限制。

没得办法,就一台电脑开2000个连接,两台电脑运行测试:

测试计划:4000连接数 每隔一秒发送一个1K数据包 连续运行一段时间,根据日志分析接收时间

运行半个小时后,程序划分时间90%都是1000,因为线程睡眠1S,所以减去1000,基本上没有时间消耗

说明4000连接数完全没问题。

统计分析:

你可能感兴趣的:(MINA,mina,测试)