Add github.com/segmentio/encoding/json to benchmark

This commit is contained in:
Masaaki Goshima 2020-12-20 03:57:24 +09:00
parent 0e0bf702c1
commit 7028c33e67
3 changed files with 184 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
gojay "github.com/francoispqt/gojay" gojay "github.com/francoispqt/gojay"
gojson "github.com/goccy/go-json" gojson "github.com/goccy/go-json"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
segmentiojson "github.com/segmentio/encoding/json"
) )
func Benchmark_Encode_SmallStruct_EncodingJson(b *testing.B) { func Benchmark_Encode_SmallStruct_EncodingJson(b *testing.B) {
@ -37,6 +38,15 @@ func Benchmark_Encode_SmallStruct_GoJay(b *testing.B) {
} }
} }
func Benchmark_Encode_SmallStruct_SegmentioJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := segmentiojson.Marshal(NewSmallPayload()); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_SmallStruct_GoJson(b *testing.B) { func Benchmark_Encode_SmallStruct_GoJson(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -46,6 +56,57 @@ func Benchmark_Encode_SmallStruct_GoJson(b *testing.B) {
} }
} }
func Benchmark_Encode_SmallStructCached_EncodingJson(b *testing.B) {
cached := NewSmallPayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(cached); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_SmallStructCached_JsonIter(b *testing.B) {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
cached := NewSmallPayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(cached); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_SmallStructCached_GoJay(b *testing.B) {
cached := NewSmallPayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojay.MarshalJSONObject(cached); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_SmallStructCached_SegmentioJson(b *testing.B) {
cached := NewSmallPayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := segmentiojson.Marshal(cached); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_SmallStructCached_GoJson(b *testing.B) {
cached := NewSmallPayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.Marshal(cached); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MediumStruct_EncodingJson(b *testing.B) { func Benchmark_Encode_MediumStruct_EncodingJson(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -74,6 +135,15 @@ func Benchmark_Encode_MediumStruct_GoJay(b *testing.B) {
} }
} }
func Benchmark_Encode_MediumStruct_SegmentioJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := segmentiojson.Marshal(NewMediumPayload()); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MediumStruct_GoJson(b *testing.B) { func Benchmark_Encode_MediumStruct_GoJson(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -83,6 +153,57 @@ func Benchmark_Encode_MediumStruct_GoJson(b *testing.B) {
} }
} }
func Benchmark_Encode_MediumStructCached_EncodingJson(b *testing.B) {
cached := NewMediumPayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(cached); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MediumStructCached_JsonIter(b *testing.B) {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
cached := NewMediumPayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(cached); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MediumStructCached_GoJay(b *testing.B) {
cached := NewMediumPayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojay.MarshalJSONObject(cached); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MediumStructCached_SegmentioJson(b *testing.B) {
cached := NewMediumPayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := segmentiojson.Marshal(cached); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MediumStructCached_GoJson(b *testing.B) {
cached := NewMediumPayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.Marshal(cached); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_LargeStruct_EncodingJson(b *testing.B) { func Benchmark_Encode_LargeStruct_EncodingJson(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -111,6 +232,15 @@ func Benchmark_Encode_LargeStruct_GoJay(b *testing.B) {
} }
} }
func Benchmark_Encode_LargeStruct_SegmentioJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := segmentiojson.Marshal(NewLargePayload()); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_LargeStruct_GoJson(b *testing.B) { func Benchmark_Encode_LargeStruct_GoJson(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -119,3 +249,54 @@ func Benchmark_Encode_LargeStruct_GoJson(b *testing.B) {
} }
} }
} }
func Benchmark_Encode_LargeStructCached_EncodingJson(b *testing.B) {
cached := NewLargePayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(cached); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_LargeStructCached_JsonIter(b *testing.B) {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
cached := NewLargePayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(cached); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_LargeStructCached_GoJay(b *testing.B) {
cached := NewLargePayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojay.MarshalJSONObject(cached); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_LargeStructCached_SegmentioJson(b *testing.B) {
cached := NewLargePayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := segmentiojson.Marshal(cached); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_LargeStructCached_GoJson(b *testing.B) {
cached := NewLargePayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.Marshal(cached); err != nil {
b.Fatal(err)
}
}
}

View File

@ -6,6 +6,7 @@ require (
github.com/francoispqt/gojay v1.2.13 github.com/francoispqt/gojay v1.2.13
github.com/goccy/go-json v0.0.0-00010101000000-000000000000 github.com/goccy/go-json v0.0.0-00010101000000-000000000000
github.com/json-iterator/go v1.1.9 github.com/json-iterator/go v1.1.9
github.com/segmentio/encoding v0.2.4
) )
replace github.com/goccy/go-json => ../ replace github.com/goccy/go-json => ../

View File

@ -75,6 +75,8 @@ github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:
github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/segmentio/encoding v0.2.4 h1:TQRXhTlXj4urZe3Z5QVgxs9Ad1i7GYHg9peAtjOPe28=
github.com/segmentio/encoding v0.2.4/go.mod h1:MJjRE6bMDocliO2FyFC2Dusp+uYdBfHWh5Bw7QyExto=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY= github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY=
github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM= github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM=