Go使用Protocol?Buffers在數(shù)據(jù)序列化的優(yōu)勢(shì)示例詳解
介紹
這一次,我們將深入探討 Protocol Buffers(protobuf)及其在數(shù)據(jù)序列化中的超能力所在。
Protocol Buffers,也被稱為 protobuf,是由谷歌開(kāi)發(fā)的一種語(yǔ)言無(wú)關(guān)的二進(jìn)制序列化格式。其主要目的是為了高效地序列化結(jié)構(gòu)化數(shù)據(jù),用于系統(tǒng)間通信和數(shù)據(jù)存儲(chǔ)。

Protocol Buffers 的主要優(yōu)勢(shì):
- 1. 緊湊性:Protobuf 提供高效的序列化,生成較小的消息大小,提升帶寬利用效率。
- 2. 模式演進(jìn):Protobuf 支持模式演進(jìn)而不破壞兼容性,允許對(duì)數(shù)據(jù)結(jié)構(gòu)進(jìn)行無(wú)縫更新。
- 3. 高效的序列化和反序列化:Protobuf 提供快速高效的序列化,提升整體系統(tǒng)性能。
- 4. 跨平臺(tái)支持:Protobuf 允許不同平臺(tái)和語(yǔ)言之間無(wú)縫交換數(shù)據(jù)。
這些優(yōu)勢(shì)使得 Protobuf 成為在 Go 應(yīng)用程序中進(jìn)行高效數(shù)據(jù)通信和存儲(chǔ)的強(qiáng)大工具。
它比 JSON 和 XML 更好的地方:

XML,即可擴(kuò)展標(biāo)記語(yǔ)言,就像一張地圖,用標(biāo)簽幫助組織和結(jié)構(gòu)化數(shù)據(jù)。它以一種人類和機(jī)器都能理解的方式呈現(xiàn)信息。然而,XML 可能冗長(zhǎng)并占用更多空間,這可能降低性能,使數(shù)據(jù)傳輸效率降低。
JSON,即 JavaScript 對(duì)象表示法,就像一個(gè)信使,使用簡(jiǎn)單的鍵值結(jié)構(gòu)來(lái)表示數(shù)據(jù)對(duì)象。它因易于閱讀和使用而在 Web 服務(wù)之間傳輸數(shù)據(jù)時(shí)變得流行。但 JSON 的基于文本的格式可能導(dǎo)致更大的文件大小,從而影響數(shù)據(jù)傳輸速度。
相比之下,Protocol Buffers(protobuf)在數(shù)據(jù)序列化領(lǐng)域脫穎而出。它就像一個(gè)魔術(shù),將數(shù)據(jù)轉(zhuǎn)換為緊湊高效的二進(jìn)制格式。Protobuf 以快速的數(shù)據(jù)處理和適應(yīng)變化的數(shù)據(jù)結(jié)構(gòu)而聞名,并且在不破壞兼容性的情況下進(jìn)行操作。它可以與不同的編程語(yǔ)言一起使用,并確保數(shù)據(jù)的可靠性。
總之,XML 和 JSON 各有用途,但如果您需要強(qiáng)大且高效的數(shù)據(jù)序列化解決方案,Protocol Buffer(protobuf)是首選。它提供緊湊性、速度、靈活性和兼容性,使其成為高效處理數(shù)據(jù)的首選方案。
在 Golang 中的序列化性能:Protocol Buffers vs. JSON
言歸正傳,讓我們動(dòng)手實(shí)踐。
- 1. 訪問(wèn)官方 Protocol Buffers GitHub 倉(cāng)庫(kù)(https://github.com/protocolbuffers/protobuf)下載與您操作系統(tǒng)兼容的編譯器。
- 2. 使用 .proto 文件格式定義一個(gè) Protocol Buffers 消息模式。
syntax = "proto3";
package main;
option go_package = "/;msgmodel";
message MyMessage {
int32 id = 1;
string name = 2;
string email = 3;
}編譯文件
protoc — go_out=. ./*proto
這個(gè)命令從 protobuf 模式生成 Go 代碼綁定。--go_out 標(biāo)志指定輸出應(yīng)為 Go 語(yǔ)言。這將生成一個(gè) msg.pb.go 文件,其中包含您的 protobuf 模式所需的代碼綁定。
在 Golang 中實(shí)現(xiàn)一個(gè)基準(zhǔn)測(cè)試,使用 protobuf 和 JSON 對(duì)大型數(shù)據(jù)集進(jìn)行序列化。
package main
import (
"encoding/json"
"github.com/golang/protobuf/proto"
"go-protobuf/model/message"
"log"
"testing"
)
const (
iteration = 10000000 //Number of iterations for the benchmark test
)
func generateDataset() []*message.MyMessage {
var dataset []*message.MyMessage
for i := 0; i < iteration; i++ {
data := &message.MyMessage{
Email: "johndoe@example.com",
Name: "John Doe",
Id: int32(i),
}
dataset = append(dataset, data)
}
return dataset
}
func BenchmarkProtobufSerialisation(b *testing.B) {
dataset := generateDataset()
b.ResetTimer()
for n := 0; n < b.N; n++ {
for _, data := range dataset {
_, err := proto.Marshal(data)
if err != nil {
log.Fatal(err)
}
}
}
}
func BenchmarkJSONSerialization(b *testing.B) {
dataset := generateDataset()
b.ResetTimer()
for n := 0; n < b.N; n++ {
for _, data := range dataset {
_, err := json.Marshal(data)
if err != nil {
log.Fatal(err)
}
}
}
}
func main() {
// Run the benchmark tests
testing.Benchmark(BenchmarkProtobufSerialisation)
testing.Benchmark(BenchmarkJSONSerialization)
}根據(jù)基準(zhǔn)測(cè)試結(jié)果(如下所示),很明顯,就速度而言,Protocol Buffers(Protobuf)的序列化性能優(yōu)于 JSON。與 JSON 的序列化基準(zhǔn)測(cè)試相比,Protobuf 的序列化基準(zhǔn)測(cè)試完成時(shí)間明顯較短。

內(nèi)存性能比較:JSON vs. Protocol Buffers
在 Golang 中實(shí)現(xiàn)一個(gè)基準(zhǔn)測(cè)試,比較使用 Protocol Buffers 和 JSON 處理大型數(shù)據(jù)集時(shí)的內(nèi)存使用情況。
package main
import (
"encoding/json"
"github.com/golang/protobuf/proto"
"go-protobuf/model/message"
"log"
"runtime"
"runtime/debug"
"testing"
)
const (
iteration = 100000000 //Number of iterations for the benchmark test
)
func generateDataset() []*message.MyMessage {
var dataset []*message.MyMessage
for i := 0; i < iteration; i++ {
data := &message.MyMessage{
Email: "johndoe@example.com",
Name: "John Doe",
Id: int32(i),
}
dataset = append(dataset, data)
}
return dataset
}
func BenchmarkProtobufSerialisation(b *testing.B) {
dataset := generateDataset()
b.ResetTimer()
for n := 0; n < b.N; n++ {
for _, data := range dataset {
_, err := proto.Marshal(data)
if err != nil {
log.Fatal(err)
}
}
}
measureMemoryUsage(b)
}
func BenchmarkJSONSerialization(b *testing.B) {
dataset := generateDataset()
b.ResetTimer()
for n := 0; n < b.N; n++ {
for _, data := range dataset {
_, err := json.Marshal(data)
if err != nil {
log.Fatal(err)
}
}
}
measureMemoryUsage(b)
}
func measureMemoryUsage(b *testing.B) {
debug.FreeOSMemory()
var mem runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&mem)
b.ReportMetric(float64(mem.Alloc)/1024/1024, "Memory_MB")
}
func main() {
// Run the benchmark tests
testing.Benchmark(BenchmarkProtobufSerialisation)
testing.Benchmark(BenchmarkJSONSerialization)
}盡管差異很小,但基準(zhǔn)測(cè)試結(jié)果表明,與 Protobuf 序列化相比,JSON 序列化使用了更多的內(nèi)存。平均而言,JSON 序列化消耗了約 0.2052 MB 的內(nèi)存,而 Protobuf 序列化僅使用了約 0.2042 MB。盡管差異很小,但很明顯 Protobuf 在內(nèi)存使用方面更加高效。這意味著 Protobuf 的緊湊二進(jìn)制格式有助于節(jié)省內(nèi)存,使其成為處理大型數(shù)據(jù)集和提高性能的良好選擇。

結(jié)論
與在 Golang 中的 JSON 序列化相比,Protocol Buffers(protobuf)展現(xiàn)出了更優(yōu)越的性能和內(nèi)存效率。借助其緊湊的二進(jìn)制格式和高效的序列化機(jī)制,protobuf 提供了更小的消息大小、提升了網(wǎng)絡(luò)效率,并減少了帶寬使用。此外,其模式演進(jìn)能力允許對(duì)數(shù)據(jù)模型進(jìn)行無(wú)縫更新。雖然 JSON 有其優(yōu)勢(shì),但在需要高速和高內(nèi)存效率的數(shù)據(jù)序列化場(chǎng)景中,protobuf 出類拔萃,實(shí)現(xiàn)了優(yōu)化的數(shù)據(jù)傳輸和改善的系統(tǒng)性能。
以上就是Go使用Protocol Buffers在數(shù)據(jù)序列化的優(yōu)勢(shì)示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Go Protocol Buffers數(shù)據(jù)序列化的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Golang import本地包和導(dǎo)入問(wèn)題相關(guān)詳解
這篇文章主要介紹了Golang import本地包和導(dǎo)入問(wèn)題相關(guān)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Go語(yǔ)言實(shí)現(xiàn)一個(gè)簡(jiǎn)單的并發(fā)聊天室的項(xiàng)目實(shí)戰(zhàn)
本文主要介紹了Go語(yǔ)言實(shí)現(xiàn)一個(gè)簡(jiǎn)單的并發(fā)聊天室的項(xiàng)目實(shí)戰(zhàn),文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Golang協(xié)程常見(jiàn)面試題小結(jié)
本文主要介紹了Golang協(xié)程常見(jiàn)面試題小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
構(gòu)建Golang應(yīng)用最小Docker鏡像的實(shí)現(xiàn)
這篇文章主要介紹了構(gòu)建Golang應(yīng)用最小Docker鏡像的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

