缔冠期刊网

The Principle and Application of Protocol Buffer

2022-06-09

The Principle and Application of Protocol Buffer

LU Zhong-yangGUO Zhen-bo

(College of Information Engineering,Qingdao University,Qingdao Shandong 266071,China)

【Abstract】This paper mainly introduces the principle of protobuf and considers its compression efficiency and the speed of data parsing. The simulation results reveal that, compared with the xml and json, protobuf has the samllest size of serialized file and the fastest speed of parsing. Therefore, we get a conclusion that protobuf is more suitable for some applications requiring high performance.

【Key words】Protobuf;Serialization;Parsing speed

1The introduction of protobuf

Protocol buffer(protobuf) is an efficient and quick serialization frame provided by Google[1]. Through the proto compiler, users can compile the. proto file to generate the corresponding class, and then call the corresponding methods in the class to serialize and deserialize the message. Java, c++ and python are three kinds of language provided by Google, which contains the compiler and library files of corresponding language. Protobuf is a serialization tool with a higer data exchange efficiency compared with xml and json[2] and the contents of serialized data transfer format is binary. Besides, protobuf can be used for data exchange between heterogeneous environment, distributed application of data communication and data storage and configuration files and other fields.

Protobuf is a kind of protocol maily used for object serialization and binary coding. Since the underlying communication protocol is general binary format, ptotobuf has the advantages of small occupied space in the communication network and high analytical speed and etc. The basic principle of protobuf in socket communicationis: protobuf packs the transfering data according to the defined format of message and the serilizes the message before sending data; then it deserilizes the packages at the receiving side.

2Design a protobuf file

Generally, writing a protobuf application requires the following steps:

(1)Defining message format and using proto as a suffix

The definition of the message format is very simple, and each field has a tag, modifier(repeated/required/optional) and field type (bool/bytes/string/int32/long)[3]. The value of the required field must be initialized, otherwise it would make mistakes that serialization and deserialization are compiled under the library of Debug mode. For optional field, it would be assigned a default value if not initialized. Repeated field can be repeated use, such as we can employ repeated field to define that a person with multiple mobile phones. The function of tag is to identify the positions of the fields in the binary stream. The format of proto file is defined as below:

message user {

required string name = 1;

required int32 age = 2;

};

(2)Generating code files with the protocol buffers compiler

Code files are automatically generated by the proto compiler[4], which describes the message format n a specific way of language. The defined proto file is chosen as the input file of the proto compiler. The result of compiling is the code file with a specific language. Protobuf has a high ability of cross-language. This paper use C++ program to serialize the user message to a file. And the other is a Java program, which is responsible for deserialization into a user object from a file. Figure 1 shows the relationship between the two programs and protobuf.

The proto file of User object is shown as follows, and we can get C++ files and Java files from protocol buffer compiler.

package msg;

option java_package = "com.lzy.msg.proto";

Fig. 1Protobuf cross-language communication

option java_outer_classname = "Userproto";

message User {

repeated string name = 1;

repeated int32 age = 2;

}

The compiling command is:

protoc-I=./--cpp_out=./c++--java_out=./java./Userproto.proto

In the command above, --cpp_out and --java_out are respectively the path of C++ and Java files, and ./Userproto.proto is the path specified by the proto file. The C++ file includes the Userproto.pb.cc and Userproto.pb.h. In Java file, the compiler creates a new class named UserProto and com.lzy.msg.proto is the package’s name. Meoreover, there is a public static class named User in Userproto class.

We use C++ program to serialize the data in the compiled file and declare an object user in class User, setting user.set_name(“Jack”) and user.set_age(18). Data after serialization is stored in a file named a. And then we read data from the file referred by parameter through Java program and deserialize the data into a User object.

User user = User.parseFrom(new FileInputStream(args[0]));

user.getName();

user.getAge();

The deserialized results are: Jack, 18. From the example above, we can see that protobuf can achieve a good cross-language communication.

3Performance comparison of protobuf, XML and JSON

Protobuf, XML and JSON can be used as serialization tools, and can also be used in network transmission. Both JSON and XML are a kind of description language[5]. Although they have better readability than protobuf, protobuf has an obvious advantage of high compression efficiency and high speed of data parsing. This paper compares the size of files after serialization and speed of data parsing among protobuf, XML and JSON.

This paper chooses protobuf, XML and JSON to serialize the same fields. And the simulation experiment environment is CPU dual core P8800, 4G memory, 500G hard disk and 7200 rpm. Figure 2 and 3 show the experiment results.

Fig. 2Comparison of file size after serialization

Fig. 3Comparison of parsing time

From figure 2 and figure 3, we can find that the file size serialized by protobuf is less than that by JSON and XML, and parsing time of protobuf is also more fast than the that of JSON and XML. Besides, table 1 compares these three serialization tools side by side.

Tab. 1Comparision of XML, JSON and protobuf

4Conclusion

This paper makes a simple introduction of the priciple of protocol buffer, which has a high ability of cross-language and cross-platform. Besides, we make a simulation experiment showing that protobuf can save time of data parsing and the size of serialized file is the smallest conpared with XML and JSON.

【Reference】

[1]Kalman M. ProtoML: A rule-based validation language for google protocol buffers[A]. 2013 8th International Conference for Internet Technology and Secured Transactions (ICITST)[C]. London: IEEE, 2013. 188-193.

[2]Kaur G, Fuad M M. An evaluation of protocol buffer[A]. Proceedings of the IEEE SoutheastCon 2010 (SoutheastCon)[C]. Concord, NC: IEEE, 2010. 459-462.

[3]Mu ller J, Lorenz M, Geller F, et al. Assessment of communication protocols in the EPC network -replacing textual SOAP and XML with binary google protocol buffers encoding[A]. 2010 IEEE 17th International Conference on Industrial Engineering and Engineering Management (IE&EM)[C]. Xiamen: IEEE, 2010. 404-409.

[4]Feng J H, Li J H. Google protocol buffers research and application in online game[A]. Proceedings of 2011 13th IEEE Joint International Computer science and Information Technology Conference[C]. Chongqing, China: IEEE, 2011.1-4.

[5]Cui C, Ni H. Optimization simulation of XML performance based on JSON [J]. Communications Technology, 2009, 42(8):108-114.

[责任编辑:刘展]

论文中心更多

期刊百科
期刊投稿 期刊知识 期刊审稿 核心期刊目录 录用通知 期刊版面费 投稿期刊推荐 学术问答
基础教育
小学语文 中学语文 小学数学 中学数学 小学英语 中学英语 物理教学 化学教学 生物教学 政治教学 历史教学 地理教学 科学教学 音乐教学 美术教学 体育教学 信息技术 班主任管理 校长管理 幼教 教育管理 微课教学 作文教学 德育教学 教学设计
医学论文
内科医学 外科医学 预防医学 妇科医学 检测医学 眼科医学 临床医学 药学论文 口腔医学 中西医 中医学 外科 护理 基础医学 急救医学 老年医学 医学实验 儿科医学 神经医学 兽医学 肿瘤医学 综合医学
职业教育
教育学原理 电影文学教育 学前教育 教育学管理 高等教育学 教育技术学 职业技术教育 成人教育学 特殊教育学 教育心理学 家庭教育 教育毕业 中专中职教育 教学设计 国学教育 学术研究 大学教育
药学卫生
社区门诊 医药学 医患关系 医院管理 疾病预防 保健医学 公共卫生 医学教育
文科论文
农业经济 工商管理毕业 会计毕业 行政管理 法律毕业 市场营销 经济毕业 汉语言文学 财务管理 物流管理 人力资源 旅游管理 国际贸易 物业管理 新闻学 企业管理 金融银行 社会科学 食品安全 办公档案 审计学 税务税收学 外国文学 哲学
理科论文
机电毕业 土木工程 计算机毕业 电气毕业 建筑毕业 电子商务 工程毕业 设计毕业 机械制造 汽车毕业 园林毕业 农学毕业 数控毕业 软件技术 水利工程 环境生态 畜牧渔业 化工毕业 科技创新 石油矿藏
论文格式
开题报告 论文题目 摘要关键词 目录提纲 论文致谢 参考文献 附录其他 论文答辩
职业论文
教育论文 经济论文 科技论文 财会论文 管理论文 医学论文 法学论文 文学论文 工业论文 建筑论文 农业论文 水利论文 计算机论文 社科论文 机械论文 生态环境 中西文化

先发表后付款 不成功可退款

权威机构认证 专注期刊10余年 1000余家杂志社长期合作

缔冠期刊网

首页 网站地图 返回顶部
Copyright © 1998- 缔冠期刊网