Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4212454
  • 博文数量: 291
  • 博客积分: 8003
  • 博客等级: 大校
  • 技术积分: 4275
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-30 18:28
文章分类

全部博文(291)

文章存档

2017年(1)

2013年(47)

2012年(115)

2011年(121)

2010年(7)

分类: Java

2011-06-21 12:18:05

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安装包

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.写代码
我的测试代码:
 
也可按照官方例子httpserver
把代码写好,官方例子是1.x的,我在附件里把例子里的import错误改过来
 
另外为了好测试,把ServerHandler.java改为(粗体那段是我加的):
  1. package org.apache.mina.example.httpserver.codec;
  2.   

  3.   import org.apache.mina.core.future.IoFutureListener;
  4. import org.apache.mina.core.service.IoHandler;
  5.   import org.apache.mina.core.service.IoHandlerAdapter;
  6. import org.apache.mina.core.session.IdleStatus;
  7. import org.apache.mina.core.session.IoSession;

  8.   
  9.   /**
  10.    * An {@link IoHandler} for HTTP.
  11.    *
  12.    * @author The Apache MINA Project (dev@mina.apache.org)
  13.    * @version $Rev: 588178 $, $Date: 2007-10-25 18:28:40 +0900 (?, 25 10? 2007) $
  14.    */
  15.   public class ServerHandler extends IoHandlerAdapter {
  16.       @Override
  17.       public void sessionOpened(IoSession session) {
  18.           // set idle time to 60 seconds

  19.           session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 60);
  20.       }
  21.   
  22.       @Override
  23.       public void messageReceived(IoSession session, Object message) {
  24.           // Check that we can service the request context

  25.           HttpResponseMessage response = new HttpResponseMessage();
  26.           response.setContentType("text/plain");
  27.           response.setResponseCode(HttpResponseMessage.HTTP_STATUS_SUCCESS);
  28.           response.appendBody("CONNECTED");
  29.   
  30.           // msg.setResponseCode(HttpResponseMessage.HTTP_STATUS_SUCCESS);

  31.           // byte[] b = new byte[ta.buffer.limit()];

  32.           // ta.buffer.rewind().get(b);

  33.           // msg.appendBody(b);

  34.           // System.out.println("####################");

  35.           // System.out.println(" GET_TILE RESPONSE SENT - ATTACHMENT GOOD DIAMOND.SI="+d.si+

  36.           // ", "+new java.text.SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss.SSS").format(new java.util.Date()));

  37.           // System.out.println("#################### - status="+ta.state+", index="+message.getIndex());

  38.   
  39.           // Unknown request

  40. // response = new HttpResponseMessage();

  41. // response.setResponseCode(HttpResponseMessage.HTTP_STATUS_NOT_FOUND);

  42. // response.appendBody(String.format(

  43. // "

    UNKNOWN REQUEST %d

    ",

  44. // HttpResponseMessage.HTTP_STATUS_NOT_FOUND));

  45.           
  46.         response = new HttpResponseMessage();
  47.         response.setResponseCode(HttpResponseMessage.HTTP_STATUS_SUCCESS);
  48.         response.appendBody("

    "+new java.text.SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss.SSS").format(new java.util.Date())+"

    "
    );
  49.   
  50.           if (response != null) {
  51.               session.write(response).addListener(IoFutureListener.CLOSE);
  52.           }
  53.       }
  54.   
  55.       @Override
  56.       public void sessionIdle(IoSession session, IdleStatus status) {
  57.           //IoSessionLogger.getLogger(session).info("Disconnecting the idle.");

  58.           session.close();
  59.       }
  60.   
  61.       @Override
  62.       public void exceptionCaught(IoSession session, Throwable cause) {
  63.           //IoSessionLogger.getLogger(session).warn(cause);

  64.           session.close();
  65.       }
  66.   }
4.打成jar包
先运行一下工程
点击工程右键->export->Runable jar file->选择“server-HelloServer”(若是没有的话先运行一下工程就有了)->finish
5.测试
把jar上传到linux(192.168.13.120)下
执行:
 
在另外一台机器(192.168.13.105)上测试:
1)1个连接,10万次请求
  1. ab -n 100000
测试结果:
  1. Concurrency Level: 1
  2. Time taken for tests: 117.718673 seconds
  3. Complete requests: 100000
  4. Failed requests: 0
  5. Write errors: 0
  6. Total transferred: 39000000 bytes
  7. HTML transferred: 6400000 bytes
  8. Requests per second: 849.48 [#/sec] (mean)
  9. Time per request: 1.177 [ms] (mean)
  10. Time per request: 1.177 [ms] (mean, across all concurrent requests)
  11. Transfer rate: 323.53 [Kbytes/sec] received
server的负载
  1. load average: 0.07, 0.02, 0.00
2)100个连接并发.10万次请求
  1. ab -n 100000 -c 100
  1. Concurrency Level: 100
  2. Time taken for tests: 9.999371 seconds
  3. Complete requests: 100000
  4. Failed requests: 0
  5. Write errors: 0
  6. Total transferred: 39003900 bytes
  7. HTML transferred: 6400640 bytes
  8. Requests per second: 10000.63 [#/sec] (mean)
  9. Time per request: 9.999 [ms] (mean)
  10. Time per request: 0.100 [ms] (mean, across all concurrent requests)
  11. Transfer rate: 3809.14 [Kbytes/sec] received
server负载
  1. load average: 0.62, 0.19, 0.06
3)1000个连接并发,10万次请求
  1. ab -n 100000 -c 1000
结果:
  1. Concurrency Level: 1000
  2. Time taken for tests: 19.697403 seconds
  3. Complete requests: 100000
  4. Failed requests: 0
  5. Write errors: 0
  6. Total transferred: 39000390 bytes
  7. HTML transferred: 6400064 bytes
  8. Requests per second: 5076.81 [#/sec] (mean)
  9. Time per request: 196.974 [ms] (mean)
  10. Time per request: 0.197 [ms] (mean, across all concurrent requests)
  11. Transfer rate: 1933.55 [Kbytes/sec] received
负载:
  1. load average: 1.76, 0.43, 0.14
结论:mina2.0在合理的负载下1000个并发,最高能达到5076.81rps(Requests per second),基本上能满足需求
end
阅读(13059) | 评论(1) | 转发(0) |
0

上一篇:论流行游戏

下一篇:庆贺博文过百

给主人留下些什么吧!~~

warrior7012012-03-27 21:36:18

文章也得很棒,学习了。。。