轉(zhuǎn)載自:http://blog.mimvp.com/2015/03/comparing-protobuf-and-thrift/Protobuf 和 Thrift 簡介
google protobuf
Protocol buffers are Google’s language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages – Java, C++, or Python.
github:google/protobuf
google developers: protocol-buffers
Apache Thrift
The Apache Thrift software framework, for scalable cross-language services development, combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other languages.
github:apache/thrift
protobuf,json,xml,binary,Thrift之間的對(duì)比
一條消息數(shù)據(jù),protobuf序列化后的大小是json的10分之一,xml格式的20分之一,是二進(jìn)制序列化的10分之一,總體看來ProtoBuf的優(yōu)勢(shì)還是很明顯的。
protobuf是google提供的一個(gè)開源序列化框架,類似于XML,JSON這樣的數(shù)據(jù)表示語言。
protobuf在google中是一個(gè)比較核心的基礎(chǔ)庫,作為分布式運(yùn)算涉及到大量的不同業(yè)務(wù)消息的傳遞,如何高效簡潔的表示、操作這些業(yè)務(wù)消息在google這樣的大規(guī)模應(yīng)用中是至關(guān)重要的。而protobuf這樣的庫正好是在效率、數(shù)據(jù)大小、易用性之間取得了很好的平衡。
protobuf簡單總結(jié)如下幾點(diǎn):
1.靈活(方便接口更新)、高效(效率經(jīng)過google的優(yōu)化,傳輸效率比普通的XML等高很多);
2.易于使用;開發(fā)人員通過按照一定的語法定義結(jié)構(gòu)化的消息格式,然后送給命令行工具,工具將自動(dòng)生成相關(guān)的類,可以支持java、c++、python等語言環(huán)境。通過將這些類包含在項(xiàng)目中,可以很輕松的調(diào)用相關(guān)方法來完成業(yè)務(wù)消息的序列化與反序列化工作。
3.語言支持;原生支持c++,java,python
個(gè)人總結(jié)的適用protobuf的場(chǎng)合:
1.需要和其它系統(tǒng)做消息交換的,對(duì)消息大小很敏感的。那么protobuf適合了,它語言無關(guān),消息空間相對(duì)xml和json等節(jié)省很多。
2.小數(shù)據(jù)的場(chǎng)合。如果你是大數(shù)據(jù),用它并不適合。
3.項(xiàng)目語言是c++,java,python的,因?yàn)樗鼈兛梢允褂胓oogle的源生類庫,序列化和反序列化的效率非常高。其它的語言需要第三方或者自己寫,序列化和反序列化的效率不保證。
4.總體而言,protobuf還是非常好用的,被很多開源系統(tǒng)用于數(shù)據(jù)通信的工具,在google也是核心的基礎(chǔ)庫。
此外,還有更牛叉的facebook的thrift,2007年由Facebook開發(fā),之后在2008年加到Apache計(jì)劃中。是一個(gè)跨語言的輕量級(jí)RPC消息和數(shù)據(jù)交換框架,Thrift能生成的語言有: C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk, and OCaml,這是它的一大優(yōu)點(diǎn)。
Thrift 和 Protobuf 應(yīng)用場(chǎng)景
在查看了一系列的資料后,覺得要從性能上的區(qū)別來在Thrift和Protobuf之間做選擇意義不大,因?yàn)樗麄兊男阅芴咏恕N覀儜?yīng)該從項(xiàng)目支持,文檔,易用性,特性方面來進(jìn)行選擇。如在《Thrift vs. Protocol Buffers》一文說提到的。
Thrift和Protobuf的最大不同,在于Thrift提供了完整的RPC支持,包含了Server/Client,而Protobuf只包括了stub的生成器和格式定義。
下面列出來的是一些數(shù)據(jù)交換格式的Performance測(cè)試數(shù)據(jù),另外有兩份針對(duì)Thrift和Protobuf的測(cè)試數(shù)據(jù):
thrift-protobuf-compare :包含了多種數(shù)據(jù)交換格式的benchmarking;
Thrift and Protocol Buffers performance in Java
Thrift and Protocol Buffers performance in Java Round 2 :作者在對(duì)Protobuf進(jìn)行了一些參數(shù)調(diào)優(yōu)后重做的測(cè)試,測(cè)試結(jié)果Protobuf從帶寬節(jié)省和序列化/解序列化速度上都占了優(yōu)勢(shì)。
數(shù)據(jù)類型
protobuf | thrift | protobuf | thrift | protobuf | thrift | protobuf | thrift |
---|
double | double | float | | | byte | | i16 |
int32 | i32 | int64 | i64 | uint32 | | uint64 | |
sint32 | | sint64 | | fixed32 | | fixed64 | |
sfixed32 | | sfixed64 | | bool | bool | string | string |
bytes | binary | message | struct | enum | enum | service | service |
綜合對(duì)比
| protobuf | thrift |
功能特性 | 主要是一種序列化機(jī)制 | 提供了全套R(shí)PC解決方案,包括序列化機(jī)制、傳輸層、并發(fā)處理框架等 |
支持語言 | C++/Java/Python | C++, Java, Python, Ruby, Perl, PHP, C#, Erlang, Haskell |
易用性 | 語法類似,使用方式等類似 |
生成代碼的質(zhì)量 | 可讀性都還過得去,執(zhí)行效率另測(cè) |
升級(jí)時(shí)版本兼容性 | 均支持向后兼容和向前兼容 |
學(xué)習(xí)成本 | 功能單一,容易學(xué)習(xí) | 功能豐富、學(xué)習(xí)成本高 |
文檔&社區(qū) | 官方文檔較為豐富,google搜索protocol buffer有2000W+結(jié)果,google group被墻不能訪問 | 官方文檔較少,沒有API文檔,google搜索apache thrift僅40W結(jié)果,郵件列表不怎么活躍 |
性能對(duì)比
由于thrift功能較protobuf豐富,因此單從序列化機(jī)制上進(jìn)行性能比較,按照序列化后字節(jié)數(shù)、序列化時(shí)間、反序列化時(shí)間三個(gè)指標(biāo)進(jìn)行,對(duì)thrift的二進(jìn)制、壓縮、protobuf三種格式進(jìn)行對(duì)比。
測(cè)試方法:
取了15000+條樣本數(shù)據(jù),分別寫了三個(gè)指標(biāo)的測(cè)試程序,在自己的電腦上執(zhí)行,其中時(shí)間測(cè)試循環(huán)1000次,總的序列化/反序列化次數(shù)1500W+。
平均字節(jié)數(shù)
thrift二進(jìn)制 | 535 |
thrift壓縮 | 473 |
protobuf | 477 |
序列化(1500W次)時(shí)間(ms):
thrift二進(jìn)制 | 306034 |
thrift壓縮 | 304256 |
protobuf | 177652 |
反序列化(1500W次)時(shí)間(ms):
thrift二進(jìn)制 | 287972 |
thrift壓縮 | 315991 |
protobuf | 157192 |
thrift的時(shí)間測(cè)試可能不是很準(zhǔn),由于thrift產(chǎn)生代碼的復(fù)雜性,編寫的測(cè)試代碼為了適應(yīng)其接口,在調(diào)用堆棧上可能有一些額外開銷。
參考推薦:
Apache Avro 與 Thrift 比較(阿里數(shù)據(jù)平臺(tái))
Apache Thrift – 可伸縮的跨語言服務(wù)開發(fā)框架(IBM)
Google 的開源技術(shù)protobuf 簡介與例子
幾種序列化協(xié)議(protobuf,xstream,jackjson,jdk,hessian)相關(guān)數(shù)據(jù)對(duì)比