go-json/benchmarks/decode_test.go

480 lines
12 KiB
Go
Raw Normal View History

2020-04-26 06:03:17 +03:00
package benchmark
import (
2020-07-31 06:22:00 +03:00
"bytes"
2020-04-26 06:03:17 +03:00
"encoding/json"
"testing"
gojay "github.com/francoispqt/gojay"
gojson "github.com/goccy/go-json"
jsoniter "github.com/json-iterator/go"
segmentiojson "github.com/segmentio/encoding/json"
2021-04-28 10:59:01 +03:00
fastjson "github.com/valyala/fastjson"
2020-04-26 06:03:17 +03:00
)
2020-07-31 12:24:33 +03:00
func Benchmark_Decode_SmallStruct_Unmarshal_EncodingJson(b *testing.B) {
2020-04-26 06:03:17 +03:00
b.ReportAllocs()
for n := 0; n < b.N; n++ {
result := SmallPayload{}
if err := json.Unmarshal(SmallFixture, &result); err != nil {
b.Fatal(err)
}
}
}
2021-04-28 10:59:01 +03:00
func Benchmark_Decode_SmallStruct_Unmarshal_FastJson(b *testing.B) {
smallFixture := string(SmallFixture)
b.ReportAllocs()
for n := 0; n < b.N; n++ {
var p fastjson.Parser
if _, err := p.Parse(smallFixture); err != nil {
b.Fatal(err)
}
}
}
2021-06-21 17:57:46 +03:00
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)
}
}
}
2020-07-31 12:24:33 +03:00
func Benchmark_Decode_SmallStruct_Unmarshal_JsonIter(b *testing.B) {
2020-04-26 06:03:17 +03:00
b.ReportAllocs()
for n := 0; n < b.N; n++ {
result := SmallPayload{}
if err := jsoniter.Unmarshal(SmallFixture, &result); err != nil {
b.Fatal(err)
}
}
}
2020-07-31 12:24:33 +03:00
func Benchmark_Decode_SmallStruct_Unmarshal_GoJay(b *testing.B) {
2020-07-31 12:07:40 +03:00
b.ReportAllocs()
for n := 0; n < b.N; n++ {
result := SmallPayload{}
2020-07-31 12:24:33 +03:00
if err := gojay.UnmarshalJSONObject(SmallFixture, &result); err != nil {
2020-07-31 12:07:40 +03:00
b.Fatal(err)
}
}
}
2020-07-31 12:24:33 +03:00
func Benchmark_Decode_SmallStruct_Unmarshal_GoJayUnsafe(b *testing.B) {
2020-04-26 06:03:17 +03:00
b.ReportAllocs()
2020-07-31 12:24:33 +03:00
for i := 0; i < b.N; i++ {
2020-04-26 06:03:17 +03:00
result := SmallPayload{}
2020-07-31 12:24:33 +03:00
if err := gojay.Unsafe.UnmarshalJSONObject(SmallFixture, &result); err != nil {
2020-04-26 06:03:17 +03:00
b.Fatal(err)
}
}
}
2020-07-31 12:24:33 +03:00
func Benchmark_Decode_SmallStruct_Unmarshal_GoJson(b *testing.B) {
2020-04-26 06:03:17 +03:00
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := SmallPayload{}
2020-07-31 12:24:33 +03:00
if err := gojson.Unmarshal(SmallFixture, &result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_SmallStruct_Unmarshal_GoJsonNoEscape(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := SmallPayload{}
if err := gojson.UnmarshalNoEscape(SmallFixture, &result); err != nil {
2020-04-26 06:03:17 +03:00
b.Fatal(err)
}
}
}
2020-07-31 12:24:33 +03:00
func Benchmark_Decode_SmallStruct_Stream_EncodingJson(b *testing.B) {
2020-07-31 06:22:00 +03:00
b.ReportAllocs()
2020-07-31 11:10:03 +03:00
reader := bytes.NewReader(SmallFixture)
2020-07-31 06:22:00 +03:00
for i := 0; i < b.N; i++ {
result := SmallPayload{}
2020-07-31 11:10:03 +03:00
reader.Reset(SmallFixture)
2020-07-31 12:24:33 +03:00
if err := json.NewDecoder(reader).Decode(&result); err != nil {
2020-07-31 06:22:00 +03:00
b.Fatal(err)
}
}
}
2021-06-21 17:57:46 +03:00
func Benchmark_Decode_SmallStruct_Stream_SegmentioJson(b *testing.B) {
2020-04-26 06:03:17 +03:00
b.ReportAllocs()
2020-07-31 12:24:33 +03:00
reader := bytes.NewReader(SmallFixture)
2020-04-26 06:03:17 +03:00
for i := 0; i < b.N; i++ {
result := SmallPayload{}
2020-07-31 12:24:33 +03:00
reader.Reset(SmallFixture)
2021-06-21 17:57:46 +03:00
if err := segmentiojson.NewDecoder(reader).Decode(&result); err != nil {
2020-04-26 06:03:17 +03:00
b.Fatal(err)
}
}
}
2021-06-21 17:57:46 +03:00
func Benchmark_Decode_SmallStruct_Stream_JsonIter(b *testing.B) {
2020-04-26 06:03:17 +03:00
b.ReportAllocs()
2020-07-31 12:24:33 +03:00
reader := bytes.NewReader(SmallFixture)
2021-06-21 17:57:46 +03:00
for i := 0; i < b.N; i++ {
2020-07-31 12:24:33 +03:00
result := SmallPayload{}
2021-06-21 17:57:46 +03:00
reader.Reset(SmallFixture)
if err := jsoniter.NewDecoder(reader).Decode(&result); err != nil {
2020-07-31 12:24:33 +03:00
b.Fatal(err)
}
}
}
2021-06-21 17:57:46 +03:00
func Benchmark_Decode_SmallStruct_Stream_GoJay(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(SmallFixture)
2021-06-21 17:57:46 +03:00
for n := 0; n < b.N; n++ {
reader.Reset(SmallFixture)
2021-06-21 17:57:46 +03:00
result := SmallPayload{}
if err := gojay.NewDecoder(reader).DecodeObject(&result); err != nil {
b.Fatal(err)
}
}
}
2020-07-31 12:24:33 +03:00
func Benchmark_Decode_SmallStruct_Stream_GoJson(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(SmallFixture)
2020-04-26 06:03:17 +03:00
for i := 0; i < b.N; i++ {
result := SmallPayload{}
2020-07-31 12:24:33 +03:00
reader.Reset(SmallFixture)
if err := gojson.NewDecoder(reader).Decode(&result); err != nil {
2020-04-26 06:03:17 +03:00
b.Fatal(err)
}
}
}
2020-07-31 14:24:39 +03:00
func Benchmark_Decode_MediumStruct_Unmarshal_EncodingJson(b *testing.B) {
2020-04-26 06:03:17 +03:00
b.ReportAllocs()
for n := 0; n < b.N; n++ {
result := MediumPayload{}
if err := json.Unmarshal(MediumFixture, &result); err != nil {
b.Fatal(err)
}
}
}
2021-04-28 10:59:01 +03:00
func Benchmark_Decode_MediumStruct_Unmarshal_FastJson(b *testing.B) {
mediumFixture := string(MediumFixture)
b.ReportAllocs()
for n := 0; n < b.N; n++ {
var p fastjson.Parser
if _, err := p.Parse(mediumFixture); err != nil {
b.Fatal(err)
}
}
}
2021-06-21 17:57:46 +03:00
func Benchmark_Decode_MediumStruct_Unmarshal_SegmentioJson(b *testing.B) {
2020-04-26 06:03:17 +03:00
b.ReportAllocs()
2021-06-21 17:57:46 +03:00
for i := 0; i < b.N; i++ {
2020-04-26 06:03:17 +03:00
result := MediumPayload{}
2021-06-21 17:57:46 +03:00
if err := segmentiojson.Unmarshal(MediumFixture, &result); err != nil {
2020-04-26 06:03:17 +03:00
b.Fatal(err)
}
}
}
2021-06-21 17:57:46 +03:00
func Benchmark_Decode_MediumStruct_Unmarshal_JsonIter(b *testing.B) {
2020-04-26 06:03:17 +03:00
b.ReportAllocs()
for n := 0; n < b.N; n++ {
result := MediumPayload{}
2021-06-21 17:57:46 +03:00
if err := jsoniter.Unmarshal(MediumFixture, &result); err != nil {
2020-04-26 06:03:17 +03:00
b.Fatal(err)
}
}
}
2021-06-21 17:57:46 +03:00
func Benchmark_Decode_MediumStruct_Unmarshal_GoJay(b *testing.B) {
2020-04-26 06:03:17 +03:00
b.ReportAllocs()
2021-06-21 17:57:46 +03:00
for n := 0; n < b.N; n++ {
2020-04-26 06:03:17 +03:00
result := MediumPayload{}
2021-06-21 17:57:46 +03:00
if err := gojay.UnmarshalJSONObject(MediumFixture, &result); err != nil {
2020-04-26 06:03:17 +03:00
b.Fatal(err)
}
}
}
2021-06-21 17:57:46 +03:00
func Benchmark_Decode_MediumStruct_Unmarshal_GoJayUnsafe(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := MediumPayload{}
2021-06-21 17:57:46 +03:00
if err := gojay.Unsafe.UnmarshalJSONObject(MediumFixture, &result); err != nil {
b.Fatal(err)
}
}
}
2020-07-31 14:24:39 +03:00
func Benchmark_Decode_MediumStruct_Unmarshal_GoJson(b *testing.B) {
2020-04-26 06:03:17 +03:00
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := MediumPayload{}
if err := gojson.Unmarshal(MediumFixture, &result); err != nil {
b.Fatal(err)
}
}
}
2020-07-31 14:24:39 +03:00
func Benchmark_Decode_MediumStruct_Unmarshal_GoJsonNoEscape(b *testing.B) {
2020-04-26 06:03:17 +03:00
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := MediumPayload{}
if err := gojson.UnmarshalNoEscape(MediumFixture, &result); err != nil {
b.Fatal(err)
}
}
}
2020-07-31 14:24:39 +03:00
func Benchmark_Decode_MediumStruct_Stream_EncodingJson(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(MediumFixture)
for i := 0; i < b.N; i++ {
result := MediumPayload{}
reader.Reset(MediumFixture)
if err := json.NewDecoder(reader).Decode(&result); err != nil {
b.Fatal(err)
}
}
}
2021-06-21 17:57:46 +03:00
func Benchmark_Decode_MediumStruct_Stream_SegmentioJson(b *testing.B) {
2020-07-31 14:24:39 +03:00
b.ReportAllocs()
reader := bytes.NewReader(MediumFixture)
2021-06-21 17:57:46 +03:00
for n := 0; n < b.N; n++ {
2020-07-31 14:24:39 +03:00
reader.Reset(MediumFixture)
2021-06-21 17:57:46 +03:00
result := MediumPayload{}
if err := segmentiojson.NewDecoder(reader).Decode(&result); err != nil {
2020-07-31 14:24:39 +03:00
b.Fatal(err)
}
}
}
2021-06-21 17:57:46 +03:00
func Benchmark_Decode_MediumStruct_Stream_JsonIter(b *testing.B) {
2020-07-31 14:24:39 +03:00
b.ReportAllocs()
reader := bytes.NewReader(MediumFixture)
2021-06-21 17:57:46 +03:00
for i := 0; i < b.N; i++ {
2020-07-31 14:24:39 +03:00
result := MediumPayload{}
2021-06-21 17:57:46 +03:00
reader.Reset(MediumFixture)
if err := jsoniter.NewDecoder(reader).Decode(&result); err != nil {
2020-07-31 14:24:39 +03:00
b.Fatal(err)
}
}
}
2021-06-21 17:57:46 +03:00
func Benchmark_Decode_MediumStruct_Stream_GoJay(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(MediumFixture)
for n := 0; n < b.N; n++ {
reader.Reset(MediumFixture)
result := MediumPayload{}
2021-06-21 17:57:46 +03:00
if err := gojay.NewDecoder(reader).DecodeObject(&result); err != nil {
b.Fatal(err)
}
}
}
2020-07-31 14:24:39 +03:00
func Benchmark_Decode_MediumStruct_Stream_GoJson(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(MediumFixture)
for i := 0; i < b.N; i++ {
result := MediumPayload{}
reader.Reset(MediumFixture)
if err := gojson.NewDecoder(reader).Decode(&result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_LargeStruct_Unmarshal_EncodingJson(b *testing.B) {
2020-04-26 06:03:17 +03:00
b.ReportAllocs()
for n := 0; n < b.N; n++ {
result := LargePayload{}
if err := json.Unmarshal(LargeFixture, &result); err != nil {
b.Fatal(err)
}
}
}
2021-04-28 10:59:01 +03:00
func Benchmark_Decode_LargeStruct_Unmarshal_FastJson(b *testing.B) {
largeFixture := string(LargeFixture)
b.ReportAllocs()
for n := 0; n < b.N; n++ {
var p fastjson.Parser
if _, err := p.Parse(largeFixture); err != nil {
b.Fatal(err)
}
}
}
2021-06-21 17:57:46 +03:00
func Benchmark_Decode_LargeStruct_Unmarshal_SegmentioJson(b *testing.B) {
2020-04-26 06:03:17 +03:00
b.ReportAllocs()
2021-06-21 17:57:46 +03:00
for i := 0; i < b.N; i++ {
2020-04-26 06:03:17 +03:00
result := LargePayload{}
2021-06-21 17:57:46 +03:00
if err := segmentiojson.Unmarshal(LargeFixture, &result); err != nil {
2020-04-26 06:03:17 +03:00
b.Fatal(err)
}
}
}
2021-06-21 17:57:46 +03:00
func Benchmark_Decode_LargeStruct_Unmarshal_JsonIter(b *testing.B) {
2020-04-26 06:03:17 +03:00
b.ReportAllocs()
for n := 0; n < b.N; n++ {
result := LargePayload{}
2021-06-21 17:57:46 +03:00
if err := jsoniter.Unmarshal(LargeFixture, &result); err != nil {
2020-04-26 06:03:17 +03:00
b.Fatal(err)
}
}
}
2021-06-21 17:57:46 +03:00
func Benchmark_Decode_LargeStruct_Unmarshal_GoJay(b *testing.B) {
2020-04-26 06:03:17 +03:00
b.ReportAllocs()
2021-06-21 17:57:46 +03:00
for n := 0; n < b.N; n++ {
2020-04-26 06:03:17 +03:00
result := LargePayload{}
2021-06-21 17:57:46 +03:00
if err := gojay.UnmarshalJSONObject(LargeFixture, &result); err != nil {
2020-04-26 06:03:17 +03:00
b.Fatal(err)
}
}
}
2021-06-21 17:57:46 +03:00
func Benchmark_Decode_LargeStruct_Unmarshal_GoJayUnsafe(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := LargePayload{}
2021-06-21 17:57:46 +03:00
if err := gojay.Unsafe.UnmarshalJSONObject(LargeFixture, &result); err != nil {
b.Fatal(err)
}
}
}
2020-07-31 14:24:39 +03:00
func Benchmark_Decode_LargeStruct_Unmarshal_GoJson(b *testing.B) {
2020-04-26 06:03:17 +03:00
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := LargePayload{}
if err := gojson.Unmarshal(LargeFixture, &result); err != nil {
b.Fatal(err)
}
}
}
2020-07-31 14:24:39 +03:00
func Benchmark_Decode_LargeStruct_Unmarshal_GoJsonNoEscape(b *testing.B) {
2020-04-26 06:03:17 +03:00
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := LargePayload{}
if err := gojson.UnmarshalNoEscape(LargeFixture, &result); err != nil {
b.Fatal(err)
}
}
}
2020-07-31 14:28:04 +03:00
2021-06-05 07:00:26 +03:00
func Benchmark_Decode_LargeStruct_Unmarshal_GoJsonFirstWinMode(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := LargePayload{}
if err := gojson.UnmarshalWithOption(
LargeFixture,
&result,
gojson.DecodeFieldPriorityFirstWin(),
); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_LargeStruct_Unmarshal_GoJsonNoEscapeFirstWinMode(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := LargePayload{}
if err := gojson.UnmarshalNoEscape(
LargeFixture,
&result,
gojson.DecodeFieldPriorityFirstWin(),
); err != nil {
b.Fatal(err)
}
}
}
2020-07-31 14:28:04 +03:00
func Benchmark_Decode_LargeStruct_Stream_EncodingJson(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(LargeFixture)
for i := 0; i < b.N; i++ {
result := LargePayload{}
reader.Reset(LargeFixture)
if err := json.NewDecoder(reader).Decode(&result); err != nil {
b.Fatal(err)
}
}
}
2021-06-21 17:57:46 +03:00
func Benchmark_Decode_LargeStruct_Stream_SegmentioJson(b *testing.B) {
2020-07-31 14:28:04 +03:00
b.ReportAllocs()
reader := bytes.NewReader(LargeFixture)
for i := 0; i < b.N; i++ {
result := LargePayload{}
reader.Reset(LargeFixture)
2021-06-21 17:57:46 +03:00
if err := segmentiojson.NewDecoder(reader).Decode(&result); err != nil {
2020-07-31 14:28:04 +03:00
b.Fatal(err)
}
}
}
2021-06-21 17:57:46 +03:00
func Benchmark_Decode_LargeStruct_Stream_JsonIter(b *testing.B) {
2020-07-31 14:28:04 +03:00
b.ReportAllocs()
reader := bytes.NewReader(LargeFixture)
2021-06-21 17:57:46 +03:00
for i := 0; i < b.N; i++ {
2020-07-31 14:28:04 +03:00
result := LargePayload{}
2021-06-21 17:57:46 +03:00
reader.Reset(LargeFixture)
if err := jsoniter.NewDecoder(reader).Decode(&result); err != nil {
2020-07-31 14:28:04 +03:00
b.Fatal(err)
}
}
}
2021-06-21 17:57:46 +03:00
func Benchmark_Decode_LargeStruct_Stream_GoJay(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(LargeFixture)
2021-06-21 17:57:46 +03:00
for n := 0; n < b.N; n++ {
reader.Reset(LargeFixture)
2021-06-21 17:57:46 +03:00
result := LargePayload{}
if err := gojay.NewDecoder(reader).DecodeObject(&result); err != nil {
b.Fatal(err)
}
}
}
2020-07-31 14:28:04 +03:00
func Benchmark_Decode_LargeStruct_Stream_GoJson(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(LargeFixture)
for i := 0; i < b.N; i++ {
result := LargePayload{}
reader.Reset(LargeFixture)
if err := gojson.NewDecoder(reader).Decode(&result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_LargeStruct_Stream_GoJsonFirstWinMode(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(LargeFixture)
for i := 0; i < b.N; i++ {
result := LargePayload{}
reader.Reset(LargeFixture)
if err := gojson.NewDecoder(reader).DecodeWithOption(
&result,
gojson.DecodeFieldPriorityFirstWin(),
); err != nil {
b.Fatal(err)
}
}
}