Introduction
ProtoBuf is google team development tool for efficiently storing and reading structured data. What is it structured data, as the expression literally, that is, with certain data structures. For example, there are many phone book data records, each record contains a name, ID, e-mail, phone, etc., this arrangement is repeated.
Similar
XML, JSON can also be used to store such structured data, but using data ProtoBuf representation can be more efficient, smaller and data compression.
Principle
ProtoBuf by ProtoBuf compiler-independent programming language-specific .proto suffix data structure documents translated into various programming languages (Java, C / C ++, Python) special class files, then by Google's various programming languages support library lib can call API. (On how to write proto structure, can be found on their own documentation)
protobuf compiler install
Mac: brew install protobuf
example
1. create a proto file
message.proto
syntax = "proto3";
the Message the Person {
Int32 the above mentioned id = 1;
String 2 = name;
REPEATED Phone = Phone. 4;
enum {PhoneType
MOBILE = 0;
the HOME =. 1;
WORK = 2;
}
Phone {Message
String Number =. 1;
PhoneType type = 2;
}
}
2. Create a Java project
and placed src / main proto file / folder proto
3. proto compiled file to the Java version of
the command line cd to src / main directory
terminal execute the command:. protoc --java_out = / java ./proto/*.proto
will find that has been generated in the corresponding Java classes in your src / main / java Lane
4. depend on Java version of ProtoBuf support library
here to cite a use-dependent chestnuts with Gradle
Implementation 'com.google.protobuf: Protobuf Java-: 3.9.1'
5. the Java objects into 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 =http://www.senta7.net/content/?729.html System.currentTimeMillis();
byte[] buff = person.toByteArray();
System.out.println(www.jujinyule.com"ProtoBuf 编码耗时:" + (System.currentTimeMillis() - old));
System.out.println(Arrays.toString(buff));
System.out.println ( "ProtoBuf Data length:" + buff.length);
6. The protobuf the data is converted back to Java objects
System.out.println ( "- start decoding -");
Old = System.currentTimeMillis ();
personOut = Message.Person.parseFrom Message.Person (BUFF);
System.out.println ( "protobuf decoding Processed:" + (System.currentTimeMillis () - Old));
System.out.printf ( "Id:% D , the Name:% S \ n-", personOut.getId (), personOut.getName ());
List <Message.Person.Phone> personOut.getPhoneList PhoneList = ();
for (Message.Person.Phone Phone: PhoneList)
{
System.out.printf ( "phone number:% S (% S www.xcdeyiju.com) \ n-", phone.getNumber (), phone.getType ());
}
comparison
In order to reflect the advantage ProtoBuf, I wrote a Java class the same structure, and convert Java objects to JSON data to be compared with ProtoBuf. JSON library compiled using GSON library provided by Google, part of the code of JSON is not posted, and the results show a direct
result of the comparison results
run once
[JSON] start coding
JSON-encoded once, time-consuming: 22ms
JSON data Length: 106
- Start decoding -
JSON decoder 1, Processed: 1ms
[] protobuf start encoding
protobuf encoding 1, Processed: 32ms
protobuf data length: 34
- start decoding -
protobuf decoder 1, Processed: 3ms
10 runs
[start coding JSON ]
JSON encoder 10, Processed: 22ms
JSON data length: 106
- start decoding -
JSON decoder 10, time-consuming: 4ms
[] protobuf start encoding
protobuf encoder 10, Processed: 29 ms
protobuf data length: 34
- start decoding -
protobuf decoding 10 times, time-consuming: 3ms
run 100 times
[coding] JSON start
100 JSON-encoded, time-consuming: 32ms
JSON Data Length: 106
- start decoding -
JSON decoder 100, Processed: 8ms
[] protobuf start encoding
protobuf encoder 100, Processed: 31ms
protobuf data length: 34
- start decoding -
ProtoBuf decoder 100, Processed: 4ms
run 1000
[JSON] start encoding
JSON encoder 1000, Processed: 39ms
JSON data length: 106
- start decoding -
JSON decoder 1000, Processed: 21ms
[] ProtoBuf start coding
ProtoBuf coding 1000, Processed: 37ms
protobuf data length: 34
- start decoding -
protobuf decoder 1000, Processed: 8ms
run 10,000
[] JSON start encoding
JSON encoding 10000, Processed: 126ms
JSON data length: 106
- start decoding -
JSON decoding 10,000 times, time-consuming: 93ms
[ProtoBuf start coding]
ProtoBuf coded 10,000 times, time-consuming: 49ms
Protobuf data length: 34
- Start decoding -
protobuf decoding 10,000, Processed: 23ms
Run 100,000
[] JSON start encoding
JSON encoded 100,000, Processed: 248ms
JSON Data Length: 106
- Start decoding -
JSON decoding 100,000 times, Processed: 180ms
[] protobuf start encoding
protobuf coding 100,000, Processed: 51ms
protobuf data length: 34
- start decoding -
protobuf decoding 100,000, Processed: 58ms
summarized
codec performance of
the above-described chestnut simply sampling, according to actually my experiments found that
the number of times in a thousand less, protoBuf encoding and decoding performance, are comparable with JSON, JSON trend even than the poor.
In more than 2,000 times, the encoding and decoding performance ProtoBuf, much higher than JSON.
The number of 10 million or more, encoding and decoding performance ProtoBuf's quite clear, far higher than the performance of JSON.
Memory footprint
ProtoBuf memory 34, and JSON reach 106, ProtoBuf memory footprint JSON only 1/3
the end of
fact, the experiments can be optimized there are many places, even in this rough test, can also be seen in ProtoBuf Advantage.
compatible
New field
added in the proto file nickname field
generated Java file
data in the old proto byte array, converted into objects
Id: 1994, Name: XIAOLEI
phone number: 100860 (HOME)
phone number: 100100 (MOBILE)
getNickname =
result, that can be converted successfully.
Delete field
Delete the file name in the proto field
generated Java file
data with the old proto byte array, convert the object
Id: 1994, Name: null
phone number: 100860 (HOME)
phone number: 100100 (MOBILE)
ProtoBuf of the trial compared with JSON
Guess you like
Origin www.cnblogs.com/qwangxiao/p/11329330.html
Recommended
Ranking