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

本文对比了两种解码方案在Eclipse环境下客户端2k并发下的性能表现,结果显示方案二在达到150W数量时比方案一更快。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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


方案一:

public class JTT808CodecDecoder  extends CumulativeProtocolDecoder {
	<pre name="code" class="java">         ……(此处省略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);
  <span style="white-space:pre">		</span>……(此处省略N行代码)


}
  <span style="white-space:pre">	</span>……(此处省略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行代码)
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值