Add benchmark for streaming encoding

This commit is contained in:
Masaaki Goshima 2021-01-31 23:01:39 +09:00
parent ffc954d356
commit 7895767eaa
1 changed files with 50 additions and 4 deletions

View File

@ -1,6 +1,7 @@
package benchmark package benchmark
import ( import (
"bytes"
"encoding/json" "encoding/json"
"testing" "testing"
@ -559,6 +560,51 @@ func Benchmark_Encode_Interface_GoJson(b *testing.B) {
} }
func Benchmark_Encode_Bool_EncodingJson(b *testing.B) { func Benchmark_Encode_Bool_EncodingJson(b *testing.B) {
b.ReportAllocs()
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
for i := 0; i < b.N; i++ {
if err := enc.Encode(true); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_Bool_JsonIter(b *testing.B) {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
b.ReportAllocs()
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
for i := 0; i < b.N; i++ {
if err := enc.Encode(true); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_Bool_SegmentioJson(b *testing.B) {
b.ReportAllocs()
var buf bytes.Buffer
enc := segmentiojson.NewEncoder(&buf)
for i := 0; i < b.N; i++ {
if err := enc.Encode(true); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_Bool_GoJson(b *testing.B) {
b.ReportAllocs()
var buf bytes.Buffer
enc := gojson.NewEncoder(&buf)
for i := 0; i < b.N; i++ {
if err := enc.Encode(true); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Marshal_Bool_EncodingJson(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
if _, err := json.Marshal(true); err != nil { if _, err := json.Marshal(true); err != nil {
@ -567,7 +613,7 @@ func Benchmark_Encode_Bool_EncodingJson(b *testing.B) {
} }
} }
func Benchmark_Encode_Bool_JsonIter(b *testing.B) { func Benchmark_Marshal_Bool_JsonIter(b *testing.B) {
var json = jsoniter.ConfigCompatibleWithStandardLibrary var json = jsoniter.ConfigCompatibleWithStandardLibrary
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -577,7 +623,7 @@ func Benchmark_Encode_Bool_JsonIter(b *testing.B) {
} }
} }
func Benchmark_Encode_Bool_Jettison(b *testing.B) { func Benchmark_Marshal_Bool_Jettison(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
if _, err := jettison.Marshal(true); err != nil { if _, err := jettison.Marshal(true); err != nil {
@ -586,7 +632,7 @@ func Benchmark_Encode_Bool_Jettison(b *testing.B) {
} }
} }
func Benchmark_Encode_Bool_SegmentioJson(b *testing.B) { func Benchmark_Marshal_Bool_SegmentioJson(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
if _, err := segmentiojson.Marshal(true); err != nil { if _, err := segmentiojson.Marshal(true); err != nil {
@ -595,7 +641,7 @@ func Benchmark_Encode_Bool_SegmentioJson(b *testing.B) {
} }
} }
func Benchmark_Encode_Bool_GoJson(b *testing.B) { func Benchmark_Marshal_Bool_GoJson(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
if _, err := gojson.Marshal(true); err != nil { if _, err := gojson.Marshal(true); err != nil {