Add benchmark for encoding of type which implemented MarshalJSON

This commit is contained in:
Masaaki Goshima 2021-01-28 22:19:16 +09:00
parent 94dcae2d48
commit 2558d4399f
1 changed files with 57 additions and 0 deletions

View File

@ -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)
}
}
}