From 2558d4399f5437ce07958e64d27685376009259c Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Thu, 28 Jan 2021 22:19:16 +0900 Subject: [PATCH] Add benchmark for encoding of type which implemented MarshalJSON --- benchmarks/encode_test.go | 57 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/benchmarks/encode_test.go b/benchmarks/encode_test.go index be2c92c..7031b58 100644 --- a/benchmarks/encode_test.go +++ b/benchmarks/encode_test.go @@ -603,3 +603,60 @@ func Benchmark_Encode_Int_GoJson(b *testing.B) { } } } + +type marshaler struct{} + +func (*marshaler) MarshalJSON() ([]byte, error) { + return []byte(`"hello"`), nil +} + +func Benchmark_Encode_MarshalJSON_EncodingJson(b *testing.B) { + v := &marshaler{} + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if _, err := json.Marshal(v); err != nil { + b.Fatal(err) + } + } +} + +func Benchmark_Encode_MarshalJSON_JsonIter(b *testing.B) { + v := &marshaler{} + var json = jsoniter.ConfigCompatibleWithStandardLibrary + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if _, err := json.Marshal(v); err != nil { + b.Fatal(err) + } + } +} + +func Benchmark_Encode_MarshalJSON_Jettison(b *testing.B) { + v := &marshaler{} + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if _, err := jettison.Marshal(v); err != nil { + b.Fatal(err) + } + } +} + +func Benchmark_Encode_MarshalJSON_SegmentioJson(b *testing.B) { + v := &marshaler{} + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if _, err := segmentiojson.Marshal(v); err != nil { + b.Fatal(err) + } + } +} + +func Benchmark_Encode_MarshalJSON_GoJson(b *testing.B) { + v := &marshaler{} + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if _, err := gojson.Marshal(v); err != nil { + b.Fatal(err) + } + } +}