成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

在 Go 中使用 Protocol Buffers

開發 后端
今天我們將深入探討 Protocol Buffers(protobuf)及其在數據序列化中的超能力所在。

各位準備好了嗎!這一次,我們將深入探討 Protocol Buffers(protobuf)及其在數據序列化中的超能力所在。

介紹

Protocol Buffers,也被稱為 protobuf,是由谷歌開發的一種語言無關的二進制序列化格式。其主要目的是為了高效地序列化結構化數據,用于系統間通信和數據存儲。

Protocol Buffers 的主要優勢:

  • 緊湊性:Protobuf 提供高效的序列化,生成較小的消息大小,提升帶寬利用效率。
  • 模式演進:Protobuf 支持模式演進而不破壞兼容性,允許對數據結構進行無縫更新。
  • 高效的序列化和反序列化:Protobuf 提供快速高效的序列化,提升整體系統性能。
  • 跨平臺支持:Protobuf 允許不同平臺和語言之間無縫交換數據。

這些優勢使得 Protobuf 成為在 Go 應用程序中進行高效數據通信和存儲的強大工具。

它比 JSON 和 XML 更好的地方:

XML,即可擴展標記語言,就像一張地圖,用標簽幫助組織和結構化數據。它以一種人類和機器都能理解的方式呈現信息。然而,XML 可能冗長并占用更多空間,這可能降低性能,使數據傳輸效率降低。

JSON,即 JavaScript 對象表示法,就像一個信使,使用簡單的鍵值結構來表示數據對象。它因易于閱讀和使用而在 Web 服務之間傳輸數據時變得流行。但 JSON 的基于文本的格式可能導致更大的文件大小,從而影響數據傳輸速度。

相比之下,Protocol Buffers(protobuf)在數據序列化領域脫穎而出。它就像一個魔術,將數據轉換為緊湊高效的二進制格式。Protobuf 以快速的數據處理和適應變化的數據結構而聞名,并且在不破壞兼容性的情況下進行操作。它可以與不同的編程語言一起使用,并確保數據的可靠性。

總之,XML 和 JSON 各有用途,但如果您需要強大且高效的數據序列化解決方案,Protocol Buffer(protobuf)是首選。它提供緊湊性、速度、靈活性和兼容性,使其成為高效處理數據的首選方案。

在 Golang 中的序列化性能:Protocol Buffers vs. JSON

言歸正傳,讓我們動手實踐。

訪問官方 Protocol Buffers GitHub 倉庫(https://github.com/protocolbuffers/protobuf)下載與您操作系統兼容的編譯器。

使用 .proto 文件格式定義一個 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

這個命令從 protobuf 模式生成 Go 代碼綁定。--go_out 標志指定輸出應為 Go 語言。這將生成一個 msg.pb.go 文件,其中包含您的 protobuf 模式所需的代碼綁定。

在 Golang 中實現一個基準測試,使用 protobuf 和 JSON 對大型數據集進行序列化:

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)
}

根據基準測試結果(如下所示),很明顯,就速度而言,Protocol Buffers(Protobuf)的序列化性能優于 JSON。與 JSON 的序列化基準測試相比,Protobuf 的序列化基準測試完成時間明顯較短。

內存性能比較:JSON vs. Protocol Buffers

在 Golang 中實現一個基準測試,比較使用 Protocol Buffers 和 JSON 處理大型數據集時的內存使用情況:

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)

}

盡管差異很小,但基準測試結果表明,與 Protobuf 序列化相比,JSON 序列化使用了更多的內存。平均而言,JSON 序列化消耗了約 0.2052 MB 的內存,而 Protobuf 序列化僅使用了約 0.2042 MB。盡管差異很小,但很明顯 Protobuf 在內存使用方面更加高效。這意味著 Protobuf 的緊湊二進制格式有助于節省內存,使其成為處理大型數據集和提高性能的良好選擇。

結論

現在是總結的時候了!!!

與在 Golang 中的 JSON 序列化相比,Protocol Buffers(protobuf)展現出了更優越的性能和內存效率。借助其緊湊的二進制格式和高效的序列化機制,protobuf 提供了更小的消息大小、提升了網絡效率,并減少了帶寬使用。此外,其模式演進能力允許對數據模型進行無縫更新。雖然 JSON 有其優勢,但在需要高速和高內存效率的數據序列化場景中,protobuf 出類拔萃,實現了優化的數據傳輸和改善的系統性能。

責任編輯:趙寧寧 來源: 技術的游戲
相關推薦

2014-08-05 10:07:03

JSONRuby

2024-06-03 00:00:06

高性能數據傳輸應用程序

2023-10-28 16:22:21

Go接口

2021-11-29 22:59:34

Go Dockertest集成

2016-11-07 20:43:37

C程序員Protocol Bu

2022-01-03 08:06:15

函數Go數據

2024-08-19 01:10:00

RedisGo代碼

2022-11-03 20:38:01

CMD命令Go

2023-12-31 16:29:31

GoGoroutinesChannels

2013-12-13 17:21:14

Lua腳本語言

2009-06-25 16:49:24

Hibernate

2023-04-12 15:25:09

Bytrace鴻蒙

2023-11-17 12:04:39

GORM并發

2013-06-25 09:52:32

GoGo語言Go編程

2011-06-15 10:08:01

Qt CVS

2017-04-05 13:17:26

LinuxShellvi

2023-07-23 19:26:18

Linuxcat 命令

2010-04-29 17:51:40

Unix工具

2023-07-04 16:36:03

Linuxcd 命令

2024-02-04 00:00:00

Go貨幣接口
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美国产亚洲一区二区 | 精品国产鲁一鲁一区二区张丽 | 黄色在线观看国产 | 91原创视频 | www.av在线| 亚洲 精品 综合 精品 自拍 | 亚洲第一女人av | 久久久久久久电影 | 搞黄网站在线观看 | 欧美一级做a爰片免费视频 国产美女特级嫩嫩嫩bbb片 | 3p视频在线观看 | 欧美精品中文字幕久久二区 | 黄网站在线播放 | 国产91在线播放精品91 | 51ⅴ精品国产91久久久久久 | 自拍偷拍av| 日本理论片好看理论片 | 一区二区三区国产精品 | 亚洲国产成人精品久久久国产成人一区 | 亚洲三级国产 | 欧美一区二区在线播放 | 成人亚洲片 | 天天干天天干 | 超黄视频网站 | 日韩精品在线观看一区二区 | 一级高清视频 | 九色av | 欧美性生活网 | 伊人超碰在线 | 精品国产不卡一区二区三区 | 激情五月婷婷综合 | 午夜天堂精品久久久久 | 国产一区二区视频在线观看 | h视频在线观看免费 | 国产综合精品 | 成年网站在线观看 | 免费久久久久久 | 黄色毛片免费 | 欧美网站一区二区 | 在线a视频 | 国产精品观看 |