1       介绍

Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data – think XML, but smaller, faster, and simpler.

2       Example:

2.1   定义proto文件

addrbook.proto

message Person {

required string name = 1;

required int32 id = 2;

optional string email = 3;

enum PhoneType {

MOBILE = 0;

HOME = 1;

WORK = 2;

}

message PhoneNumber {

required string number = 1;

optional PhoneType type = 2 [default = HOME];

}

repeated PhoneNumber phone = 4;

}

2.2 编译:

protoc.exe -I=./ --cpp_out=./ ./addrbook.proto

生成文件:addrbook.pb.cc addrbook.pb.h

2.3 使用

(依赖库:libprotoc.lib;libprotobuf.lib

2.3.1  序列化

Person person;

person.set_id(10);

person.set_name("protobuf");

person.set_email("test@gmail.com");

Person::PhoneNumber* phone_num1 = person.add_phone();

phone_num1->set_number("12345678");

phone_num1->set_type(Person_PhoneType::Person_PhoneType_MOBILE);

Person::PhoneNumber* phone_num2 = person.add_phone();

phone_num2->set_number("123456780");

std::string ouput;

size_t size = person.ByteSize();

person.SerializeToString(&ouput);

/* 使用string做需要的操作 */

2.3.2  反序列化

Person person;

person.ParseFromString(in);

std::string name = person.name();

int id = person.id();

std::string email = person.email();

int num = person.phone_size();

Person::PhoneNumber* phone =  new Person::PhoneNumber[num];

for (size_t i = 0; i < num; ++i)

{

phone[i] = person.phone(i);

std::string phone_num = phone[i].number();

int type = phone[i].type();

}

 

3       Why not just use XML?

Protocol buffers have many advantages over XML for serializing structured data. Protocol buffers:

  • are simpler
  • are 3 to 10 times smaller
  • are 20 to 100 times faster
  • are less ambiguous
  • generate data access classes that are easier to use programmatically

JsonXMLProtoBuf特点比较

Json

  • human readable/editable
  • can be parsed without knowing schema in advance
  • excellent browser support
  • less verbose than XML

XML

  • human readable/editable
  • can be parsed without knowing schema in advance
  • standard for SOAP etc
  • good tooling support (xsd, xslt, sax, dom, etc)
  • pretty verbose

Protobuf

  • very dense data (small output)
  • hard to robustly decode without knowing the schema (data format is internally ambiguous, and needs schema to clarify)
  • very fast processing
  • not intended for human eyes (dense binary)

All have good support on most platforms.

以上均为网上整理

另通过ICE和protobuf写了个简单的测试例子:http://pan.baidu.com/s/1i3oPfdN

protobuf 测试使用的更多相关文章

  1. linux protobuf 测试官方例子遇到报错及解决办法。

    测试例子时出现报错如下,在最下面会写出安装流程. -------------------------------------报错----1------------------------------- ...

  2. Netty5 + Protobuf 使用

    1. 安装开发环境 1.1 Netty环境 这里我使用Netty5.0.0版本 到这里下载即可http://netty.io/ 下载netty-all-5.0.0.Alpha2.jar 这个jar包简 ...

  3. 在centos 6.9下Protocol Buffers数据传输及存储协议的使用(python)

    我们知道Protocol Buffers是Google定义的一种跨语言.跨平台.可扩展的数据传输及存储的协议,因为将字段协议分别放在传输两端,传输数据中只包含数据本身,不需要包含字段说明,所以传输数据 ...

  4. Protobuf for Python测试保存和读取文件

    安装pip, setuptools, and wheel 如果已经从python.org,安装啦Python 2 >=2.7.9 or Python 3 >=3.4 ,那么就已经有啦pip ...

  5. protobuf 安装 及 小测试

    参考:http://shift-alt-ctrl.iteye.com/blog/2210885 版本: 2.5.0 百度云盘上有jar包. mac 上安装: 新建:/Users/zj/software ...

  6. protobuf简单测试应用

    protobuf是google推出的一种数据交换协议,比较适合应用于底层服务交互,nodejs提供protobufjs包的实现,下面是一个简单的测试demo: 首先是.proto文件: package ...

  7. 测试Websocket建立通信,使用protobuf格式交换数据

    接到一个应用测试,应用实现主要使用websocket保持长链接,使用protobuf格式交换数据,用途为发送消息,需要我们测试评估性能,初步评估需要测试长链接数.峰值消息数以及长期运行稳定性 整体需求 ...

  8. 如何用jmeter对websockt和protobuf进行压力测试

    参考代码:https://github.com/hutao722/kekexinxin 这是基于Jmeter WebsocketSampler的插件,支持对基于websocket和protobuf的服 ...

  9. 如何用jmeter对websock和protobuf进行压力测试

    1. 一个websocket插件官网地址 https://github.com/maciejzaleski/JMeter-WebSocketSampler 2. 可以用上述插件,也可以自己扩展,以实现 ...

随机推荐

  1. makefile 字符串处理函数

    截取自<跟我一起写Makefile> (1)  $(subst <from>, <to>, <text>) 名称: 字符串替换函数 subst 功能: ...

  2. 转载:详解Java 自动装箱与拆箱的实现原理

    原文:http://www.jb51.net/article/111847.htm 什么是自动装箱和拆箱 自动装箱就是Java自动将原始类型值转换成对应的对象,比如将int的变量转换成Integer对 ...

  3. vue及webpack在项目中的一些优化

    传送:https://www.haorooms.com/post/vue_webpack_youhua

  4. PHP: POST Content-Length of xxx bytes exceeds the limit of 8388608 bytes

    用户上传了 4 个附件,每个小于 5M,但是总大小超过了 15 M. 在 Nginx 日志中找到了如下错误信息,还没有到 Laravel 日志那一层. 2018/08/13 10:14:38 [err ...

  5. 记录片宇宙之the secret of the sun

  6. bootstrap——辅助类和响应式工具类

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. CentOS安装redis-audit 但执行时出错未解决 记录一下安装过程

    网上很多安装过程都太老了,测试很多方法终于成功了,但执行时还是出错,哪位熟悉的可以告知一下. yum install -y ruby rubygems ruby-devel git gcc gem s ...

  8. P3331 [ZJOI2011]礼物(GIFT)

    题解: 首先转化为平面问题 对于每一个z,f(x,y)的值为它能向上延伸的最大高度 ...莫名其妙想出来的是n^4 以每个点作为右下边界n^3枚举再o(n)枚举左下边界计算z的最大值 然而很显然这种做 ...

  9. 定制库到maven库

    有一些jar不支持maven,这个时候就可以使用下面的处理方式. kaptcha,它是一个流行的第三方Java库,它被用来生成 “验证码” 的图片,以阻止垃圾邮件,但它不在 Maven 的中央仓库中. ...

  10. python中的面相对象

    1.常用术语 2.创建类 empCount 变量是一个类变量,它的值将在这个类的所有实例之间共享.你可以在内部类或外部类使用 Employee.empCount 访问. 第一种方法__init__() ...