From 7028c33e6775f3e5d414f77d1f060bdfece6b010 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Sun, 20 Dec 2020 03:57:24 +0900 Subject: [PATCH] Add github.com/segmentio/encoding/json to benchmark --- benchmarks/encode_test.go | 181 ++++++++++++++++++++++++++++++++++++++ benchmarks/go.mod | 1 + benchmarks/go.sum | 2 + 3 files changed, 184 insertions(+) diff --git a/benchmarks/encode_test.go b/benchmarks/encode_test.go index ba1d69f..582b031 100644 --- a/benchmarks/encode_test.go +++ b/benchmarks/encode_test.go @@ -7,6 +7,7 @@ import ( gojay "github.com/francoispqt/gojay" gojson "github.com/goccy/go-json" jsoniter "github.com/json-iterator/go" + segmentiojson "github.com/segmentio/encoding/json" ) 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) { b.ReportAllocs() 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) { b.ReportAllocs() 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) { b.ReportAllocs() 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) { b.ReportAllocs() 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) { b.ReportAllocs() 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) + } + } +} diff --git a/benchmarks/go.mod b/benchmarks/go.mod index b7e683c..a4af8ae 100644 --- a/benchmarks/go.mod +++ b/benchmarks/go.mod @@ -6,6 +6,7 @@ require ( 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/segmentio/encoding v0.2.4 ) replace github.com/goccy/go-json => ../ diff --git a/benchmarks/go.sum b/benchmarks/go.sum index 714d88f..1aa81f2 100644 --- a/benchmarks/go.sum +++ b/benchmarks/go.sum @@ -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/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/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/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=