From 01698ac4a54aed5466c51b2a3f37f1de10c6d2a3 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Mon, 21 Jun 2021 23:54:52 +0900 Subject: [PATCH 1/2] Add bytedance/sonic to benchmark --- benchmarks/decode_test.go | 31 +++++++++++++++++++++ benchmarks/encode_test.go | 58 +++++++++++++++++++++++++++++++++++++++ benchmarks/go.mod | 3 +- benchmarks/go.sum | 15 ++++++++++ 4 files changed, 106 insertions(+), 1 deletion(-) diff --git a/benchmarks/decode_test.go b/benchmarks/decode_test.go index d074c1d..8621352 100644 --- a/benchmarks/decode_test.go +++ b/benchmarks/decode_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "testing" + "github.com/bytedance/sonic" gojay "github.com/francoispqt/gojay" gojson "github.com/goccy/go-json" jsoniter "github.com/json-iterator/go" @@ -33,6 +34,16 @@ func Benchmark_Decode_SmallStruct_Unmarshal_FastJson(b *testing.B) { } } +func Benchmark_Decode_SmallStruct_Unmarshal_Sonic(b *testing.B) { + b.ReportAllocs() + for n := 0; n < b.N; n++ { + result := SmallPayload{} + if err := sonic.Unmarshal(SmallFixture, &result); err != nil { + b.Fatal(err) + } + } +} + func Benchmark_Decode_SmallStruct_Unmarshal_JsonIter(b *testing.B) { b.ReportAllocs() for n := 0; n < b.N; n++ { @@ -174,6 +185,16 @@ func Benchmark_Decode_MediumStruct_Unmarshal_FastJson(b *testing.B) { } } +func Benchmark_Decode_MediumStruct_Unmarshal_Sonic(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + result := MediumPayload{} + if err := sonic.Unmarshal(MediumFixture, &result); err != nil { + b.Fatal(err) + } + } +} + func Benchmark_Decode_MediumStruct_Unmarshal_JsonIter(b *testing.B) { b.ReportAllocs() for n := 0; n < b.N; n++ { @@ -315,6 +336,16 @@ func Benchmark_Decode_LargeStruct_Unmarshal_FastJson(b *testing.B) { } } +func Benchmark_Decode_LargeStruct_Unmarshal_Sonic(b *testing.B) { + b.ReportAllocs() + for n := 0; n < b.N; n++ { + result := LargePayload{} + if err := sonic.Unmarshal(LargeFixture, &result); err != nil { + b.Fatal(err) + } + } +} + func Benchmark_Decode_LargeStruct_Unmarshal_JsonIter(b *testing.B) { b.ReportAllocs() for n := 0; n < b.N; n++ { diff --git a/benchmarks/encode_test.go b/benchmarks/encode_test.go index 4da8034..59575ce 100644 --- a/benchmarks/encode_test.go +++ b/benchmarks/encode_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "testing" + "github.com/bytedance/sonic" gojay "github.com/francoispqt/gojay" gojson "github.com/goccy/go-json" jsoniter "github.com/json-iterator/go" @@ -77,6 +78,15 @@ func Benchmark_Encode_SmallStruct_SegmentioJson(b *testing.B) { } } +func Benchmark_Encode_SmallStruct_Sonic(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if _, err := sonic.Marshal(NewSmallPayload()); err != nil { + b.Fatal(err) + } + } +} + func Benchmark_Encode_SmallStruct_GoJsonColored(b *testing.B) { colorOpt := gojson.Colorize(gojson.DefaultColorScheme) b.ReportAllocs() @@ -176,6 +186,16 @@ func Benchmark_Encode_SmallStructCached_SegmentioJson(b *testing.B) { } } +func Benchmark_Encode_SmallStructCached_Sonic(b *testing.B) { + cached := NewSmallPayload() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if _, err := sonic.Marshal(cached); err != nil { + b.Fatal(err) + } + } +} + func Benchmark_Encode_SmallStructCached_GoJsonColored(b *testing.B) { cached := NewSmallPayload() colorOpt := gojson.Colorize(gojson.DefaultColorScheme) @@ -262,6 +282,15 @@ func Benchmark_Encode_MediumStruct_SegmentioJson(b *testing.B) { } } +func Benchmark_Encode_MediumStruct_Sonic(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if _, err := sonic.Marshal(NewMediumPayload()); err != nil { + b.Fatal(err) + } + } +} + func Benchmark_Encode_MediumStruct_GoJsonColored(b *testing.B) { colorOpt := gojson.Colorize(gojson.DefaultColorScheme) b.ReportAllocs() @@ -351,6 +380,16 @@ func Benchmark_Encode_MediumStructCached_SegmentioJson(b *testing.B) { } } +func Benchmark_Encode_MediumStructCached_Sonic(b *testing.B) { + cached := NewMediumPayload() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if _, err := sonic.Marshal(cached); err != nil { + b.Fatal(err) + } + } +} + func Benchmark_Encode_MediumStructCached_GoJsonColored(b *testing.B) { cached := NewMediumPayload() colorOpt := gojson.Colorize(gojson.DefaultColorScheme) @@ -437,6 +476,15 @@ func Benchmark_Encode_LargeStruct_SegmentioJson(b *testing.B) { } } +func Benchmark_Encode_LargeStruct_Sonic(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if _, err := sonic.Marshal(NewLargePayload()); err != nil { + b.Fatal(err) + } + } +} + func Benchmark_Encode_LargeStruct_GoJsonColored(b *testing.B) { colorOpt := gojson.Colorize(gojson.DefaultColorScheme) b.ReportAllocs() @@ -526,6 +574,16 @@ func Benchmark_Encode_LargeStructCached_SegmentioJson(b *testing.B) { } } +func Benchmark_Encode_LargeStructCached_Sonic(b *testing.B) { + cached := NewLargePayload() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if _, err := sonic.Marshal(cached); err != nil { + b.Fatal(err) + } + } +} + func Benchmark_Encode_LargeStructCached_GoJsonColored(b *testing.B) { cached := NewLargePayload() colorOpt := gojson.Colorize(gojson.DefaultColorScheme) diff --git a/benchmarks/go.mod b/benchmarks/go.mod index d88b0ed..ba77afb 100644 --- a/benchmarks/go.mod +++ b/benchmarks/go.mod @@ -3,9 +3,10 @@ module benchmark go 1.12 require ( + github.com/bytedance/sonic v0.0.0-20210621033418-85a0d4219f94 github.com/francoispqt/gojay v1.2.13 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.10 github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7 github.com/segmentio/encoding v0.2.4 diff --git a/benchmarks/go.sum b/benchmarks/go.sum index a512e0b..9770754 100644 --- a/benchmarks/go.sum +++ b/benchmarks/go.sum @@ -12,6 +12,10 @@ github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYU github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g= github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s= +github.com/bytedance/sonic v0.0.0-20210621033418-85a0d4219f94 h1:+PwepVHd43KuN9mFnAgB4962W/zuF/RGsM7B73Dyt+8= +github.com/bytedance/sonic v0.0.0-20210621033418-85a0d4219f94/go.mod h1:+QewJQ2P8nkGhdzk5fHTSwRn03eDeJAbHWIlQnv95J4= +github.com/chenzhuoyu/base64x v0.0.0-20210528162528-3c6c11c43ee5 h1:7AStn2tanqGY99xzW+Ve1p6YYqnRr1m/yswJ4h0BhcY= +github.com/chenzhuoyu/base64x v0.0.0-20210528162528-3c6c11c43ee5/go.mod h1:NfDzX8KeqVNX62apij1OkqoeDdq1VR3g0TRZo99kkBA= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/coreos/go-systemd v0.0.0-20181012123002-c6f51f82210d/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= @@ -49,8 +53,12 @@ github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0 github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68= +github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/klauspost/cpuid/v2 v2.0.6 h1:dQ5ueTiftKxp0gyjKSx5+8BtPWkyQbd95m8Gys/RarI= +github.com/klauspost/cpuid/v2 v2.0.6/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -117,7 +125,13 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0 github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= +github.com/tidwall/gjson v1.8.0/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk= +github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.1.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/valyala/fastjson v1.6.3 h1:tAKFnnwmeMGPbwJ7IwxcTPCNr3uIzoIj3/Fh90ra4xc= github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY= github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU= @@ -188,6 +202,7 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= grpc.go4.org v0.0.0-20170609214715-11d0a25b4919/go.mod h1:77eQGdRu53HpSqPFJFmuJdjuHRquDANNeA4x7B8WQ9o= honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= From 291234afdbf290a64f48b0ba99a443810e32ea75 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Mon, 21 Jun 2021 23:57:46 +0900 Subject: [PATCH 2/2] Sort benchmark target --- benchmarks/decode_test.go | 132 +++++++++++++++++++------------------- 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/benchmarks/decode_test.go b/benchmarks/decode_test.go index 8621352..f963263 100644 --- a/benchmarks/decode_test.go +++ b/benchmarks/decode_test.go @@ -34,6 +34,16 @@ func Benchmark_Decode_SmallStruct_Unmarshal_FastJson(b *testing.B) { } } +func Benchmark_Decode_SmallStruct_Unmarshal_SegmentioJson(b *testing.B) { + b.ReportAllocs() + for n := 0; n < b.N; n++ { + result := SmallPayload{} + if err := segmentiojson.Unmarshal(SmallFixture, &result); err != nil { + b.Fatal(err) + } + } +} + func Benchmark_Decode_SmallStruct_Unmarshal_Sonic(b *testing.B) { b.ReportAllocs() for n := 0; n < b.N; n++ { @@ -74,16 +84,6 @@ func Benchmark_Decode_SmallStruct_Unmarshal_GoJayUnsafe(b *testing.B) { } } -func Benchmark_Decode_SmallStruct_Unmarshal_SegmentioJson(b *testing.B) { - b.ReportAllocs() - for n := 0; n < b.N; n++ { - result := SmallPayload{} - if err := segmentiojson.Unmarshal(SmallFixture, &result); err != nil { - b.Fatal(err) - } - } -} - func Benchmark_Decode_SmallStruct_Unmarshal_GoJson(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { @@ -116,6 +116,18 @@ func Benchmark_Decode_SmallStruct_Stream_EncodingJson(b *testing.B) { } } +func Benchmark_Decode_SmallStruct_Stream_SegmentioJson(b *testing.B) { + b.ReportAllocs() + reader := bytes.NewReader(SmallFixture) + for i := 0; i < b.N; i++ { + result := SmallPayload{} + reader.Reset(SmallFixture) + if err := segmentiojson.NewDecoder(reader).Decode(&result); err != nil { + b.Fatal(err) + } + } +} + func Benchmark_Decode_SmallStruct_Stream_JsonIter(b *testing.B) { b.ReportAllocs() reader := bytes.NewReader(SmallFixture) @@ -140,18 +152,6 @@ func Benchmark_Decode_SmallStruct_Stream_GoJay(b *testing.B) { } } -func Benchmark_Decode_SmallStruct_Stream_SegmentioJson(b *testing.B) { - b.ReportAllocs() - reader := bytes.NewReader(SmallFixture) - for i := 0; i < b.N; i++ { - result := SmallPayload{} - reader.Reset(SmallFixture) - if err := segmentiojson.NewDecoder(reader).Decode(&result); err != nil { - b.Fatal(err) - } - } -} - func Benchmark_Decode_SmallStruct_Stream_GoJson(b *testing.B) { b.ReportAllocs() reader := bytes.NewReader(SmallFixture) @@ -195,6 +195,16 @@ func Benchmark_Decode_MediumStruct_Unmarshal_Sonic(b *testing.B) { } } +func Benchmark_Decode_MediumStruct_Unmarshal_SegmentioJson(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + result := MediumPayload{} + if err := segmentiojson.Unmarshal(MediumFixture, &result); err != nil { + b.Fatal(err) + } + } +} + func Benchmark_Decode_MediumStruct_Unmarshal_JsonIter(b *testing.B) { b.ReportAllocs() for n := 0; n < b.N; n++ { @@ -225,16 +235,6 @@ func Benchmark_Decode_MediumStruct_Unmarshal_GoJayUnsafe(b *testing.B) { } } -func Benchmark_Decode_MediumStruct_Unmarshal_SegmentioJson(b *testing.B) { - b.ReportAllocs() - for i := 0; i < b.N; i++ { - result := MediumPayload{} - if err := segmentiojson.Unmarshal(MediumFixture, &result); err != nil { - b.Fatal(err) - } - } -} - func Benchmark_Decode_MediumStruct_Unmarshal_GoJson(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { @@ -267,6 +267,18 @@ func Benchmark_Decode_MediumStruct_Stream_EncodingJson(b *testing.B) { } } +func Benchmark_Decode_MediumStruct_Stream_SegmentioJson(b *testing.B) { + b.ReportAllocs() + reader := bytes.NewReader(MediumFixture) + for n := 0; n < b.N; n++ { + reader.Reset(MediumFixture) + result := MediumPayload{} + if err := segmentiojson.NewDecoder(reader).Decode(&result); err != nil { + b.Fatal(err) + } + } +} + func Benchmark_Decode_MediumStruct_Stream_JsonIter(b *testing.B) { b.ReportAllocs() reader := bytes.NewReader(MediumFixture) @@ -291,18 +303,6 @@ func Benchmark_Decode_MediumStruct_Stream_GoJay(b *testing.B) { } } -func Benchmark_Decode_MediumStruct_Stream_SegmentioJson(b *testing.B) { - b.ReportAllocs() - reader := bytes.NewReader(MediumFixture) - for n := 0; n < b.N; n++ { - reader.Reset(MediumFixture) - result := MediumPayload{} - if err := segmentiojson.NewDecoder(reader).Decode(&result); err != nil { - b.Fatal(err) - } - } -} - func Benchmark_Decode_MediumStruct_Stream_GoJson(b *testing.B) { b.ReportAllocs() reader := bytes.NewReader(MediumFixture) @@ -346,6 +346,16 @@ func Benchmark_Decode_LargeStruct_Unmarshal_Sonic(b *testing.B) { } } +func Benchmark_Decode_LargeStruct_Unmarshal_SegmentioJson(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + result := LargePayload{} + if err := segmentiojson.Unmarshal(LargeFixture, &result); err != nil { + b.Fatal(err) + } + } +} + func Benchmark_Decode_LargeStruct_Unmarshal_JsonIter(b *testing.B) { b.ReportAllocs() for n := 0; n < b.N; n++ { @@ -376,16 +386,6 @@ func Benchmark_Decode_LargeStruct_Unmarshal_GoJayUnsafe(b *testing.B) { } } -func Benchmark_Decode_LargeStruct_Unmarshal_SegmentioJson(b *testing.B) { - b.ReportAllocs() - for i := 0; i < b.N; i++ { - result := LargePayload{} - if err := segmentiojson.Unmarshal(LargeFixture, &result); err != nil { - b.Fatal(err) - } - } -} - func Benchmark_Decode_LargeStruct_Unmarshal_GoJson(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { @@ -446,6 +446,18 @@ func Benchmark_Decode_LargeStruct_Stream_EncodingJson(b *testing.B) { } } +func Benchmark_Decode_LargeStruct_Stream_SegmentioJson(b *testing.B) { + b.ReportAllocs() + reader := bytes.NewReader(LargeFixture) + for i := 0; i < b.N; i++ { + result := LargePayload{} + reader.Reset(LargeFixture) + if err := segmentiojson.NewDecoder(reader).Decode(&result); err != nil { + b.Fatal(err) + } + } +} + func Benchmark_Decode_LargeStruct_Stream_JsonIter(b *testing.B) { b.ReportAllocs() reader := bytes.NewReader(LargeFixture) @@ -470,18 +482,6 @@ func Benchmark_Decode_LargeStruct_Stream_GoJay(b *testing.B) { } } -func Benchmark_Decode_LargeStruct_Stream_SegmentioJson(b *testing.B) { - b.ReportAllocs() - reader := bytes.NewReader(LargeFixture) - for i := 0; i < b.N; i++ { - result := LargePayload{} - reader.Reset(LargeFixture) - if err := segmentiojson.NewDecoder(reader).Decode(&result); err != nil { - b.Fatal(err) - } - } -} - func Benchmark_Decode_LargeStruct_Stream_GoJson(b *testing.B) { b.ReportAllocs() reader := bytes.NewReader(LargeFixture)