Apache MINA(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络应用程序提供了非常便利的框架。当前发行的 MINA 版本支持基于 Java NIO 技术的 TCP/UDP 应用程序开发、串口通讯程序(只在最新的预览版中提供),MINA 所支持的功能也在进一步的扩展中。
新项目里的web service需要高并发服务能力,tomcat目前最多支持到200个并发。远达不到要求。
0.测试环境:
client和server:CPU:单核2G,内存:4GB;
网络:局域网
1.下载mina2.0安装包
http://labs.renren.com/apache-mirror//mina/2.0.4/apache-mina-2.0.4-bin.tar.gz
http://mina.apache.org/downloads.html
2.解压后
2.解压后
打开eclipse建立工程HelloServer,把mina-core-2.0.4.jar,mina-filter-compression-2.0.4.jar
,slf4j-api-1.6.1.jar导入,注意java必须要1.5以上
3.写代码
我的测试代码:
- package org.apache.mina.example.httpserver.codec;
- import org.apache.mina.core.future.IoFutureListener;
- import org.apache.mina.core.service.IoHandler;
- import org.apache.mina.core.service.IoHandlerAdapter;
- import org.apache.mina.core.session.IdleStatus;
- import org.apache.mina.core.session.IoSession;
- /**
- * An {@link IoHandler} for HTTP.
- *
- * @author The Apache MINA Project ([email protected])
- * @version $Rev: 588178 $, $Date: 2007-10-25 18:28:40 +0900 (?, 25 10? 2007) $
- */
- public class ServerHandler extends IoHandlerAdapter {
- @Override
- public void sessionOpened(IoSession session) {
- // set idle time to 60 seconds
- session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 60);
- }
- @Override
- public void messageReceived(IoSession session, Object message) {
- // Check that we can service the request context
- HttpResponseMessage response = new HttpResponseMessage();
- response.setContentType("text/plain");
- response.setResponseCode(HttpResponseMessage.HTTP_STATUS_SUCCESS);
- response.appendBody("CONNECTED");
- // msg.setResponseCode(HttpResponseMessage.HTTP_STATUS_SUCCESS);
- // byte[] b = new byte[ta.buffer.limit()];
- // ta.buffer.rewind().get(b);
- // msg.appendBody(b);
- // System.out.println("####################");
- // System.out.println(" GET_TILE RESPONSE SENT - ATTACHMENT GOOD DIAMOND.SI="+d.si+
- // ", "+new java.text.SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss.SSS").format(new java.util.Date()));
- // System.out.println("#################### - status="+ta.state+", index="+message.getIndex());
- // Unknown request
- // response = new HttpResponseMessage();
- // response.setResponseCode(HttpResponseMessage.HTTP_STATUS_NOT_FOUND);
- // response.appendBody(String.format(
- // "
UNKNOWN REQUEST %d
",- // HttpResponseMessage.HTTP_STATUS_NOT_FOUND));
- response = new HttpResponseMessage();
- response.setResponseCode(HttpResponseMessage.HTTP_STATUS_SUCCESS);
- response.appendBody("
"
+new java.text.SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss.SSS").format(new java.util.Date())+"");- if (response != null) {
- session.write(response).addListener(IoFutureListener.CLOSE);
- }
- }
- @Override
- public void sessionIdle(IoSession session, IdleStatus status) {
- //IoSessionLogger.getLogger(session).info("Disconnecting the idle.");
- session.close();
- }
- @Override
- public void exceptionCaught(IoSession session, Throwable cause) {
- //IoSessionLogger.getLogger(session).warn(cause);
- session.close();
- }
- }
也可按照官方例子httpserver http://mina.apache.org/report/trunk/xref/org/apache/mina/example/httpserver/
把代码写好,官方例子是1.x的,我在附件里把例子里的import错误改过来
另外为了好测试,把ServerHandler.java改为(粗体那段是我加的):
4.打成jar包
先运行一下工程
点击工程右键->export->Runable jar file->选择“server-HelloServer”(若是没有的话先运行一下工程就有了)->finish
5.测试
把jar上传到linux(192.168.13.120)下
执行:
在另外一台机器(192.168.13.105)上测试:
1)1个连接,10万次请求
- ab -n 100000 http://192.168.13.120:8080/
测试结果:
- Concurrency Level: 1
- Time taken for tests: 117.718673 seconds
- Complete requests: 100000
- Failed requests: 0
- Write errors: 0
- Total transferred: 39000000 bytes
- HTML transferred: 6400000 bytes
- Requests per second: 849.48 [#/sec] (mean)
- Time per request: 1.177 [ms] (mean)
- Time per request: 1.177 [ms] (mean, across all concurrent requests)
- Transfer rate: 323.53 [Kbytes/sec] received
server的负载
- load average: 0.07, 0.02, 0.00
2)100个连接并发.10万次请求
- ab -n 100000 -c 100 http://192.168.13.120:8080/
- Concurrency Level: 100
- Time taken for tests: 9.999371 seconds
- Complete requests: 100000
- Failed requests: 0
- Write errors: 0
- Total transferred: 39003900 bytes
- HTML transferred: 6400640 bytes
- Requests per second: 10000.63 [#/sec] (mean)
- Time per request: 9.999 [ms] (mean)
- Time per request: 0.100 [ms] (mean, across all concurrent requests)
- Transfer rate: 3809.14 [Kbytes/sec] received
server负载
- load average: 0.62, 0.19, 0.06
3)1000个连接并发,10万次请求
- ab -n 100000 -c 1000 http://192.168.13.120:8080/
结果:
- Concurrency Level: 1000
- Time taken for tests: 19.697403 seconds
- Complete requests: 100000
- Failed requests: 0
- Write errors: 0
- Total transferred: 39000390 bytes
- HTML transferred: 6400064 bytes
- Requests per second: 5076.81 [#/sec] (mean)
- Time per request: 196.974 [ms] (mean)
- Time per request: 0.197 [ms] (mean, across all concurrent requests)
- Transfer rate: 1933.55 [Kbytes/sec] received
负载:
- load average: 1.76, 0.43, 0.14
结论:mina2.0在合理的负载下1000个并发,最高能达到5076.81rps(Requests per second),基本上能满足需求
end