golang json 效能分析
Json 作為一種重要的資料格式,具有良好的可讀性以及自描述性,廣泛地應用在各種資料傳輸場景中。Go 語言裡面原生支援了這種資料格式的序列化以及反序列化,內部使用反射機制實現,效能有點差,在高度依賴 json 解析的應用裡,往往會成為效能瓶頸,好在已有很多第三方庫幫我們解決了這個問題,但是這麼多庫,對於像我這種有選擇困難症的人來說,到底要怎麼選擇呢,下面就給大家來一一分析一下
ffjson
go get -u github.com/pquerna/ffjson
原生的庫效能比較差的主要原因是使用了很多反射的機制,為了解決這個問題,ffjson 通過預編譯生成程式碼,型別的判斷在預編譯階段已經確定,避免了在執行時的反射
但也因此在編譯前需要多一個步驟,需要先生成 ffjson 程式碼,生成程式碼只需要執行 ffjson <file.go>
就可以了,其中 file.go
是一個包含 json 結構體定義的 go 檔案。注意這裡 ffjson 是這個庫提供的一個程式碼生成工具,直接執行上面的 go get
會把這個工具安裝在 $GOPATH/bin
目錄下,把 $GOPATH/bin
加到 $PATH
環境變數裡面,可以全域性訪問
另外,如果有些結構,不想讓 ffjson 生成程式碼,可以通過增加註釋的方式
// ffjson: skip
type Foo struct {
Bar string
}
// ffjson: nodecoder
type Foo struct {
Bar string
}
easyjson
go get -u github.com/mailru/easyjson/...
easyjson 的思想和 ffjson 是一致的,都是增加一個預編譯的過程,預先生成對應結構的序列化反序列化程式碼,除此之外,easyjson 還放棄了一些原生庫裡面支援的一些不必要的特性,比如:key 型別宣告,key 大小寫不敏感等等,以達到更高的效能
生成程式碼執行 easyjson -all <file.go>
即可,如果不指定 -all
引數,只會對帶有 //easyjson:json
的結構生成程式碼
//easyjson:json
type A struct {
Bar string
}
jsoniter
go get -u github.com/json-iterator/go
這是一個很神奇的庫,滴滴開發的,不像 easyjson 和 ffjson 都使用了預編譯,而且 100% 相容原生庫,但是效能超級好,也不知道怎麼實現的,如果有人知道的話,可以告訴我一下嗎?
使用上面,你只要把所有的
import "encoding/json"
替換成
import "github.com/json-iterator/go"
var json = jsoniter.ConfigCompatibleWithStandardLibrary
就可以了,其它都不需要動
codec-json
go get -u github.com/ugorji/go/codec
這個庫裡面其實包含很多內容,json 只是其中的一個功能,比較老,使用起來比較麻煩,效能也不是很好
jsonparser
go get -u github.com/buger/jsonparser
嚴格來說,這個庫不屬於 json 序列化的庫,只是提供了一些 json 解析的介面,使用的時候需要自己去設定結構裡面的值,事實上,每次呼叫都需要重新解析 json 物件,效能並不是很好
就像名字暗示的那樣,這個庫只是一個解析庫,並沒有序列化的介面
效能測試
對上面這些 json 庫,作了一些效能測試,測試程式碼在:https://github.com/hatlonely/hellogolang/blob/master/internal/json/json_benchmark_test.go,下面是在我的 Macbook 上測試的結果(實際結果和庫的版本以及機器環境有關,建議自己再測試一遍):
BenchmarkMarshalStdJson-4 1000000 1097 ns/op
BenchmarkMarshalJsonIterator-4 2000000 781 ns/op
BenchmarkMarshalFfjson-4 2000000 941 ns/op
BenchmarkMarshalEasyjson-4 3000000 513 ns/op
BenchmarkMarshalCodecJson-4 1000000 1074 ns/op
BenchmarkMarshalCodecJsonWithBufio-4 1000000 2161 ns/op
BenchmarkUnMarshalStdJson-4 500000 2512 ns/op
BenchmarkUnMarshalJsonIterator-4 2000000 591 ns/op
BenchmarkUnMarshalFfjson-4 1000000 1127 ns/op
BenchmarkUnMarshalEasyjson-4 2000000 608 ns/op
BenchmarkUnMarshalCodecJson-4 20000 122694 ns/op
BenchmarkUnMarshalCodecJsonWithBufio-4 500000 3417 ns/op
BenchmarkUnMarshalJsonparser-4 2000000 877 ns/op
從上面的結果可以看出來:
- easyjson 無論是序列化還是反序列化都是最優的,序列化提升了1倍,反序列化提升了3倍
- jsoniter 效能也很好,接近於easyjson,關鍵是沒有預編譯過程,100%相容原生庫
- ffjson 的序列化提升並不明顯,反序列化提升了1倍
- codecjson 和原生庫相比,差不太多,甚至更差
- jsonparser 不太適合這樣的場景,效能提升並不明顯,而且沒有反序列化
所以綜合考慮,建議大家使用 jsoniter,如果追求極致的效能,考慮 easyjson
參考連結
ffjson: https://github.com/pquerna/ffjson easyjson: https://github.com/mailru/easyjson jsoniter: https://github.com/json-iterator/go jsonparser: https://github.com/buger/jsonparser codecjson: http://ugorji.net/blog/go-codec-primer
轉載請註明出處 本文連結:http://hatlonely.github.io/2018/01/28/golang-json-效能分析/
相關文章
- Golang高效能json包:easyjsonGolangJSON
- Golang效能分析與優化Golang優化
- golang JSON技巧GolangJSON
- golang 效能調優分析工具 pprof(下)Golang
- golang 效能調優分析工具 pprof (上)Golang
- Golang 流式解析 JsonGolangJSON
- Golang 的 JSON 包GolangJSON
- golang 效能優化分析:benchmark 結合 pprofGolang優化
- golang 使用pprof和go-torch做效能分析Golang
- golang json處理問題GolangJSON
- 細說 Golang 的 JSON 解析GolangJSON
- golang json字串轉結構體GolangJSON字串結構體
- golang中struct、json、map互相轉化GolangStructJSON
- Golang逃逸分析Golang
- JSON劫持漏洞分析JSON
- golang 解析php輸出json相容問題GolangPHPJSON
- php 透過 JSON RPC 與 golang 通訊PHPJSONRPCGolang
- Golang net/http 效能優化GolangHTTP優化
- Golang CLOSE WAIT 分析GolangAI
- Golang 效能測試 (3) 跟蹤刨析 golang traceGolang
- 高效能的JSON logger - onelogJSON
- 如何提升JSON.stringify()的效能?JSON
- 如何提升JSON.stringify的效能?JSON
- Golang操作結構體、Map轉化為JSONGolang結構體JSON
- Golang高效實踐之interface、reflection、json實踐GolangJSON
- Golang:將日誌以Json格式輸出到KafkaGolangJSONKafka
- Json序列化在golang中的應用JSONGolang
- CYQ.Data 操作 Json 效能測試:對比 Newtonsoft.JsonJSON
- Golang pprof 效能調優實戰,效能提升 3 倍!Golang
- Golang效能最佳化實踐Golang
- Golang WaitGroup原始碼分析GolangAI原始碼
- golang物件導向分析Golang物件
- Golang 大殺器之效能剖析 PProfGolang
- golang 效能優化之 bitset 代替 hashsetGolang優化
- golang 效能優化之累加雜湊Golang優化
- Java幾種常用JSON庫效能比較JavaJSON
- CPU效能分析
- 效能分析大全
- Java 效能分析Java