基于mina架构的JTT808协议两种解码方式性能比较
2014-11-26 11:34
477 查看
方案一继承CumulativeProtocolDecoder,实现doDecode方法进行解码;
方案二继承ProtocolDecoder ,实现decode方法进行解码;
在一台普通pc机(默认配置的Eclipse中直接运行测试程序)上测试客户端2k并发,方案一35分钟勉强达到达到150W数量,方案二只用了32分钟就已经达到150W数量。
方案一:
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);
}
}
方案二:
方案二继承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行代码) }
相关文章推荐
- 转:几种基于HTTP协议的RPC性能比较,以及ICE接口性能测试比较
- Tomcat 6 支持 NIO -- Tomcat的四种基于HTTP协议的Connector性能比较
- mina两种协议获取方式(TCP/UDP)
- 使用SQL Server 2005 新的语法ROW_NUMBER()进行分页的两种不同方式的性能比较
- Tomcat的四种基于HTTP协议的Connector性能比较
- 基于两种方式的cs架构开发技术
- MySQL之——提升Replication性能的两种架构方式
- Tomcat的四种基于HTTP协议的Connector性能比较
- 几种基于HTTP协议的RPC性能比较
- Tomcat的四种基于HTTP协议的Connector性能比较
- HashMap 遍历的两种方式及性能比较
- 两种AOP实现方式的性能比较 (转)
- Tomcat 6 支持 NIO -- Tomcat的四种基于HTTP协议的Connector性能比较
- 使用SQL Server 2005 新的语法ROW_NUMBER()进行分页的两种不同方式的性能比较
- 数组求和的两种方式与性能比较
- 《Spark商业案例与性能调优实战100课》第9课:商业案例之通过Spark SQL 下两种不同方式实现口碑最佳和最热门电影比较
- Tomcat的四种基于HTTP协议的Connector性能比较
- 使用SQL Server 2005 新的语法ROW_NUMBER()进行分页的两种不同方式的性能比较
- Tomcat 6 支持 NIO -- Tomcat的四种基于HTTP协议的Connector性能比较(转载)
- Tomcat的四种基于HTTP协议的Connector性能比较