基于mina架构的JTT808协议两种解码方式性能比较

方案一继承CumulativeProtocolDecoder,实现doDecode方法进行解码;

方案二继承ProtocolDecoder ,实现decode方法进行解码;

在一台普通pc机(默认配置的Eclipse中直接运行测试程序)上测试客户端2k并发,方案一35分钟勉强达到达到150W数量,方案二只用了32分钟就已经达到150W数量。


方案一:

public class JTT808CodecDecoder  extends CumulativeProtocolDecoder {
	
         ……(此处省略N行代码)
protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)throws Exception { boolean matched=false; int start=in.position(); int limit=in.limit(); while (in.hasRemaining()) { byte b=in.get(); if(!matched){ if(b==JTT808Message.FLAG){ matched=true; start=in.position()-1; } continue; } if(b!=JTT808Message.FLAG) continue; int pos = in.position(); try{ if(in.hasRemaining()){ b=in.get(); in.position(start); in.limit(pos); if(b==JTT808Message.FLAG){ decode(in,out); }else{ illegalMessage(in); } }else{ in.position(start); in.limit(pos); decode(in,out); } }catch(Exception e){ illegalMessage(in); logger.error(e.getMessage(),e); }finally{ in.limit(limit); in.position(pos); } return true; } if(matched){ in.limit(limit); in.position(start); return false; } return true; } private void decode(IoBuffer buf,ProtocolDecoderOutput out){ int size=buf.limit()-buf.position(); byte[] bytes = new byte[size]; buf.get(bytes,0, bytes.length);
  		……(此处省略N行代码)


}
  	……(此处省略N行代码)

}
 
  方案二: 
  

public class JTT808CodecDecoder implements ProtocolDecoder {
	
         ……(此处省略N行代码)

	//此解码方式性能更高
	public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)throws Exception {
		  Context ctx = getContext(session);
		  boolean mark=false;
		  if(ctx.getBuf().position()>0){
			  if(ctx.getBuf().get(0)==JTT808Message.FLAG){
					 mark=true;
			  }else{
				  ctx.getBuf().clear(); 
			  }
		  }
		  while (in.hasRemaining()) {
			    byte b=in.get();
			    ctx.getBuf().put(b);	    
	            if(b!=JTT808Message.FLAG) continue;
	            if(mark){
            		if(in.hasRemaining()){
            			b=in.get();
            			if(b==JTT808Message.FLAG){
            				decode(ctx.getBuf(),out);
            			}else{
            				logger.error("illegal message:"+bytesToHexString(ctx.getBuf().array()));
            				out.write("-1");
            			}
            			ctx.getBuf().clear(); 
        				ctx.getBuf().put(b);
        				mark=true;
            		}else{
            			decode(ctx.getBuf(),out);
            			ctx.getBuf().clear();
            		}
	            }else{
	            	mark=true;  
	            }
	     }
    }
	
	private void decode(IoBuffer buf,ProtocolDecoderOutput out){
		int size=buf.position();
		buf.flip();
		byte[] bytes = new byte[size];
		buf.get(bytes);

                ……(此处省略N行代码)
        }
       ……(此处省略N行代码)
}


你可能感兴趣的:(基于mina架构的JTT808协议两种解码方式性能比较)