轉載自: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之間的對比
一條消息數據,protobuf序列化后的大小是json的10分之一,xml格式的20分之一,是二進制序列化的10分之一,總體看來ProtoBuf的優勢還是很明顯的。
protobuf是google提供的一個開源序列化框架,類似于XML,JSON這樣的數據表示語言。
protobuf在google中是一個比較核心的基礎庫,作為分布式運算涉及到大量的不同業務消息的傳遞,如何高效簡潔的表示、操作這些業務消息在google這樣的大規模應用中是至關重要的。而protobuf這樣的庫正好是在效率、數據大小、易用性之間取得了很好的平衡。
protobuf簡單總結如下幾點:
1.靈活(方便接口更新)、高效(效率經過google的優化,傳輸效率比普通的XML等高很多);
2.易于使用;開發人員通過按照一定的語法定義結構化的消息格式,然后送給命令行工具,工具將自動生成相關的類,可以支持java、c++、python等語言環境。通過將這些類包含在項目中,可以很輕松的調用相關方法來完成業務消息的序列化與反序列化工作。
3.語言支持;原生支持c++,java,python
個人總結的適用protobuf的場合:
1.需要和其它系統做消息交換的,對消息大小很敏感的。那么protobuf適合了,它語言無關,消息空間相對xml和json等節省很多。
2.小數據的場合。如果你是大數據,用它并不適合。
3.項目語言是c++,java,python的,因為它們可以使用google的源生類庫,序列化和反序列化的效率非常高。其它的語言需要第三方或者自己寫,序列化和反序列化的效率不保證。
4.總體而言,protobuf還是非常好用的,被很多開源系統用于數據通信的工具,在google也是核心的基礎庫。
此外,還有更牛叉的facebook的thrift,2007年由Facebook開發,之后在2008年加到Apache計劃中。是一個跨語言的輕量級RPC消息和數據交換框架,Thrift能生成的語言有: C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk, and OCaml,這是它的一大優點。
Thrift 和 Protobuf 應用場景
在查看了一系列的資料后,覺得要從性能上的區別來在Thrift和Protobuf之間做選擇意義不大,因為他們的性能太接近了。我們應該從項目支持,文檔,易用性,特性方面來進行選擇。如在《Thrift vs. Protocol Buffers》一文說提到的。
Thrift和Protobuf的最大不同,在于Thrift提供了完整的RPC支持,包含了Server/Client,而Protobuf只包括了stub的生成器和格式定義。
下面列出來的是一些數據交換格式的Performance測試數據,另外有兩份針對Thrift和Protobuf的測試數據:
thrift-protobuf-compare :包含了多種數據交換格式的benchmarking;
Thrift and Protocol Buffers performance in Java
Thrift and Protocol Buffers performance in Java Round 2 :作者在對Protobuf進行了一些參數調優后重做的測試,測試結果Protobuf從帶寬節省和序列化/解序列化速度上都占了優勢。
數據類型
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 |
綜合對比
| protobuf | thrift |
功能特性 | 主要是一種序列化機制 | 提供了全套RPC解決方案,包括序列化機制、傳輸層、并發處理框架等 |
支持語言 | C++/Java/Python | C++, Java, Python, Ruby, Perl, PHP, C#, Erlang, Haskell |
易用性 | 語法類似,使用方式等類似 |
生成代碼的質量 | 可讀性都還過得去,執行效率另測 |
升級時版本兼容性 | 均支持向后兼容和向前兼容 |
學習成本 | 功能單一,容易學習 | 功能豐富、學習成本高 |
文檔&社區 | 官方文檔較為豐富,google搜索protocol buffer有2000W+結果,google group被墻不能訪問 | 官方文檔較少,沒有API文檔,google搜索apache thrift僅40W結果,郵件列表不怎么活躍 |
性能對比
由于thrift功能較protobuf豐富,因此單從序列化機制上進行性能比較,按照序列化后字節數、序列化時間、反序列化時間三個指標進行,對thrift的二進制、壓縮、protobuf三種格式進行對比。
測試方法:
取了15000+條樣本數據,分別寫了三個指標的測試程序,在自己的電腦上執行,其中時間測試循環1000次,總的序列化/反序列化次數1500W+。
平均字節數
thrift二進制 | 535 |
thrift壓縮 | 473 |
protobuf | 477 |
序列化(1500W次)時間(ms):
thrift二進制 | 306034 |
thrift壓縮 | 304256 |
protobuf | 177652 |
反序列化(1500W次)時間(ms):
thrift二進制 | 287972 |
thrift壓縮 | 315991 |
protobuf | 157192 |
thrift的時間測試可能不是很準,由于thrift產生代碼的復雜性,編寫的測試代碼為了適應其接口,在調用堆棧上可能有一些額外開銷。
參考推薦:
Apache Avro 與 Thrift 比較(阿里數據平臺)
Apache Thrift – 可伸縮的跨語言服務開發框架(IBM)
Google 的開源技術protobuf 簡介與例子
幾種序列化協議(protobuf,xstream,jackjson,jdk,hessian)相關數據對比