mina的HTTP性能测试

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安装包
打开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.写代码
我的测试代码:
 
把代码写好,官方例子是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 ([email protected])
  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. // "<html><body><h1>UNKNOWN REQUEST %d</h1></body></html>",

  44. // HttpResponseMessage.HTTP_STATUS_NOT_FOUND));

  45.           
  46.         response = new HttpResponseMessage( ) ;
  47.         response. setResponseCode( HttpResponseMessage. HTTP_STATUS_SUCCESS) ;
  48.         response. appendBody( "<html><body><h1>" + new java . text . SimpleDateFormat ( "EEE, dd MMM yyyy HH:mm:ss.SSS" ) . format ( new java . util . Date ( ) ) + "</h1></body></html>" ) ;
  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)下
执行:
mina的HTTP性能测试_第1张图片
 
在另外一台机器(192.168.13.105)上测试:
1)1个连接,10万次请求
  1. ab -n 100000 http://192.168.13.120:8080/
测试结果:
  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 http://192.168.13.120:8080/
  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 http://192.168.13.120:8080/
结果:
  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

转:http://blog.chinaunix.net/uid-11121450-id-385713.html

你可能感兴趣的:(Mina)