Redis JdkSerializationRedisSerializer,stringRedisSerializer,ProtoBuf 体积,性能简单比较.
/** * User: laizhenwei * Date: 2018-04-10 Time: 14:17 * Description: */ @RunWith(SpringRunner.class) @SpringBootTest public class MessageProtoTest { //ProtoBuf @Resource private RedisTemplate<String,MessageProto.Message> messageProtoRedisTemplate; //JDK @Resource private RedisTemplate<String,MessageProto.Message> redisTemplate; //String json @Resource private RedisTemplate<String,String> jsonRedisTemplate; //JDK @Test public void add(){ BoundSetOperations<String, MessageProto.Message> operations = redisTemplate.boundSetOps("message1"); long begin = System.nanoTime(); for(int i = 0;i<100 ;i++){ MessageProto.Message.Builder builder = MessageProto.Message.newBuilder(); builder.setId(i); builder.setLength(10); builder.setMsgBody("你好啊"); builder.setType(MessageProto.Message.Type.ACTIVE_VALUE); MessageProto.Message message = builder.build(); operations.add(message); } System.out.println((System.nanoTime() - begin)/1000000); } //ProtoBuf @Test public void add2(){ BoundSetOperations<String, MessageProto.Message> operations = messageProtoRedisTemplate.boundSetOps("message2"); long begin = System.nanoTime(); for(int i = 0;i<100 ;i++){ MessageProto.Message.Builder builder = MessageProto.Message.newBuilder(); builder.setId(i); builder.setLength(10); builder.setMsgBody("你好啊"); builder.setType(MessageProto.Message.Type.ACTIVE_VALUE); MessageProto.Message message = builder.build(); operations.add(message); } System.out.println((System.nanoTime() - begin)/1000000); } //String json @Test public void add3(){ Gson gson = new Gson(); BoundSetOperations<String, String> operations = jsonRedisTemplate.boundSetOps("message3"); long begin = System.nanoTime(); for(int i = 0;i<100 ;i++){ MessageProto.Message.Builder builder = MessageProto.Message.newBuilder(); builder.setId(i); builder.setLength(10); builder.setMsgBody("你好啊"); builder.setType(MessageProto.Message.Type.ACTIVE_VALUE); MessageProto.Message message = builder.build(); operations.add(gson.toJson(message)); } System.out.println((System.nanoTime() - begin)/1000000); } }
耗时:
jdk: 192毫秒
protoBuf: 93 毫秒
Json: 107 毫秒
单行数据大小
JDK:
protoBuf:
JSON:
JDK使用ProtoBuf 生成的 java 文件类生产,看起来有很多垃圾数据
这次用 普通JAVA 类,写一个差不多结构的再试一次
@Getter @Setter public static class Message implements Serializable { private static final long serialVersionUID = -7270500345236168152L; //消息类型 private Byte type; //消息长度 private Integer length; private Long id; //消息体 private String msgBody; public Message(Byte type, Integer length, Long id, String msgBody) { this.type = type; this.length = length; this.id = id; this.msgBody = msgBody; } @Getter public enum Type { ACTIVE((byte) 0x00, "激活"), MESSAGE((byte) 0x01, "消息"); //状态代码 private Byte code; //状态名称 private String name; //构造方法 Type(Byte code, String name) { this.code = code; this.name = name; } //根据code获取状态名称 public static String getNameByCode(Byte code) { for (Type item : Type.values()) if (item.getCode().equals(code)) return item.getName(); return ""; } } }
//JDK @Test public void add() { BoundSetOperations<String, MessageProtoTest.Message> operations = redisTemplate.boundSetOps("message4"); long begin = System.nanoTime(); for (int i = 0; i < 100; i++) operations.add(new MessageProtoTest.Message(Message.Type.ACTIVE.getCode(),10,(long)i,"你好啊")); System.out.println((System.nanoTime() - begin) / 1000000); }
结果: 耗时 137毫秒
单行数据大小:
看起来更糟糕
Message.proto
syntax = "proto3"; option java_package = "com.lzw.protobuf"; option java_outer_classname = "MessageProto"; message Message { int32 type = 1; int32 length = 2; int32 id = 3; string msgBody = 4; enum Type { ACTIVE = 0; MESSAGE = 1; } }