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. keepalived实现haproxy负载均衡器的高可用

    一.keepalived简介 keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于,用来防止单点故障. 二.vrrp协议2.1 vrrp协议简介 在现实的网络环境中,两台需要通信 ...

  2. nagios系列(七)nagios通过自定义脚本的方式监控mysql主从同步

    nagios监控mysql主从同步 起因:nagios可能监控到mysql服务的运行情况,但确不能监控mysql的主从复制是否正常:有时候,同步已经停止,但管理人员却不知道. 登陆mysql从服务器, ...

  3. 小技巧css解决移动端ios不兼容position:fixed属性,无需插件

    移动端开发仿app头部底部固定设置position:fixed,android2.2以上已经实现.但是在ios8以下系统,当小键盘激活时,都会出现位置浮动问题.如图: 如何解决: 查阅资料之后想到一下 ...

  4. TestNG配置注解

    以下是TestNG支持的注释列表: 注解 描述 @BeforeSuite 在该套件的所有测试都运行在注释的方法之前,仅运行一次. @AfterSuite 在该套件的所有测试都运行在注释方法之后,仅运行 ...

  5. java多线程快速入门(十六)

    ThreadLocal关键字实现每个线程有自己的变量 package com.cppdy; class Number { private int num; public static ThreadLo ...

  6. linux - awk 和kill 批量杀死进程

    ps -ef|grep check_os.sh | grep -v grep | awk '{print $2}' | xargs kill -9 $2表示第2列,即进程号PID; grep -v g ...

  7. bootstrap之表单和图片

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

  8. ThinkPHP3.1快速入门教程

    ThinkPHP3.1快速入门教程 http://www.thinkphp.cn/info/155.html   ------------------------------------------- ...

  9. Maven的下载,安装,配置,测试,初识以及Maven私服

    :Maven目录分析 bin:含有mvn运行的脚本 boot:含有plexus-classworlds类加载器框架 conf:含有settings.xml配置文件 lib:含有Maven运行时所需要的 ...

  10. k8s 使用

    转自:https://blog.csdn.net/zyc88888/article/details/79281954