acecase 发表于 2022-11-10 09:52

protobuff和json

protobuff和json广泛应用应用于通信和存储上,2种方式使用那种各有各的特定。protobuff序列化比json快,体积也比json的序列化小 (json耗在属性名,属性越多明显),在性能和内容大小上有要求的选protobuff相对优,但protobuff要定义proto文件,有沟通成本,而且状态也有顺序,修改老版本的proto要遵循一定的规则,而json就比较通用修改也没有那么多规则。
简单的例子对比protobuff和json(fastjson)序列化速度和序列化后的体制大小:
proto文件:
syntax = "proto3";
package com.test1;
option java_package = "com.test1";
option java_multiple_files = true;

message UserInfo{
string userId   = 1;//userId
string url      = 2;//头像
string name   = 3;//名字
int32 sex       = 4;//玩家性别
}UserVO:
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
@AllArgsConstructor
public class UserVo {

    private String userId;

    private String url;

    private String name;

    private int sex;
}
Main:
import java.nio.charset.Charset;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;

import lombok.SneakyThrows;

public class ProtoBuffVsJson {

    @SneakyThrows
    public static void main(String[] args) {
      String uid = "testProtoBuffVsJson";
      String url = "https://www.baidu.com";
      String name = "testProtoBuffVsJson";
      int sex = 1;

      Charset charset = Charset.forName("UTF-8");
      SerializeConfig serializeConfig = SerializeConfig.getGlobalInstance();
      ParserConfig parserConfig = ParserConfig.getGlobalInstance();
      SerializerFeature[] serializerFeatures = new SerializerFeature[]{SerializerFeature.BrowserSecure};;
      SerializeFilter[] serializeFilters = new SerializeFilter;;
      Feature[] features = new Feature;

      UserVo uservo = UserVo.builder().userId(uid).url(url).name(name).sex(sex).build();
      UserInfo userInfo = UserInfo.newBuilder().setUserId(uid).setUrl(url).setName(name).setSex(sex).build();

      long voEStartTime = System.nanoTime();
      byte[] voBytes = JSON.toJSONBytes(charset, uservo, serializeConfig, serializeFilters, null,
                JSON.DEFAULT_GENERATE_FEATURE, serializerFeatures);
      long voEEndTime = System.nanoTime();

      long protoEStartTime = System.nanoTime();
      byte[] protoBytes = userInfo.toByteArray();
      long protoEEndTime = System.nanoTime();

      long voDStartTime = System.nanoTime();
      uservo = JSON.parseObject(voBytes, charset, UserVo.class, parserConfig, null,
                JSON.DEFAULT_PARSER_FEATURE, features);
      long voDEndTime = System.nanoTime();

      long protoDStartTime = System.nanoTime();
      userInfo = UserInfo.parseFrom(protoBytes);
      long protoDEndTime = System.nanoTime();

      System.out.println("json 序列化时间:" + (voEEndTime - voEStartTime)
                + "ns 反序列化时间:" + (voDEndTime - voDStartTime)
                + "ns byte数组长度:" + voBytes.length);
      System.out.println("proto 序列化时间:" + (protoEEndTime - protoEStartTime)
                + "ns 反序列化时间:" + (protoDEndTime - protoDStartTime)
                + "ns byte数组长度:" + protoBytes.length);

    }
}输出结果:

页: [1]
查看完整版本: protobuff和json