当前位置:网站首页>Serialization: comparison of protobuf and JSON!

Serialization: comparison of protobuf and JSON!

2020-12-07 19:43:31 Manong's breakthrough

Introduce

ProtoBuf yes google Tools developed by the team to efficiently store and read structured data . What is structured data , As the literal expression goes , It's data with a certain structure . For example, there are a lot of records in the phone book , Each record contains the name 、ID、 mail 、 Telephone, etc , This structure repeats itself .

similar

XML、JSON It can also be used to store such structured data , But use ProtoBuf The data represented can be more efficient , And compress the data smaller .

principle

ProtoBuf It's through ProtoBuf The compiler will be specific to the programming language .proto The suffix data structure file is compiled into various programming languages (Java,C/C++,Python) Special class file , And then through Google Support library for each programming language provided by lib You can call API.( About proto How to write a structure , You can refer to the documentation yourself )

ProtoBuf Compiler installation

Mac : brew install protobuf

for instance

1. So let's create one proto file message.proto

syntax = "proto3";

message Person {
    int32 id = 1;
    string name = 2;

    repeated Phone phone = 4;

    enum PhoneType {
        MOBILE = 0;
        HOME = 1;
        WORK = 2;
    }

    message Phone {
        string number = 1;
        PhoneType type = 2;
    }
}

2. Create a Java project And will proto File placement src/main/proto Under the folder

3. compile proto Document to Java edition Using the command line cd To src/main Under the table of contents

Terminal execution command : protoc --java_out=./java ./proto/*.proto

Will find , In your src/main/java It has already generated the corresponding in Java class

4. rely on Java Version of ProtoBuf support library Here's just one use Gradle Use dependent chestnuts

implementation 'com.google.protobuf:protobuf-java:3.9.1'

5. take Java Object to ProtoBuf data

Message.Person.Phone.Builder phoneBuilder = Message.Person.Phone.newBuilder();
Message.Person.Phone phone1 = phoneBuilder
        .setNumber("100860")
        .setType(Message.Person.PhoneType.HOME)
        .build();
Message.Person.Phone phone2 = phoneBuilder
        .setNumber("100100")
        .setType(Message.Person.PhoneType.MOBILE)
        .build();
Message.Person.Builder personBuilder = Message.Person.newBuilder();
personBuilder.setId(1994);
personBuilder.setName("XIAOLEI");
personBuilder.addPhone(phone1);
personBuilder.addPhone(phone2);

Message.Person person = personBuilder.build();
long old = System.currentTimeMillis();
byte[] buff = person.toByteArray();
System.out.println("ProtoBuf  Encoding takes time :" + (System.currentTimeMillis() - old));
System.out.println(Arrays.toString(buff));
System.out.println("ProtoBuf  Data length :" + buff.length);

6. take ProtoBuf data , Convert back to Java object

System.out.println("- Start decoding -");
old = System.currentTimeMillis();
Message.Person personOut = Message.Person.parseFrom(buff);
System.out.println("ProtoBuf  Decoding takes time :" + (System.currentTimeMillis() - old));
System.out.printf("Id:%d, Name:%s\n", personOut.getId(), personOut.getName());
List<Message.Person.Phone> phoneList = personOut.getPhoneList();
for (Message.Person.Phone phone : phoneList)
{
    System.out.printf(" cell-phone number :%s (%s)\n", phone.getNumber(), phone.getType());
}

Compare

In order to embody ProtoBuf The advantages of , I wrote about the same structure Java class , And will Java Object conversion to JSON data , Come and ProtoBuf Compare .JSON The compiler uses Google Provided GSON library ,JSON Part of the code will not be posted , Show the results directly

Comparison results

function 1 Time

 JSON  Start coding  】
JSON  code 1 Time , Time consuming :22ms
JSON  Data length :106
- Start decoding -
JSON  decode 1 Time , Time consuming :1ms

 ProtoBuf  Start coding  】
ProtoBuf  code 1 Time , Time consuming :32ms
ProtoBuf  Data length :34
- Start decoding -
ProtoBuf  decode 1 Time , Time consuming :3ms

function 10 Time

 JSON  Start coding  】
JSON  code 10 Time , Time consuming :22ms
JSON  Data length :106
- Start decoding -
JSON  decode 10 Time , Time consuming :4ms

 ProtoBuf  Start coding  】
ProtoBuf  code 10 Time , Time consuming :29ms
ProtoBuf  Data length :34
- Start decoding -
ProtoBuf  decode 10 Time , Time consuming :3ms

function 100 Time

 JSON  Start coding  】
JSON  code 100 Time , Time consuming :32ms
JSON  Data length :106
- Start decoding -
JSON  decode 100 Time , Time consuming :8ms

 ProtoBuf  Start coding  】
ProtoBuf  code 100 Time , Time consuming :31ms
ProtoBuf  Data length :34
- Start decoding -
ProtoBuf  decode 100 Time , Time consuming :4ms

function 1000 Time

 JSON  Start coding  】
JSON  code 1000 Time , Time consuming :39ms
JSON  Data length :106
- Start decoding -
JSON  decode 1000 Time , Time consuming :21ms

 ProtoBuf  Start coding  】
ProtoBuf  code 1000 Time , Time consuming :37ms
ProtoBuf  Data length :34
- Start decoding -
ProtoBuf  decode 1000 Time , Time consuming :8ms

function 1 ten thousand Time

 JSON  Start coding  】
JSON  code 10000 Time , Time consuming :126ms
JSON  Data length :106
- Start decoding -
JSON  decode 10000 Time , Time consuming :93ms

 ProtoBuf  Start coding  】
ProtoBuf  code 10000 Time , Time consuming :49ms
ProtoBuf  Data length :34
- Start decoding -
ProtoBuf  decode 10000 Time , Time consuming :23ms

function 10 ten thousand Time

 JSON  Start coding  】
JSON  code 100000 Time , Time consuming :248ms
JSON  Data length :106
- Start decoding -
JSON  decode 100000 Time , Time consuming :180ms

 ProtoBuf  Start coding  】
ProtoBuf  code 100000 Time , Time consuming :51ms
ProtoBuf  Data length :34
- Start decoding -
ProtoBuf  decode 100000 Time , Time consuming :58ms

summary

Encoding and decoding performance These chestnuts are just simple samples , In fact, according to my experiments

  • The times are 1 Less than a thousand ,ProtoBuf The encoding and decoding performance of , Both with JSON Be roughly the same , Even more than JSON Bad trends .
  • The times are 2 More than a thousand ,ProtoBuf The encoding and decoding performance of , All ratio JSON A much higher .
  • The times are 10 All the above ,ProtoBuf The encoding and decoding performance is obvious , Far higher than JSON Performance of .

Memory footprint ProtoBuf Of memory 34, and JSON arrive 106 ,ProtoBuf The memory usage of is only JSON Of 1/3.

ending

In fact, this experiment has a lot to optimize , Even if it's a rough test , You can see ProtoBuf The advantages of .

compatible

New fields

  • stay proto Add... To the file nickname Field
  • Generate Java file
  • With the old proto Byte array data , Convert to object
Id:1994, Name:XIAOLEI
 cell-phone number :100860 (HOME)
 cell-phone number :100100 (MOBILE)
getNickname=

result , It can be converted successfully .

Delete field

  • stay proto Delete... From file name Field
  • Generate Java file
  • With the old proto Byte array data , Convert to object
Id:1994, Name:null
 cell-phone number :100860 (HOME)
 cell-phone number :100100 (MOBILE)

result , It can be converted successfully .

source :my.oschina.net/xiaolei123/blog/3085607

版权声明
本文为[Manong's breakthrough]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201207194233396u.html