基于mina架构的JT/T808协议两种解码方式性能比较
方案一继承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行代码) }
相关推荐
cywhoyi 2020-11-23
rise 2020-11-22
sssdssxss 2020-11-20
xuedabao 2020-11-19
alien 2020-11-15
JLow 2020-11-12
ruancw 2020-11-10
地平线 2020-11-02
yinren 2020-11-02
evolone 2020-10-29
liupengqwert 2020-10-28
acaoye 2020-10-27
jyj0 2020-10-27
ruancw 2020-10-27
JAVA飘香 2020-10-26
withjeffrey 2020-10-23
litefish 2020-10-16
richermen 2020-10-15
kjyiyi 2020-10-10