1
0
Fork 0

Adding upstream version 0.10.5.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-22 08:03:40 +02:00
parent 104c0c203d
commit e733edafba
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
141 changed files with 102352 additions and 0 deletions

642
benchmarks/bench_test.go Normal file
View file

@ -0,0 +1,642 @@
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Large data benchmark.
// The JSON data is a summary of agl's changes in the
// go, webkit, and chromium open source projects.
// We benchmark converting between the JSON form
// and in-memory data structures.
package benchmark
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"os"
"strings"
"testing"
stdjson "encoding/json"
"github.com/goccy/go-json"
jsoniter "github.com/json-iterator/go"
segmentiojson "github.com/segmentio/encoding/json"
"github.com/wI2L/jettison"
)
type codeResponse struct {
Tree *codeNode `json:"tree"`
Username string `json:"username"`
}
type codeNode struct {
Name string `json:"name"`
Kids []*codeNode `json:"kids"`
CLWeight float64 `json:"cl_weight"`
Touches int `json:"touches"`
MinT int64 `json:"min_t"`
MaxT int64 `json:"max_t"`
MeanT int64 `json:"mean_t"`
}
var codeJSON []byte
var codeStruct codeResponse
func codeInit() {
f, err := os.Open("testdata/code.json.gz")
if err != nil {
panic(err)
}
defer f.Close()
gz, err := gzip.NewReader(f)
if err != nil {
panic(err)
}
data, err := io.ReadAll(gz)
if err != nil {
panic(err)
}
codeJSON = data
if err := stdjson.Unmarshal(codeJSON, &codeStruct); err != nil {
panic("unmarshal code.json: " + err.Error())
}
{
stdjsonbytes, err := stdjson.Marshal(&codeStruct)
if err != nil {
panic("marshal code.json: " + err.Error())
}
jsonbytes, err := json.Marshal(&codeStruct)
if err != nil {
panic("marshal code.json: " + err.Error())
}
if len(stdjsonbytes) != len(jsonbytes) {
panic(fmt.Sprintf("stdjson = %d but go-json = %d", len(stdjsonbytes), len(jsonbytes)))
}
}
if _, err := json.Marshal(&codeStruct); err != nil {
panic("marshal code.json: " + err.Error())
}
if !bytes.Equal(data, codeJSON) {
println("different lengths", len(data), len(codeJSON))
for i := 0; i < len(data) && i < len(codeJSON); i++ {
if data[i] != codeJSON[i] {
println("re-marshal: changed at byte", i)
println("orig: ", string(codeJSON[i-10:i+10]))
println("new: ", string(data[i-10:i+10]))
break
}
}
panic("re-marshal code.json: different result")
}
}
func Benchmark_EncodeBigData_GoJson(b *testing.B) {
b.ReportAllocs()
if codeJSON == nil {
b.StopTimer()
codeInit()
b.StartTimer()
}
b.RunParallel(func(pb *testing.PB) {
enc := json.NewEncoder(io.Discard)
for pb.Next() {
if err := enc.Encode(&codeStruct); err != nil {
b.Fatal("Encode:", err)
}
}
})
b.SetBytes(int64(len(codeJSON)))
}
func Benchmark_EncodeBigData_EncodingJson(b *testing.B) {
b.ReportAllocs()
if codeJSON == nil {
b.StopTimer()
codeInit()
b.StartTimer()
}
b.RunParallel(func(pb *testing.PB) {
enc := stdjson.NewEncoder(io.Discard)
for pb.Next() {
if err := enc.Encode(&codeStruct); err != nil {
b.Fatal("Encode:", err)
}
}
})
b.SetBytes(int64(len(codeJSON)))
}
func Benchmark_EncodeBigData_JsonIter(b *testing.B) {
b.ReportAllocs()
if codeJSON == nil {
b.StopTimer()
codeInit()
b.StartTimer()
}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
b.RunParallel(func(pb *testing.PB) {
enc := json.NewEncoder(io.Discard)
for pb.Next() {
if err := enc.Encode(&codeStruct); err != nil {
b.Fatal("Encode:", err)
}
}
})
b.SetBytes(int64(len(codeJSON)))
}
func Benchmark_EncodeBigData_SegmentioJson(b *testing.B) {
b.ReportAllocs()
if codeJSON == nil {
b.StopTimer()
codeInit()
b.StartTimer()
}
b.RunParallel(func(pb *testing.PB) {
enc := segmentiojson.NewEncoder(io.Discard)
for pb.Next() {
if err := enc.Encode(&codeStruct); err != nil {
b.Fatal("Encode:", err)
}
}
})
b.SetBytes(int64(len(codeJSON)))
}
func Benchmark_MarshalBigData_GoJson(b *testing.B) {
b.ReportAllocs()
if codeJSON == nil {
b.StopTimer()
codeInit()
b.StartTimer()
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err := json.Marshal(&codeStruct); err != nil {
b.Fatal("Marshal:", err)
}
}
})
b.SetBytes(int64(len(codeJSON)))
}
func Benchmark_MarshalBigData_EncodingJson(b *testing.B) {
b.ReportAllocs()
if codeJSON == nil {
b.StopTimer()
codeInit()
b.StartTimer()
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err := stdjson.Marshal(&codeStruct); err != nil {
b.Fatal("Marshal:", err)
}
}
})
b.SetBytes(int64(len(codeJSON)))
}
func Benchmark_MarshalBigData_JsonIter(b *testing.B) {
b.ReportAllocs()
if codeJSON == nil {
b.StopTimer()
codeInit()
b.StartTimer()
}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err := json.Marshal(&codeStruct); err != nil {
b.Fatal("Marshal:", err)
}
}
})
b.SetBytes(int64(len(codeJSON)))
}
func Benchmark_MarshalBigData_Jettison(b *testing.B) {
b.ReportAllocs()
if codeJSON == nil {
b.StopTimer()
codeInit()
b.StartTimer()
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err := jettison.Marshal(&codeStruct); err != nil {
b.Fatal("Marshal:", err)
}
}
})
b.SetBytes(int64(len(codeJSON)))
}
func Benchmark_MarshalBigData_SegmentioJson(b *testing.B) {
b.ReportAllocs()
if codeJSON == nil {
b.StopTimer()
codeInit()
b.StartTimer()
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err := segmentiojson.Marshal(&codeStruct); err != nil {
b.Fatal("Marshal:", err)
}
}
})
b.SetBytes(int64(len(codeJSON)))
}
func benchMarshalBytes(n int, marshaler func(interface{}) ([]byte, error)) func(*testing.B) {
sample := []byte("hello world")
// Use a struct pointer, to avoid an allocation when passing it as an
// interface parameter to Marshal.
v := &struct {
Bytes []byte
}{
bytes.Repeat(sample, (n/len(sample))+1)[:n],
}
return func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := marshaler(v); err != nil {
b.Fatal("Marshal:", err)
}
}
}
}
func Benchmark_MarshalBytes_EncodingJson(b *testing.B) {
// 32 fits within encodeState.scratch.
b.Run("32", benchMarshalBytes(32, stdjson.Marshal))
// 256 doesn't fit in encodeState.scratch, but is small enough to
// allocate and avoid the slower base64.NewEncoder.
b.Run("256", benchMarshalBytes(256, stdjson.Marshal))
// 4096 is large enough that we want to avoid allocating for it.
b.Run("4096", benchMarshalBytes(4096, stdjson.Marshal))
}
func Benchmark_MarshalBytes_JsonIter(b *testing.B) {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// 32 fits within encodeState.scratch.
b.Run("32", benchMarshalBytes(32, json.Marshal))
// 256 doesn't fit in encodeState.scratch, but is small enough to
// allocate and avoid the slower base64.NewEncoder.
b.Run("256", benchMarshalBytes(256, json.Marshal))
// 4096 is large enough that we want to avoid allocating for it.
b.Run("4096", benchMarshalBytes(4096, json.Marshal))
}
func Benchmark_MarshalBytes_GoJson(b *testing.B) {
b.ReportAllocs()
// 32 fits within encodeState.scratch.
b.Run("32", benchMarshalBytes(32, json.Marshal))
// 256 doesn't fit in encodeState.scratch, but is small enough to
// allocate and avoid the slower base64.NewEncoder.
b.Run("256", benchMarshalBytes(256, json.Marshal))
// 4096 is large enough that we want to avoid allocating for it.
b.Run("4096", benchMarshalBytes(4096, json.Marshal))
}
func Benchmark_EncodeRawMessage_EncodingJson(b *testing.B) {
b.ReportAllocs()
m := struct {
A int
B json.RawMessage
}{}
b.RunParallel(func(pb *testing.PB) {
enc := stdjson.NewEncoder(io.Discard)
for pb.Next() {
if err := enc.Encode(&m); err != nil {
b.Fatal("Encode:", err)
}
}
})
}
func Benchmark_EncodeRawMessage_JsonIter(b *testing.B) {
b.ReportAllocs()
m := struct {
A int
B json.RawMessage
}{}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
b.RunParallel(func(pb *testing.PB) {
enc := json.NewEncoder(io.Discard)
for pb.Next() {
if err := enc.Encode(&m); err != nil {
b.Fatal("Encode:", err)
}
}
})
}
func Benchmark_EncodeRawMessage_GoJson(b *testing.B) {
b.ReportAllocs()
m := struct {
A int
B json.RawMessage
}{}
b.RunParallel(func(pb *testing.PB) {
enc := json.NewEncoder(io.Discard)
for pb.Next() {
if err := enc.Encode(&m); err != nil {
b.Fatal("Encode:", err)
}
}
})
}
func Benchmark_MarshalString_EncodingJson(b *testing.B) {
b.ReportAllocs()
j := struct {
Bar string `json:"bar,string"`
}{
Bar: `foobar`,
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err := stdjson.Marshal(&j); err != nil {
b.Fatal(err)
}
}
})
}
func Benchmark_MarshalString_JsonIter(b *testing.B) {
b.ReportAllocs()
j := struct {
Bar string `json:"bar,string"`
}{
Bar: `foobar`,
}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err := json.Marshal(&j); err != nil {
b.Fatal(err)
}
}
})
}
func Benchmark_MarshalString_GoJson(b *testing.B) {
b.ReportAllocs()
j := struct {
Bar string `json:"bar,string"`
}{
Bar: `foobar`,
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err := json.Marshal(&j); err != nil {
b.Fatal(err)
}
}
})
}
func BenchmarkCodeDecoder(b *testing.B) {
b.ReportAllocs()
if codeJSON == nil {
b.StopTimer()
codeInit()
b.StartTimer()
}
b.RunParallel(func(pb *testing.PB) {
var buf bytes.Buffer
dec := json.NewDecoder(&buf)
var r codeResponse
for pb.Next() {
buf.Write(codeJSON)
// hide EOF
buf.WriteByte('\n')
buf.WriteByte('\n')
buf.WriteByte('\n')
if err := dec.Decode(&r); err != nil {
if err != io.EOF {
b.Fatal("Decode:", err)
}
}
}
})
b.SetBytes(int64(len(codeJSON)))
}
func BenchmarkUnicodeDecoder(b *testing.B) {
b.ReportAllocs()
j := []byte(`"\uD83D\uDE01"`)
b.SetBytes(int64(len(j)))
r := bytes.NewReader(j)
dec := json.NewDecoder(r)
var out string
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := dec.Decode(&out); err != nil {
if err != io.EOF {
b.Fatal("Decode:", err)
}
}
r.Seek(0, 0)
}
}
func BenchmarkDecoderStream(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
var buf bytes.Buffer
dec := json.NewDecoder(&buf)
buf.WriteString(`"` + strings.Repeat("x", 1000000) + `"` + "\n\n\n")
var x interface{}
if err := dec.Decode(&x); err != nil {
b.Fatal("Decode:", err)
}
ones := strings.Repeat(" 1\n", 300000) + "\n\n\n"
b.StartTimer()
for i := 0; i < b.N; i++ {
if i%300000 == 0 {
buf.WriteString(ones)
}
x = nil
if err := dec.Decode(&x); err != nil || x != 1.0 {
if err != io.EOF {
b.Fatalf("Decode: %v after %d", err, i)
}
}
}
}
func BenchmarkCodeUnmarshal(b *testing.B) {
b.ReportAllocs()
if codeJSON == nil {
b.StopTimer()
codeInit()
b.StartTimer()
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var r codeResponse
if err := json.Unmarshal(codeJSON, &r); err != nil {
b.Fatal("Unmarshal:", err)
}
}
})
b.SetBytes(int64(len(codeJSON)))
}
func BenchmarkCodeUnmarshalReuse(b *testing.B) {
b.ReportAllocs()
if codeJSON == nil {
b.StopTimer()
codeInit()
b.StartTimer()
}
b.RunParallel(func(pb *testing.PB) {
var r codeResponse
for pb.Next() {
if err := json.Unmarshal(codeJSON, &r); err != nil {
b.Fatal("Unmarshal:", err)
}
}
})
b.SetBytes(int64(len(codeJSON)))
}
func BenchmarkUnmarshalString(b *testing.B) {
b.ReportAllocs()
data := []byte(`"hello, world"`)
b.RunParallel(func(pb *testing.PB) {
var s string
for pb.Next() {
if err := json.Unmarshal(data, &s); err != nil {
b.Fatal("Unmarshal:", err)
}
}
})
}
func BenchmarkUnmarshalFloat64(b *testing.B) {
b.ReportAllocs()
data := []byte(`3.14`)
b.RunParallel(func(pb *testing.PB) {
var f float64
for pb.Next() {
if err := json.Unmarshal(data, &f); err != nil {
b.Fatal("Unmarshal:", err)
}
}
})
}
func BenchmarkUnmarshalInt64(b *testing.B) {
b.ReportAllocs()
data := []byte(`3`)
b.RunParallel(func(pb *testing.PB) {
var x int64
for pb.Next() {
if err := json.Unmarshal(data, &x); err != nil {
b.Fatal("Unmarshal:", err)
}
}
})
}
func BenchmarkIssue10335(b *testing.B) {
b.ReportAllocs()
j := []byte(`{"a":{ }}`)
b.RunParallel(func(pb *testing.PB) {
var s struct{}
for pb.Next() {
if err := json.Unmarshal(j, &s); err != nil {
b.Fatal(err)
}
}
})
}
func BenchmarkUnmapped(b *testing.B) {
b.ReportAllocs()
j := []byte(`{"s": "hello", "y": 2, "o": {"x": 0}, "a": [1, 99, {"x": 1}]}`)
b.RunParallel(func(pb *testing.PB) {
var s struct{}
for pb.Next() {
if err := json.Unmarshal(j, &s); err != nil {
b.Fatal(err)
}
}
})
}
func Benchmark_Compact_EncodingJson(b *testing.B) {
b.ReportAllocs()
if codeJSON == nil {
b.StopTimer()
codeInit()
b.StartTimer()
}
for i := 0; i < b.N; i++ {
var buf bytes.Buffer
if err := stdjson.Compact(&buf, codeJSON); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Compact_GoJson(b *testing.B) {
b.ReportAllocs()
if codeJSON == nil {
b.StopTimer()
codeInit()
b.StartTimer()
}
for i := 0; i < b.N; i++ {
var buf bytes.Buffer
if err := json.Compact(&buf, codeJSON); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Indent_EncodingJson(b *testing.B) {
b.ReportAllocs()
if codeJSON == nil {
b.StopTimer()
codeInit()
b.StartTimer()
}
for i := 0; i < b.N; i++ {
var buf bytes.Buffer
if err := stdjson.Indent(&buf, codeJSON, "-", " "); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Indent_GoJson(b *testing.B) {
b.ReportAllocs()
if codeJSON == nil {
b.StopTimer()
codeInit()
b.StartTimer()
}
for i := 0; i < b.N; i++ {
var buf bytes.Buffer
if err := json.Indent(&buf, codeJSON, "-", " "); err != nil {
b.Fatal(err)
}
}
}

489
benchmarks/decode_test.go Normal file
View file

@ -0,0 +1,489 @@
package benchmark
import (
"bytes"
"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"
fastjson "github.com/valyala/fastjson"
)
func Benchmark_Decode_SmallStruct_Unmarshal_EncodingJson(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
result := SmallPayload{}
if err := json.Unmarshal(SmallFixture, &result); err != nil {
b.Fatal(err)
}
}
}
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)
}
}
}
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)
}
}
}
func Benchmark_Decode_SmallStruct_Unmarshal_JsonIter(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
result := SmallPayload{}
if err := jsoniter.Unmarshal(SmallFixture, &result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_SmallStruct_Unmarshal_GoJay(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
result := SmallPayload{}
if err := gojay.UnmarshalJSONObject(SmallFixture, &result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_SmallStruct_Unmarshal_GoJayUnsafe(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := SmallPayload{}
if err := gojay.Unsafe.UnmarshalJSONObject(SmallFixture, &result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_SmallStruct_Unmarshal_GoJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := SmallPayload{}
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 {
b.Fatal(err)
}
}
}
func Benchmark_Decode_SmallStruct_Stream_EncodingJson(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(SmallFixture)
for i := 0; i < b.N; i++ {
result := SmallPayload{}
reader.Reset(SmallFixture)
if err := json.NewDecoder(reader).Decode(&result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_SmallStruct_Stream_SegmentioJson(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(SmallFixture)
for i := 0; i < b.N; i++ {
result := SmallPayload{}
reader.Reset(SmallFixture)
if err := segmentiojson.NewDecoder(reader).Decode(&result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_SmallStruct_Stream_JsonIter(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(SmallFixture)
for i := 0; i < b.N; i++ {
result := SmallPayload{}
reader.Reset(SmallFixture)
if err := jsoniter.NewDecoder(reader).Decode(&result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_SmallStruct_Stream_GoJay(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(SmallFixture)
for n := 0; n < b.N; n++ {
reader.Reset(SmallFixture)
result := SmallPayload{}
if err := gojay.NewDecoder(reader).DecodeObject(&result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_SmallStruct_Stream_GoJson(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(SmallFixture)
for i := 0; i < b.N; i++ {
result := SmallPayload{}
reader.Reset(SmallFixture)
if err := gojson.NewDecoder(reader).Decode(&result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_MediumStruct_Unmarshal_EncodingJson(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
result := MediumPayload{}
if err := json.Unmarshal(MediumFixture, &result); err != nil {
b.Fatal(err)
}
}
}
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)
}
}
}
func Benchmark_Decode_MediumStruct_Unmarshal_SegmentioJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := MediumPayload{}
if err := segmentiojson.Unmarshal(MediumFixture, &result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_MediumStruct_Unmarshal_JsonIter(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
result := MediumPayload{}
if err := jsoniter.Unmarshal(MediumFixture, &result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_MediumStruct_Unmarshal_GoJay(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
result := MediumPayload{}
if err := gojay.UnmarshalJSONObject(MediumFixture, &result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_MediumStruct_Unmarshal_GoJayUnsafe(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := MediumPayload{}
if err := gojay.Unsafe.UnmarshalJSONObject(MediumFixture, &result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_MediumStruct_Unmarshal_GoJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := MediumPayload{}
if err := gojson.Unmarshal(MediumFixture, &result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_MediumStruct_Unmarshal_GoJsonNoEscape(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := MediumPayload{}
if err := gojson.UnmarshalNoEscape(MediumFixture, &result); err != nil {
b.Fatal(err)
}
}
}
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)
}
}
}
func Benchmark_Decode_MediumStruct_Stream_SegmentioJson(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(MediumFixture)
for n := 0; n < b.N; n++ {
reader.Reset(MediumFixture)
result := MediumPayload{}
if err := segmentiojson.NewDecoder(reader).Decode(&result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_MediumStruct_Stream_JsonIter(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(MediumFixture)
for i := 0; i < b.N; i++ {
result := MediumPayload{}
reader.Reset(MediumFixture)
if err := jsoniter.NewDecoder(reader).Decode(&result); err != nil {
b.Fatal(err)
}
}
}
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{}
if err := gojay.NewDecoder(reader).DecodeObject(&result); err != nil {
b.Fatal(err)
}
}
}
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) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
result := LargePayload{}
if err := json.Unmarshal(LargeFixture, &result); err != nil {
b.Fatal(err)
}
}
}
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)
}
}
}
func Benchmark_Decode_LargeStruct_Unmarshal_SegmentioJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := LargePayload{}
if err := segmentiojson.Unmarshal(LargeFixture, &result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_LargeStruct_Unmarshal_JsonIter(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
result := LargePayload{}
if err := jsoniter.Unmarshal(LargeFixture, &result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_LargeStruct_Unmarshal_GoJay(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
result := LargePayload{}
if err := gojay.UnmarshalJSONObject(LargeFixture, &result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_LargeStruct_Unmarshal_GoJayUnsafe(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := LargePayload{}
if err := gojay.Unsafe.UnmarshalJSONObject(LargeFixture, &result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_LargeStruct_Unmarshal_GoJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := LargePayload{}
if err := gojson.Unmarshal(LargeFixture, &result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_LargeStruct_Unmarshal_GoJsonNoEscape(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := LargePayload{}
if err := gojson.UnmarshalNoEscape(LargeFixture, &result); err != nil {
b.Fatal(err)
}
}
}
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)
}
}
}
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)
}
}
}
func Benchmark_Decode_LargeStruct_Stream_SegmentioJson(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(LargeFixture)
for i := 0; i < b.N; i++ {
result := LargePayload{}
reader.Reset(LargeFixture)
if err := segmentiojson.NewDecoder(reader).Decode(&result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_LargeStruct_Stream_JsonIter(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(LargeFixture)
for i := 0; i < b.N; i++ {
result := LargePayload{}
reader.Reset(LargeFixture)
if err := jsoniter.NewDecoder(reader).Decode(&result); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Decode_LargeStruct_Stream_GoJay(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(LargeFixture)
for n := 0; n < b.N; n++ {
reader.Reset(LargeFixture)
result := LargePayload{}
if err := gojay.NewDecoder(reader).DecodeObject(&result); err != nil {
b.Fatal(err)
}
}
}
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)
}
}
}
func Benchmark_Decode_LargeSlice_EscapedString_GoJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
var v []string
if err := gojson.Unmarshal(LargeSliceEscapedString, &v); err != nil {
b.Fatal(err)
}
}
}

915
benchmarks/encode_test.go Normal file
View file

@ -0,0 +1,915 @@
package benchmark
import (
"bytes"
"context"
"encoding/json"
"testing"
gojay "github.com/francoispqt/gojay"
gojson "github.com/goccy/go-json"
jsoniter "github.com/json-iterator/go"
"github.com/pquerna/ffjson/ffjson"
segmentiojson "github.com/segmentio/encoding/json"
"github.com/wI2L/jettison"
)
func Benchmark_Encode_SmallStruct_EncodingJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(NewSmallPayload()); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_SmallStruct_FFJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := ffjson.Marshal(NewSmallPayloadFFJson()); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_SmallStruct_JsonIter(b *testing.B) {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(NewSmallPayload()); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_SmallStruct_EasyJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := NewSmallPayloadEasyJson().MarshalJSON(); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_SmallStruct_Jettison(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := jettison.Marshal(NewSmallPayload()); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_SmallStruct_GoJay(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojay.MarshalJSONObject(NewSmallPayload()); err != nil {
b.Fatal(err)
}
}
}
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_GoJsonColored(b *testing.B) {
colorOpt := gojson.Colorize(gojson.DefaultColorScheme)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.MarshalWithOption(NewSmallPayload(), colorOpt); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_SmallStruct_GoJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.Marshal(NewSmallPayload()); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_SmallStruct_GoJsonNoEscape(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.MarshalNoEscape(NewSmallPayload()); err != nil {
b.Fatal(err)
}
}
}
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_FFJson(b *testing.B) {
cached := NewSmallPayloadFFJson()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := ffjson.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_EasyJson(b *testing.B) {
cached := NewSmallPayloadEasyJson()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := cached.MarshalJSON(); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_SmallStructCached_Jettison(b *testing.B) {
cached := NewSmallPayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := jettison.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_GoJsonColored(b *testing.B) {
cached := NewSmallPayload()
colorOpt := gojson.Colorize(gojson.DefaultColorScheme)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.MarshalWithOption(cached, colorOpt); 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_SmallStructCached_GoJsonNoEscape(b *testing.B) {
cached := NewSmallPayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.MarshalNoEscape(cached); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MediumStruct_EncodingJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(NewMediumPayload()); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MediumStruct_JsonIter(b *testing.B) {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(NewMediumPayload()); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MediumStruct_EasyJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := NewMediumPayloadEasyJson().MarshalJSON(); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MediumStruct_Jettison(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := jettison.Marshal(NewMediumPayload()); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MediumStruct_GoJay(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojay.MarshalJSONObject(NewMediumPayload()); err != nil {
b.Fatal(err)
}
}
}
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_GoJsonColored(b *testing.B) {
colorOpt := gojson.Colorize(gojson.DefaultColorScheme)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.MarshalWithOption(NewMediumPayload(), colorOpt); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MediumStruct_GoJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.Marshal(NewMediumPayload()); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MediumStruct_GoJsonNoEscape(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.MarshalNoEscape(NewMediumPayload()); err != nil {
b.Fatal(err)
}
}
}
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_EasyJson(b *testing.B) {
cached := NewMediumPayloadEasyJson()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := cached.MarshalJSON(); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MediumStructCached_Jettison(b *testing.B) {
cached := NewMediumPayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := jettison.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_GoJsonColored(b *testing.B) {
cached := NewMediumPayload()
colorOpt := gojson.Colorize(gojson.DefaultColorScheme)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.MarshalWithOption(cached, colorOpt); 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_MediumStructCached_GoJsonNoEscape(b *testing.B) {
cached := NewMediumPayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.MarshalNoEscape(cached); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_LargeStruct_EncodingJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(NewLargePayload()); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_LargeStruct_JsonIter(b *testing.B) {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(NewLargePayload()); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_LargeStruct_EasyJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := NewLargePayloadEasyJson().MarshalJSON(); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_LargeStruct_Jettison(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := jettison.Marshal(NewLargePayload()); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_LargeStruct_GoJay(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojay.MarshalJSONObject(NewLargePayload()); err != nil {
b.Fatal(err)
}
}
}
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_GoJsonColored(b *testing.B) {
colorOpt := gojson.Colorize(gojson.DefaultColorScheme)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.MarshalWithOption(NewLargePayload(), colorOpt); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_LargeStruct_GoJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.Marshal(NewLargePayload()); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_LargeStruct_GoJsonNoEscape(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.MarshalNoEscape(NewLargePayload()); err != nil {
b.Fatal(err)
}
}
}
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_EasyJson(b *testing.B) {
cached := NewLargePayloadEasyJson()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := cached.MarshalJSON(); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_LargeStructCached_Jettison(b *testing.B) {
cached := NewLargePayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := jettison.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_GoJsonColored(b *testing.B) {
cached := NewLargePayload()
colorOpt := gojson.Colorize(gojson.DefaultColorScheme)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.MarshalWithOption(cached, colorOpt); 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)
}
}
}
func Benchmark_Encode_LargeStructCached_GoJsonNoEscape(b *testing.B) {
cached := NewLargePayload()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.MarshalNoEscape(cached); err != nil {
b.Fatal(err)
}
}
}
func benchMapValue() map[string]interface{} {
return map[string]interface{}{
"a": 1,
"b": 2.1,
"c": "hello",
"d": struct {
V int
}{
V: 1,
},
"e": true,
}
}
func Benchmark_Encode_MapInterface_EncodingJson(b *testing.B) {
v := benchMapValue()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(v); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MapInterface_JsonIter(b *testing.B) {
v := benchMapValue()
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_MapInterface_Jettison(b *testing.B) {
v := benchMapValue()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := jettison.Marshal(v); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MapInterface_SegmentioJson(b *testing.B) {
v := benchMapValue()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := segmentiojson.Marshal(v); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MapInterface_GoJson(b *testing.B) {
v := benchMapValue()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.Marshal(v); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_Interface_SegmentioJson(b *testing.B) {
v := []interface{}{1}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := segmentiojson.Marshal(v); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_Interface_GoJson(b *testing.B) {
v := []interface{}{1}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.Marshal(v); err != nil {
b.Fatal(err)
}
}
}
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()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(true); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Marshal_Bool_JsonIter(b *testing.B) {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(true); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Marshal_Bool_Jettison(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := jettison.Marshal(true); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Marshal_Bool_SegmentioJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := segmentiojson.Marshal(true); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Marshal_Bool_GoJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.Marshal(true); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_Int_EncodingJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(1); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_Int_JsonIter(b *testing.B) {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(1); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_Int_Jettison(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := jettison.Marshal(1); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_Int_SegmentioJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := segmentiojson.Marshal(1); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_Int_GoJson(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.Marshal(1); err != nil {
b.Fatal(err)
}
}
}
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)
}
}
}
type queryTestX struct {
XA int
XB string
XC *queryTestY
XD bool
XE float32
}
type queryTestY struct {
YA int
YB string
YC bool
YD float32
}
func Benchmark_Encode_FilterByMap(b *testing.B) {
v := &queryTestX{
XA: 1,
XB: "xb",
XC: &queryTestY{
YA: 2,
YB: "yb",
YC: true,
YD: 4,
},
XD: true,
XE: 5,
}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
filteredMap := map[string]interface{}{
"XA": v.XA,
"XB": v.XB,
"XC": map[string]interface{}{
"YA": v.XC.YA,
"YB": v.XC.YB,
},
}
if _, err := gojson.Marshal(filteredMap); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_FilterByFieldQuery(b *testing.B) {
query, err := gojson.BuildFieldQuery(
"XA",
"XB",
gojson.BuildSubFieldQuery("XC").Fields(
"YA",
"YB",
),
)
if err != nil {
b.Fatal(err)
}
v := &queryTestX{
XA: 1,
XB: "xb",
XC: &queryTestY{
YA: 2,
YB: "yb",
YC: true,
YD: 4,
},
XD: true,
XE: 5,
}
ctx := gojson.SetFieldQueryToContext(context.Background(), query)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.MarshalContext(ctx, v); err != nil {
b.Fatal(err)
}
}
}

22
benchmarks/go.mod Normal file
View file

@ -0,0 +1,22 @@
module benchmark
go 1.19
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.10
github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7
github.com/segmentio/encoding v0.2.4
github.com/valyala/fastjson v1.6.3
github.com/wI2L/jettison v0.7.1
)
require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/stretchr/testify v1.7.0 // indirect
)
replace github.com/goccy/go-json => ../

195
benchmarks/go.sum Normal file
View file

@ -0,0 +1,195 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.31.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.37.0/go.mod h1:TS1dMSSfndXH133OKGwekG838Om/cQT0BUHV3HcBgoo=
dmitri.shuralyov.com/app/changes v0.0.0-20180602232624-0a106ad413e3/go.mod h1:Yl+fi1br7+Rr3LqpNJf1/uxUdtRUV+Tnj0o93V2B9MU=
dmitri.shuralyov.com/html/belt v0.0.0-20180602232347-f7d459c86be0/go.mod h1:JLBrvjyP0v+ecvNYvCpyZgu5/xkfAUhi6wJj28eUfSU=
dmitri.shuralyov.com/service/change v0.0.0-20181023043359-a85b471d5412/go.mod h1:a1inKt/atXimZ4Mv927x+r7UpyzRUf4emIoiiSC2TN4=
dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D6DFvNNtx+9ybjezNCa8XF0xaYcETyp6rHWU=
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g=
github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/coreos/go-systemd v0.0.0-20181012123002-c6f51f82210d/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/francoispqt/gojay v1.2.13 h1:d2m3sFjloqoIUQU3TsHBgj6qg/BVGlTBeHDUmyJnXKk=
github.com/francoispqt/gojay v1.2.13/go.mod h1:ehT5mTG4ua4581f1++1WLG0vPdaA9HaiDsoyrBGkyDY=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY=
github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/grpc-ecosystem/grpc-gateway v1.5.0/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw=
github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0B/fFc00Y+Rasa88328GlI/XbtyysCtTHZS8h7IrBU=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe h1:W/GaMY0y69G4cFlmsC6B9sbuo2fP8OFP1ABjt4kPz+w=
github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00vh2OMYv+xgHpAMF4=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo=
github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7 h1:xoIK0ctDddBMnc74udxJYBqlo9Ylnsp1waqjLsnef20=
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7/go.mod h1:YARuvh7BUWHNhzDq2OM5tzR2RiCcN2D7sapiKyCel/M=
github.com/prometheus/client_golang v0.8.0/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
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.1.10/go.mod h1:RWhr02uzMB9gQC1x+MfYxedtmBibb9cZ6Vv9VxRSSbw=
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=
github.com/shurcooL/github_flavored_markdown v0.0.0-20181002035957-2122de532470/go.mod h1:2dOwnU2uBioM+SGy2aZoq1f/Sd1l9OkAeAUvjSyvgU0=
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
github.com/shurcooL/gofontwoff v0.0.0-20180329035133-29b52fc0a18d/go.mod h1:05UtEgK5zq39gLST6uB0cf3NEHjETfB4Fgr3Gx5R9Vw=
github.com/shurcooL/gopherjslib v0.0.0-20160914041154-feb6d3990c2c/go.mod h1:8d3azKNyqcHP1GaQE/c6dDgjkgSx2BZ4IoEi4F1reUI=
github.com/shurcooL/highlight_diff v0.0.0-20170515013008-09bb4053de1b/go.mod h1:ZpfEhSmds4ytuByIcDnOLkTHGUI6KNqRNPDLHDk+mUU=
github.com/shurcooL/highlight_go v0.0.0-20181028180052-98c3abbbae20/go.mod h1:UDKB5a1T23gOMUJrI+uSuH0VRDStOiUVSjBTRDVBVag=
github.com/shurcooL/home v0.0.0-20181020052607-80b7ffcb30f9/go.mod h1:+rgNQw2P9ARFAs37qieuu7ohDNQ3gds9msbT2yn85sg=
github.com/shurcooL/htmlg v0.0.0-20170918183704-d01228ac9e50/go.mod h1:zPn1wHpTIePGnXSHpsVPWEktKXHr6+SS6x/IKRb7cpw=
github.com/shurcooL/httperror v0.0.0-20170206035902-86b7830d14cc/go.mod h1:aYMfkZ6DWSJPJ6c4Wwz3QtW22G7mf/PEgaB9k/ik5+Y=
github.com/shurcooL/httpfs v0.0.0-20171119174359-809beceb2371/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
github.com/shurcooL/httpgzip v0.0.0-20180522190206-b1c53ac65af9/go.mod h1:919LwcH0M7/W4fcZ0/jy0qGght1GIhqyS/EgWGH2j5Q=
github.com/shurcooL/issues v0.0.0-20181008053335-6292fdc1e191/go.mod h1:e2qWDig5bLteJ4fwvDAc2NHzqFEthkqn7aOZAOpj+PQ=
github.com/shurcooL/issuesapp v0.0.0-20180602232740-048589ce2241/go.mod h1:NPpHK2TI7iSaM0buivtFUc9offApnI0Alt/K8hcHy0I=
github.com/shurcooL/notifications v0.0.0-20181007000457-627ab5aea122/go.mod h1:b5uSkrEVM1jQUspwbixRBhaIjIzL2xazXp6kntxYle0=
github.com/shurcooL/octicon v0.0.0-20181028054416-fa4f57f9efb2/go.mod h1:eWdoE5JD4R5UVWDucdOPg1g2fqQRq78IQa9zlOV1vpQ=
github.com/shurcooL/reactions v0.0.0-20181006231557-f2e0b4ca5b82/go.mod h1:TCR1lToEk4d2s07G3XGfz2QrgHXg4RJBvjrOozvoWfk=
github.com/shurcooL/sanitized_anchor_name v0.0.0-20170918181015-86672fcb3f95/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/shurcooL/users v0.0.0-20180125191416-49c67e49c537/go.mod h1:QJTqeLYEDaXHZDBsXlPCDqdhQuJkuw4NOtaxYe3xii4=
github.com/shurcooL/webdavfs v0.0.0-20170829043945-18c3829fa133/go.mod h1:hKmq5kWdCj2z2KEozexVbfEZIWiTjhE0+UjmZgPqehw=
github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d/go.mod h1:UdhH50NIW0fCiwBSr0co2m7BnFLdv4fQTgdqdJTHFeE=
github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e/go.mod h1:HuIsMU8RRBOtsCgI77wP899iHVBQpCmg4ErYMZB+2IA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
github.com/valyala/fastjson v1.6.3 h1:tAKFnnwmeMGPbwJ7IwxcTPCNr3uIzoIj3/Fh90ra4xc=
github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=
github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
github.com/wI2L/jettison v0.7.1 h1:XNq/WvSOAiJhFww9F5JZZcBZtKFL2Y/9WHHEHLDq9TE=
github.com/wI2L/jettison v0.7.1/go.mod h1:dj49nOP41M7x6Jql62BqqF/+nW+XJgBaWzJR0hd6M84=
go.opencensus.io v0.18.0/go.mod h1:vKdFvxhtzZ9onBp9VKHK8z/sRpBMnKAsufL7wlDrCOA=
go4.org v0.0.0-20180809161055-417644f6feb5/go.mod h1:MkTOUMDaeVYJUOUsaDXIhWPZYa1yOyC1qaOBpL57BhE=
golang.org/x/build v0.0.0-20190111050920-041ab4dc3f9d/go.mod h1:OWs+y06UdEOHN4y+MfF/py+xQ/tYqIWW03b70/CG9Rw=
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181029044818-c44066c5c816/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181106065722-10aee1819953/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190313220215-9f648a60d977/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181017192945-9dcd33a902f4/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/perf v0.0.0-20180704124530-6e6d33e29852/go.mod h1:JLpeXjPJfIyPr5TlbXLkXWLhP8nz10XfvxElABhCtcw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181029174526-d69651ed3497/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190316082340-a2f829d7f35f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181030000716-a0a13e073c7b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
google.golang.org/api v0.0.0-20180910000450-7ca32eb868bf/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
google.golang.org/api v0.0.0-20181030000543-1d582fd0359e/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
google.golang.org/api v0.1.0/go.mod h1:UGEZY7KEX120AnNLIHFMKIo4obdJhkp2tPbaPlQx13Y=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20181029155118-b69ba1387ce2/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20181202183823-bd91e49a0898/go.mod h1:7Ep/1NZk928CDR8SjdVbjWNpdIf6nzjE3BTgJDr2Atg=
google.golang.org/genproto v0.0.0-20190306203927-b5d61aea6440/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
google.golang.org/grpc v1.14.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
google.golang.org/grpc v1.16.0/go.mod h1:0JHn/cJsOMiMfNA9+DeHDlAU7KAAB5GDlYFpa9MZMio=
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
grpc.go4.org v0.0.0-20170609214715-11d0a25b4919/go.mod h1:77eQGdRu53HpSqPFJFmuJdjuHRquDANNeA4x7B8WQ9o=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
sourcegraph.com/sourcegraph/go-diff v0.5.0/go.mod h1:kuch7UrkMzY0X+p9CRK03kfuPQ2zzQcaEFbx8wA8rck=
sourcegraph.com/sqs/pbtypes v0.0.0-20180604144634-d3ebe8f20ae4/go.mod h1:ketZ/q3QxT9HOBeFhu6RdvsftgpsbFHBF5Cas6cDKZ0=

213
benchmarks/large_payload.go Normal file

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,367 @@
// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT.
package benchmark
import (
json "encoding/json"
easyjson "github.com/mailru/easyjson"
jlexer "github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter"
)
// suppress unused package warning
var (
_ *json.RawMessage
_ *jlexer.Lexer
_ *jwriter.Writer
_ easyjson.Marshaler
)
func easyjsonD519278DecodeBenchmark(in *jlexer.Lexer, out *LargePayloadEasyJson) {
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
in.Consumed()
}
in.Skip()
return
}
in.Delim('{')
for !in.IsDelim('}') {
key := in.UnsafeString()
in.WantColon()
if in.IsNull() {
in.Skip()
in.WantComma()
continue
}
switch key {
case "Users":
if in.IsNull() {
in.Skip()
out.Users = nil
} else {
in.Delim('[')
if out.Users == nil {
if !in.IsDelim(']') {
out.Users = make(DSUsers, 0, 8)
} else {
out.Users = DSUsers{}
}
} else {
out.Users = (out.Users)[:0]
}
for !in.IsDelim(']') {
var v1 *DSUser
if in.IsNull() {
in.Skip()
v1 = nil
} else {
if v1 == nil {
v1 = new(DSUser)
}
easyjsonD519278DecodeBenchmark1(in, v1)
}
out.Users = append(out.Users, v1)
in.WantComma()
}
in.Delim(']')
}
case "Topics":
if in.IsNull() {
in.Skip()
out.Topics = nil
} else {
if out.Topics == nil {
out.Topics = new(DSTopicsList)
}
easyjsonD519278DecodeBenchmark2(in, out.Topics)
}
default:
in.SkipRecursive()
}
in.WantComma()
}
in.Delim('}')
if isTopLevel {
in.Consumed()
}
}
func easyjsonD519278EncodeBenchmark(out *jwriter.Writer, in LargePayloadEasyJson) {
out.RawByte('{')
first := true
_ = first
{
const prefix string = ",\"Users\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
if in.Users == nil && (out.Flags&jwriter.NilSliceAsEmpty) == 0 {
out.RawString("null")
} else {
out.RawByte('[')
for v2, v3 := range in.Users {
if v2 > 0 {
out.RawByte(',')
}
if v3 == nil {
out.RawString("null")
} else {
easyjsonD519278EncodeBenchmark1(out, *v3)
}
}
out.RawByte(']')
}
}
{
const prefix string = ",\"Topics\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
if in.Topics == nil {
out.RawString("null")
} else {
easyjsonD519278EncodeBenchmark2(out, *in.Topics)
}
}
out.RawByte('}')
}
// MarshalJSON supports json.Marshaler interface
func (v LargePayloadEasyJson) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
easyjsonD519278EncodeBenchmark(&w, v)
return w.Buffer.BuildBytes(), w.Error
}
// MarshalEasyJSON supports easyjson.Marshaler interface
func (v LargePayloadEasyJson) MarshalEasyJSON(w *jwriter.Writer) {
easyjsonD519278EncodeBenchmark(w, v)
}
// UnmarshalJSON supports json.Unmarshaler interface
func (v *LargePayloadEasyJson) UnmarshalJSON(data []byte) error {
r := jlexer.Lexer{Data: data}
easyjsonD519278DecodeBenchmark(&r, v)
return r.Error()
}
// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
func (v *LargePayloadEasyJson) UnmarshalEasyJSON(l *jlexer.Lexer) {
easyjsonD519278DecodeBenchmark(l, v)
}
func easyjsonD519278DecodeBenchmark2(in *jlexer.Lexer, out *DSTopicsList) {
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
in.Consumed()
}
in.Skip()
return
}
in.Delim('{')
for !in.IsDelim('}') {
key := in.UnsafeString()
in.WantColon()
if in.IsNull() {
in.Skip()
in.WantComma()
continue
}
switch key {
case "Topics":
if in.IsNull() {
in.Skip()
out.Topics = nil
} else {
in.Delim('[')
if out.Topics == nil {
if !in.IsDelim(']') {
out.Topics = make(DSTopics, 0, 8)
} else {
out.Topics = DSTopics{}
}
} else {
out.Topics = (out.Topics)[:0]
}
for !in.IsDelim(']') {
var v4 *DSTopic
if in.IsNull() {
in.Skip()
v4 = nil
} else {
if v4 == nil {
v4 = new(DSTopic)
}
easyjsonD519278DecodeBenchmark3(in, v4)
}
out.Topics = append(out.Topics, v4)
in.WantComma()
}
in.Delim(']')
}
case "MoreTopicsUrl":
out.MoreTopicsUrl = string(in.String())
default:
in.SkipRecursive()
}
in.WantComma()
}
in.Delim('}')
if isTopLevel {
in.Consumed()
}
}
func easyjsonD519278EncodeBenchmark2(out *jwriter.Writer, in DSTopicsList) {
out.RawByte('{')
first := true
_ = first
{
const prefix string = ",\"Topics\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
if in.Topics == nil && (out.Flags&jwriter.NilSliceAsEmpty) == 0 {
out.RawString("null")
} else {
out.RawByte('[')
for v5, v6 := range in.Topics {
if v5 > 0 {
out.RawByte(',')
}
if v6 == nil {
out.RawString("null")
} else {
easyjsonD519278EncodeBenchmark3(out, *v6)
}
}
out.RawByte(']')
}
}
{
const prefix string = ",\"MoreTopicsUrl\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.MoreTopicsUrl))
}
out.RawByte('}')
}
func easyjsonD519278DecodeBenchmark3(in *jlexer.Lexer, out *DSTopic) {
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
in.Consumed()
}
in.Skip()
return
}
in.Delim('{')
for !in.IsDelim('}') {
key := in.UnsafeString()
in.WantColon()
if in.IsNull() {
in.Skip()
in.WantComma()
continue
}
switch key {
case "Id":
out.Id = int(in.Int())
case "Slug":
out.Slug = string(in.String())
default:
in.SkipRecursive()
}
in.WantComma()
}
in.Delim('}')
if isTopLevel {
in.Consumed()
}
}
func easyjsonD519278EncodeBenchmark3(out *jwriter.Writer, in DSTopic) {
out.RawByte('{')
first := true
_ = first
{
const prefix string = ",\"Id\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.Id))
}
{
const prefix string = ",\"Slug\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.Slug))
}
out.RawByte('}')
}
func easyjsonD519278DecodeBenchmark1(in *jlexer.Lexer, out *DSUser) {
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
in.Consumed()
}
in.Skip()
return
}
in.Delim('{')
for !in.IsDelim('}') {
key := in.UnsafeString()
in.WantColon()
if in.IsNull() {
in.Skip()
in.WantComma()
continue
}
switch key {
case "Username":
out.Username = string(in.String())
default:
in.SkipRecursive()
}
in.WantComma()
}
in.Delim('}')
if isTopLevel {
in.Consumed()
}
}
func easyjsonD519278EncodeBenchmark1(out *jwriter.Writer, in DSUser) {
out.RawByte('{')
first := true
_ = first
{
const prefix string = ",\"Username\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.Username))
}
out.RawByte('}')
}

View file

@ -0,0 +1,363 @@
package benchmark
import "github.com/francoispqt/gojay"
// Response from Clearbit API. Size: 2.4kb
var MediumFixture = []byte(`{
"person": {
"id": "d50887ca-a6ce-4e59-b89f-14f0b5d03b03",
"name": {
"fullName": "Leonid Bugaev",
"givenName": "Leonid",
"familyName": "Bugaev"
},
"email": "leonsbox@gmail.com",
"gender": "male",
"location": "Saint Petersburg, Saint Petersburg, RU",
"geo": {
"city": "Saint Petersburg",
"state": "Saint Petersburg",
"country": "Russia",
"lat": 59.9342802,
"lng": 30.3350986
},
"bio": "Senior engineer at Granify.com",
"site": "http://flickfaver.com",
"avatar": "https://d1ts43dypk8bqh.cloudfront.net/v1/avatars/d50887ca-a6ce-4e59-b89f-14f0b5d03b03",
"employment": {
"name": "www.latera.ru",
"title": "Software Engineer",
"domain": "gmail.com"
},
"facebook": {
"handle": "leonid.bugaev"
},
"github": {
"handle": "buger",
"id": 14009,
"avatar": "https://avatars.githubusercontent.com/u/14009?v=3",
"company": "Granify",
"blog": "http://leonsbox.com",
"followers": 95,
"following": 10
},
"twitter": {
"handle": "flickfaver",
"id": 77004410,
"bio": null,
"followers": 2,
"following": 1,
"statuses": 5,
"favorites": 0,
"location": "",
"site": "http://flickfaver.com",
"avatar": null
},
"linkedin": {
"handle": "in/leonidbugaev"
},
"googleplus": {
"handle": null
},
"angellist": {
"handle": "leonid-bugaev",
"id": 61541,
"bio": "Senior engineer at Granify.com",
"blog": "http://buger.github.com",
"site": "http://buger.github.com",
"followers": 41,
"avatar": "https://d1qb2nb5cznatu.cloudfront.net/users/61541-medium_jpg?1405474390"
},
"klout": {
"handle": null,
"score": null
},
"foursquare": {
"handle": null
},
"aboutme": {
"handle": "leonid.bugaev",
"bio": null,
"avatar": null
},
"gravatar": {
"handle": "buger",
"urls": [
],
"avatar": "http://1.gravatar.com/avatar/f7c8edd577d13b8930d5522f28123510",
"avatars": [
{
"url": "http://1.gravatar.com/avatar/f7c8edd577d13b8930d5522f28123510",
"type": "thumbnail"
}
]
},
"fuzzy": false
},
"company": null
}`)
type CBAvatar struct {
Url string
}
func (m *CBAvatar) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
switch key {
case "avatars":
return dec.AddString(&m.Url)
}
return nil
}
func (m *CBAvatar) NKeys() int {
return 1
}
func (m *CBAvatar) MarshalJSONObject(enc *gojay.Encoder) {
enc.AddStringKey("url", m.Url)
}
func (m *CBAvatar) IsNil() bool {
return m == nil
}
type Avatars []*CBAvatar
func (t *Avatars) UnmarshalJSONArray(dec *gojay.Decoder) error {
avatar := CBAvatar{}
*t = append(*t, &avatar)
return dec.AddObject(&avatar)
}
func (m *Avatars) MarshalJSONArray(enc *gojay.Encoder) {
for _, e := range *m {
enc.AddObject(e)
}
}
func (m *Avatars) IsNil() bool {
return m == nil
}
type CBGravatar struct {
Avatars Avatars
}
func (m *CBGravatar) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
switch key {
case "avatars":
return dec.AddArray(&m.Avatars)
}
return nil
}
func (m *CBGravatar) NKeys() int {
return 1
}
func (m *CBGravatar) MarshalJSONObject(enc *gojay.Encoder) {
enc.AddArrayKey("avatars", &m.Avatars)
}
func (m *CBGravatar) IsNil() bool {
return m == nil
}
type CBGithub struct {
Followers int
}
func (m *CBGithub) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
switch key {
case "followers":
return dec.AddInt(&m.Followers)
}
return nil
}
func (m *CBGithub) NKeys() int {
return 1
}
func (m *CBGithub) MarshalJSONObject(enc *gojay.Encoder) {
enc.AddIntKey("followers", m.Followers)
}
func (m *CBGithub) IsNil() bool {
return m == nil
}
type CBName struct {
FullName string `json:"fullName"`
}
func (m *CBName) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
switch key {
case "fullName":
return dec.AddString(&m.FullName)
}
return nil
}
func (m *CBName) NKeys() int {
return 1
}
func (m *CBName) MarshalJSONObject(enc *gojay.Encoder) {
enc.AddStringKey("fullName", m.FullName)
}
func (m *CBName) IsNil() bool {
return m == nil
}
type CBPerson struct {
Name *CBName `json:"name"`
Github *CBGithub `json:"github"`
Gravatar *CBGravatar
}
func (m *CBPerson) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
switch key {
case "name":
m.Name = &CBName{}
return dec.AddObject(m.Name)
case "github":
m.Github = &CBGithub{}
return dec.AddObject(m.Github)
case "gravatar":
m.Gravatar = &CBGravatar{}
return dec.AddObject(m.Gravatar)
}
return nil
}
func (m *CBPerson) NKeys() int {
return 3
}
func (m *CBPerson) MarshalJSONObject(enc *gojay.Encoder) {
enc.AddObjectKey("name", m.Name)
enc.AddObjectKey("github", m.Github)
enc.AddObjectKey("gravatar", m.Gravatar)
}
func (m *CBPerson) IsNil() bool {
return m == nil
}
type MediumPayload struct {
Person *CBPerson `json:"person"`
Company string `json:"company"`
}
//easyjson:json
type MediumPayloadEasyJson struct {
Person *CBPerson `json:"person"`
Company string `json:"company"`
}
func (m *MediumPayload) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
switch key {
case "person":
m.Person = &CBPerson{}
return dec.AddObject(m.Person)
case "company":
dec.AddString(&m.Company)
}
return nil
}
func (m *MediumPayload) NKeys() int {
return 2
}
func (m *MediumPayload) MarshalJSONObject(enc *gojay.Encoder) {
enc.AddObjectKey("person", m.Person)
// enc.AddStringKey("company", m.Company)
}
func (m *MediumPayload) IsNil() bool {
return m == nil
}
func NewMediumPayload() *MediumPayload {
return &MediumPayload{
Company: "test",
Person: &CBPerson{
Name: &CBName{
FullName: "test",
},
Github: &CBGithub{
Followers: 100,
},
Gravatar: &CBGravatar{
Avatars: Avatars{
&CBAvatar{
Url: "http://test.com",
},
&CBAvatar{
Url: "http://test.com",
},
&CBAvatar{
Url: "http://test.com",
},
&CBAvatar{
Url: "http://test.com",
},
&CBAvatar{
Url: "http://test.com",
},
&CBAvatar{
Url: "http://test.com",
},
&CBAvatar{
Url: "http://test.com",
},
&CBAvatar{
Url: "http://test.com",
},
},
},
},
}
}
func NewMediumPayloadEasyJson() *MediumPayloadEasyJson {
return &MediumPayloadEasyJson{
Company: "test",
Person: &CBPerson{
Name: &CBName{
FullName: "test",
},
Github: &CBGithub{
Followers: 100,
},
Gravatar: &CBGravatar{
Avatars: Avatars{
&CBAvatar{
Url: "http://test.com",
},
&CBAvatar{
Url: "http://test.com",
},
&CBAvatar{
Url: "http://test.com",
},
&CBAvatar{
Url: "http://test.com",
},
&CBAvatar{
Url: "http://test.com",
},
&CBAvatar{
Url: "http://test.com",
},
&CBAvatar{
Url: "http://test.com",
},
&CBAvatar{
Url: "http://test.com",
},
},
},
},
}
}

View file

@ -0,0 +1,453 @@
// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT.
package benchmark
import (
json "encoding/json"
easyjson "github.com/mailru/easyjson"
jlexer "github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter"
)
// suppress unused package warning
var (
_ *json.RawMessage
_ *jlexer.Lexer
_ *jwriter.Writer
_ easyjson.Marshaler
)
func easyjson8ca7813eDecodeBenchmark(in *jlexer.Lexer, out *MediumPayloadEasyJson) {
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
in.Consumed()
}
in.Skip()
return
}
in.Delim('{')
for !in.IsDelim('}') {
key := in.UnsafeString()
in.WantColon()
if in.IsNull() {
in.Skip()
in.WantComma()
continue
}
switch key {
case "person":
if in.IsNull() {
in.Skip()
out.Person = nil
} else {
if out.Person == nil {
out.Person = new(CBPerson)
}
easyjson8ca7813eDecodeBenchmark1(in, out.Person)
}
case "company":
out.Company = string(in.String())
default:
in.SkipRecursive()
}
in.WantComma()
}
in.Delim('}')
if isTopLevel {
in.Consumed()
}
}
func easyjson8ca7813eEncodeBenchmark(out *jwriter.Writer, in MediumPayloadEasyJson) {
out.RawByte('{')
first := true
_ = first
{
const prefix string = ",\"person\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
if in.Person == nil {
out.RawString("null")
} else {
easyjson8ca7813eEncodeBenchmark1(out, *in.Person)
}
}
{
const prefix string = ",\"company\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.Company))
}
out.RawByte('}')
}
// MarshalJSON supports json.Marshaler interface
func (v MediumPayloadEasyJson) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
easyjson8ca7813eEncodeBenchmark(&w, v)
return w.Buffer.BuildBytes(), w.Error
}
// MarshalEasyJSON supports easyjson.Marshaler interface
func (v MediumPayloadEasyJson) MarshalEasyJSON(w *jwriter.Writer) {
easyjson8ca7813eEncodeBenchmark(w, v)
}
// UnmarshalJSON supports json.Unmarshaler interface
func (v *MediumPayloadEasyJson) UnmarshalJSON(data []byte) error {
r := jlexer.Lexer{Data: data}
easyjson8ca7813eDecodeBenchmark(&r, v)
return r.Error()
}
// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
func (v *MediumPayloadEasyJson) UnmarshalEasyJSON(l *jlexer.Lexer) {
easyjson8ca7813eDecodeBenchmark(l, v)
}
func easyjson8ca7813eDecodeBenchmark1(in *jlexer.Lexer, out *CBPerson) {
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
in.Consumed()
}
in.Skip()
return
}
in.Delim('{')
for !in.IsDelim('}') {
key := in.UnsafeString()
in.WantColon()
if in.IsNull() {
in.Skip()
in.WantComma()
continue
}
switch key {
case "name":
if in.IsNull() {
in.Skip()
out.Name = nil
} else {
if out.Name == nil {
out.Name = new(CBName)
}
easyjson8ca7813eDecodeBenchmark2(in, out.Name)
}
case "github":
if in.IsNull() {
in.Skip()
out.Github = nil
} else {
if out.Github == nil {
out.Github = new(CBGithub)
}
easyjson8ca7813eDecodeBenchmark3(in, out.Github)
}
case "Gravatar":
if in.IsNull() {
in.Skip()
out.Gravatar = nil
} else {
if out.Gravatar == nil {
out.Gravatar = new(CBGravatar)
}
easyjson8ca7813eDecodeBenchmark4(in, out.Gravatar)
}
default:
in.SkipRecursive()
}
in.WantComma()
}
in.Delim('}')
if isTopLevel {
in.Consumed()
}
}
func easyjson8ca7813eEncodeBenchmark1(out *jwriter.Writer, in CBPerson) {
out.RawByte('{')
first := true
_ = first
{
const prefix string = ",\"name\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
if in.Name == nil {
out.RawString("null")
} else {
easyjson8ca7813eEncodeBenchmark2(out, *in.Name)
}
}
{
const prefix string = ",\"github\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
if in.Github == nil {
out.RawString("null")
} else {
easyjson8ca7813eEncodeBenchmark3(out, *in.Github)
}
}
{
const prefix string = ",\"Gravatar\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
if in.Gravatar == nil {
out.RawString("null")
} else {
easyjson8ca7813eEncodeBenchmark4(out, *in.Gravatar)
}
}
out.RawByte('}')
}
func easyjson8ca7813eDecodeBenchmark4(in *jlexer.Lexer, out *CBGravatar) {
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
in.Consumed()
}
in.Skip()
return
}
in.Delim('{')
for !in.IsDelim('}') {
key := in.UnsafeString()
in.WantColon()
if in.IsNull() {
in.Skip()
in.WantComma()
continue
}
switch key {
case "Avatars":
if in.IsNull() {
in.Skip()
out.Avatars = nil
} else {
in.Delim('[')
if out.Avatars == nil {
if !in.IsDelim(']') {
out.Avatars = make(Avatars, 0, 8)
} else {
out.Avatars = Avatars{}
}
} else {
out.Avatars = (out.Avatars)[:0]
}
for !in.IsDelim(']') {
var v1 *CBAvatar
if in.IsNull() {
in.Skip()
v1 = nil
} else {
if v1 == nil {
v1 = new(CBAvatar)
}
easyjson8ca7813eDecodeBenchmark5(in, v1)
}
out.Avatars = append(out.Avatars, v1)
in.WantComma()
}
in.Delim(']')
}
default:
in.SkipRecursive()
}
in.WantComma()
}
in.Delim('}')
if isTopLevel {
in.Consumed()
}
}
func easyjson8ca7813eEncodeBenchmark4(out *jwriter.Writer, in CBGravatar) {
out.RawByte('{')
first := true
_ = first
{
const prefix string = ",\"Avatars\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
if in.Avatars == nil && (out.Flags&jwriter.NilSliceAsEmpty) == 0 {
out.RawString("null")
} else {
out.RawByte('[')
for v2, v3 := range in.Avatars {
if v2 > 0 {
out.RawByte(',')
}
if v3 == nil {
out.RawString("null")
} else {
easyjson8ca7813eEncodeBenchmark5(out, *v3)
}
}
out.RawByte(']')
}
}
out.RawByte('}')
}
func easyjson8ca7813eDecodeBenchmark5(in *jlexer.Lexer, out *CBAvatar) {
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
in.Consumed()
}
in.Skip()
return
}
in.Delim('{')
for !in.IsDelim('}') {
key := in.UnsafeString()
in.WantColon()
if in.IsNull() {
in.Skip()
in.WantComma()
continue
}
switch key {
case "Url":
out.Url = string(in.String())
default:
in.SkipRecursive()
}
in.WantComma()
}
in.Delim('}')
if isTopLevel {
in.Consumed()
}
}
func easyjson8ca7813eEncodeBenchmark5(out *jwriter.Writer, in CBAvatar) {
out.RawByte('{')
first := true
_ = first
{
const prefix string = ",\"Url\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.Url))
}
out.RawByte('}')
}
func easyjson8ca7813eDecodeBenchmark3(in *jlexer.Lexer, out *CBGithub) {
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
in.Consumed()
}
in.Skip()
return
}
in.Delim('{')
for !in.IsDelim('}') {
key := in.UnsafeString()
in.WantColon()
if in.IsNull() {
in.Skip()
in.WantComma()
continue
}
switch key {
case "Followers":
out.Followers = int(in.Int())
default:
in.SkipRecursive()
}
in.WantComma()
}
in.Delim('}')
if isTopLevel {
in.Consumed()
}
}
func easyjson8ca7813eEncodeBenchmark3(out *jwriter.Writer, in CBGithub) {
out.RawByte('{')
first := true
_ = first
{
const prefix string = ",\"Followers\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.Followers))
}
out.RawByte('}')
}
func easyjson8ca7813eDecodeBenchmark2(in *jlexer.Lexer, out *CBName) {
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
in.Consumed()
}
in.Skip()
return
}
in.Delim('{')
for !in.IsDelim('}') {
key := in.UnsafeString()
in.WantColon()
if in.IsNull() {
in.Skip()
in.WantComma()
continue
}
switch key {
case "fullName":
out.FullName = string(in.String())
default:
in.SkipRecursive()
}
in.WantComma()
}
in.Delim('}')
if isTopLevel {
in.Consumed()
}
}
func easyjson8ca7813eEncodeBenchmark2(out *jwriter.Writer, in CBName) {
out.RawByte('{')
first := true
_ = first
{
const prefix string = ",\"fullName\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.FullName))
}
out.RawByte('}')
}

24
benchmarks/path_test.go Normal file
View file

@ -0,0 +1,24 @@
package benchmark
import (
"testing"
gojson "github.com/goccy/go-json"
)
func Benchmark_Decode_SmallStruct_UnmarshalPath_GoJson(b *testing.B) {
path, err := gojson.CreatePath("$.st")
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
var v int
if err := path.Unmarshal(SmallFixture, &v); err != nil {
b.Fatal(err)
}
if v != 1 {
b.Fatal("failed to unmarshal path")
}
}
}

View file

@ -0,0 +1,78 @@
package benchmark
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"testing"
gojson "github.com/goccy/go-json"
)
// Benchmark decoding from a slow io.Reader that never fills the buffer completely
func Benchmark_Decode_SlowReader_EncodingJson(b *testing.B) {
var expected LargePayload
if err := json.Unmarshal(LargeFixture, &expected); err != nil {
b.Fatal(err)
}
for _, chunkSize := range [5]int{16384, 4096, 1024, 256, 64} {
b.Run(fmt.Sprintf("chunksize %v", chunkSize), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
index = 0
var got LargePayload
if err := json.NewDecoder(slowReader{chunkSize: chunkSize}).Decode(&got); err != nil {
b.Fatal(err)
}
if !reflect.DeepEqual(expected, got) {
b.Fatalf("failed to decode. expected:[%+v] but got:[%+v]", expected, got)
}
}
})
}
}
func Benchmark_Decode_SlowReader_GoJson(b *testing.B) {
var expected LargePayload
if err := json.Unmarshal(LargeFixture, &expected); err != nil {
b.Fatal(err)
}
for _, chunkSize := range []int{16384, 4096, 1024, 256, 64} {
b.Run(fmt.Sprintf("chunksize %v", chunkSize), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
index = 0
var got LargePayload
if err := gojson.NewDecoder(slowReader{chunkSize: chunkSize}).Decode(&got); err != nil {
b.Fatal(err)
}
if !reflect.DeepEqual(expected, got) {
b.Fatalf("failed to decode. expected:[%+v] but got:[%+v]", expected, got)
}
}
})
}
}
type slowReader struct {
chunkSize int
}
var index int
func (s slowReader) Read(p []byte) (n int, err error) {
smallBuf := make([]byte, Min(s.chunkSize, len(p)))
x := bytes.NewReader(LargeFixture)
n, err = x.ReadAt(smallBuf, int64(index))
index += n
copy(p, smallBuf)
return
}
func Min(x, y int) int {
if x < y {
return x
}
return y
}

129
benchmarks/small_payload.go Normal file
View file

@ -0,0 +1,129 @@
package benchmark
import "github.com/francoispqt/gojay"
var SmallFixture = []byte(`{"st": 1,"sid": 486,"tt": "active","gr": 0,"uuid": "de305d54-75b4-431b-adb2-eb6b9e546014","ip": "127.0.0.1","ua": "user_agent","tz": -6,"v": 1}`)
// ffjson:skip
type SmallPayload struct {
St int
Sid int
Tt string
Gr int
Uuid string
Ip string
Ua string
Tz int
V int
}
type SmallPayloadFFJson struct {
St int
Sid int
Tt string
Gr int
Uuid string
Ip string
Ua string
Tz int
V int
}
//easyjson:json
type SmallPayloadEasyJson struct {
St int
Sid int
Tt string
Gr int
Uuid string
Ip string
Ua string
Tz int
V int
}
func (t *SmallPayload) MarshalJSONObject(enc *gojay.Encoder) {
enc.AddIntKey("st", t.St)
enc.AddIntKey("sid", t.Sid)
enc.AddStringKey("tt", t.Tt)
enc.AddIntKey("gr", t.Gr)
enc.AddStringKey("uuid", t.Uuid)
enc.AddStringKey("ip", t.Ip)
enc.AddStringKey("ua", t.Ua)
enc.AddIntKey("tz", t.Tz)
enc.AddIntKey("v", t.V)
}
func (t *SmallPayload) IsNil() bool {
return t == nil
}
func (t *SmallPayload) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
switch key {
case "st":
return dec.AddInt(&t.St)
case "sid":
return dec.AddInt(&t.Sid)
case "gr":
return dec.AddInt(&t.Gr)
case "tz":
return dec.AddInt(&t.Tz)
case "v":
return dec.AddInt(&t.V)
case "tt":
return dec.AddString(&t.Tt)
case "uuid":
return dec.AddString(&t.Uuid)
case "ip":
return dec.AddString(&t.Ip)
case "ua":
return dec.AddString(&t.Ua)
}
return nil
}
func (t *SmallPayload) NKeys() int {
return 9
}
func NewSmallPayload() *SmallPayload {
return &SmallPayload{
St: 1,
Sid: 2,
Tt: "TestString",
Gr: 4,
Uuid: "8f9a65eb-4807-4d57-b6e0-bda5d62f1429",
Ip: "127.0.0.1",
Ua: "Mozilla",
Tz: 8,
V: 6,
}
}
func NewSmallPayloadEasyJson() *SmallPayloadEasyJson {
return &SmallPayloadEasyJson{
St: 1,
Sid: 2,
Tt: "TestString",
Gr: 4,
Uuid: "8f9a65eb-4807-4d57-b6e0-bda5d62f1429",
Ip: "127.0.0.1",
Ua: "Mozilla",
Tz: 8,
V: 6,
}
}
func NewSmallPayloadFFJson() *SmallPayloadFFJson {
return &SmallPayloadFFJson{
St: 1,
Sid: 2,
Tt: "TestString",
Gr: 4,
Uuid: "8f9a65eb-4807-4d57-b6e0-bda5d62f1429",
Ip: "127.0.0.1",
Ua: "Mozilla",
Tz: 8,
V: 6,
}
}

View file

@ -0,0 +1,186 @@
// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT.
package benchmark
import (
json "encoding/json"
easyjson "github.com/mailru/easyjson"
jlexer "github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter"
)
// suppress unused package warning
var (
_ *json.RawMessage
_ *jlexer.Lexer
_ *jwriter.Writer
_ easyjson.Marshaler
)
func easyjson21677a1cDecodeBenchmark(in *jlexer.Lexer, out *SmallPayloadEasyJson) {
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
in.Consumed()
}
in.Skip()
return
}
in.Delim('{')
for !in.IsDelim('}') {
key := in.UnsafeString()
in.WantColon()
if in.IsNull() {
in.Skip()
in.WantComma()
continue
}
switch key {
case "St":
out.St = int(in.Int())
case "Sid":
out.Sid = int(in.Int())
case "Tt":
out.Tt = string(in.String())
case "Gr":
out.Gr = int(in.Int())
case "Uuid":
out.Uuid = string(in.String())
case "Ip":
out.Ip = string(in.String())
case "Ua":
out.Ua = string(in.String())
case "Tz":
out.Tz = int(in.Int())
case "V":
out.V = int(in.Int())
default:
in.SkipRecursive()
}
in.WantComma()
}
in.Delim('}')
if isTopLevel {
in.Consumed()
}
}
func easyjson21677a1cEncodeBenchmark(out *jwriter.Writer, in SmallPayloadEasyJson) {
out.RawByte('{')
first := true
_ = first
{
const prefix string = ",\"St\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.St))
}
{
const prefix string = ",\"Sid\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.Sid))
}
{
const prefix string = ",\"Tt\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.Tt))
}
{
const prefix string = ",\"Gr\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.Gr))
}
{
const prefix string = ",\"Uuid\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.Uuid))
}
{
const prefix string = ",\"Ip\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.Ip))
}
{
const prefix string = ",\"Ua\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.Ua))
}
{
const prefix string = ",\"Tz\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.Tz))
}
{
const prefix string = ",\"V\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.V))
}
out.RawByte('}')
}
// MarshalJSON supports json.Marshaler interface
func (v SmallPayloadEasyJson) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
easyjson21677a1cEncodeBenchmark(&w, v)
return w.Buffer.BuildBytes(), w.Error
}
// MarshalEasyJSON supports easyjson.Marshaler interface
func (v SmallPayloadEasyJson) MarshalEasyJSON(w *jwriter.Writer) {
easyjson21677a1cEncodeBenchmark(w, v)
}
// UnmarshalJSON supports json.Unmarshaler interface
func (v *SmallPayloadEasyJson) UnmarshalJSON(data []byte) error {
r := jlexer.Lexer{Data: data}
easyjson21677a1cDecodeBenchmark(&r, v)
return r.Error()
}
// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
func (v *SmallPayloadEasyJson) UnmarshalEasyJSON(l *jlexer.Lexer) {
easyjson21677a1cDecodeBenchmark(l, v)
}

View file

@ -0,0 +1,608 @@
// Code generated by ffjson <https://github.com/pquerna/ffjson>. DO NOT EDIT.
// source: small_payload.go
package benchmark
import (
"bytes"
"fmt"
fflib "github.com/pquerna/ffjson/fflib/v1"
)
// MarshalJSON marshal bytes to json - template
func (j *SmallPayloadFFJson) MarshalJSON() ([]byte, error) {
var buf fflib.Buffer
if j == nil {
buf.WriteString("null")
return buf.Bytes(), nil
}
err := j.MarshalJSONBuf(&buf)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// MarshalJSONBuf marshal buff to json - template
func (j *SmallPayloadFFJson) MarshalJSONBuf(buf fflib.EncodingBuffer) error {
if j == nil {
buf.WriteString("null")
return nil
}
var err error
var obj []byte
_ = obj
_ = err
buf.WriteString(`{"St":`)
fflib.FormatBits2(buf, uint64(j.St), 10, j.St < 0)
buf.WriteString(`,"Sid":`)
fflib.FormatBits2(buf, uint64(j.Sid), 10, j.Sid < 0)
buf.WriteString(`,"Tt":`)
fflib.WriteJsonString(buf, string(j.Tt))
buf.WriteString(`,"Gr":`)
fflib.FormatBits2(buf, uint64(j.Gr), 10, j.Gr < 0)
buf.WriteString(`,"Uuid":`)
fflib.WriteJsonString(buf, string(j.Uuid))
buf.WriteString(`,"Ip":`)
fflib.WriteJsonString(buf, string(j.Ip))
buf.WriteString(`,"Ua":`)
fflib.WriteJsonString(buf, string(j.Ua))
buf.WriteString(`,"Tz":`)
fflib.FormatBits2(buf, uint64(j.Tz), 10, j.Tz < 0)
buf.WriteString(`,"V":`)
fflib.FormatBits2(buf, uint64(j.V), 10, j.V < 0)
buf.WriteByte('}')
return nil
}
const (
ffjtSmallPayloadFFJsonbase = iota
ffjtSmallPayloadFFJsonnosuchkey
ffjtSmallPayloadFFJsonSt
ffjtSmallPayloadFFJsonSid
ffjtSmallPayloadFFJsonTt
ffjtSmallPayloadFFJsonGr
ffjtSmallPayloadFFJsonUuid
ffjtSmallPayloadFFJsonIp
ffjtSmallPayloadFFJsonUa
ffjtSmallPayloadFFJsonTz
ffjtSmallPayloadFFJsonV
)
var ffjKeySmallPayloadFFJsonSt = []byte("St")
var ffjKeySmallPayloadFFJsonSid = []byte("Sid")
var ffjKeySmallPayloadFFJsonTt = []byte("Tt")
var ffjKeySmallPayloadFFJsonGr = []byte("Gr")
var ffjKeySmallPayloadFFJsonUuid = []byte("Uuid")
var ffjKeySmallPayloadFFJsonIp = []byte("Ip")
var ffjKeySmallPayloadFFJsonUa = []byte("Ua")
var ffjKeySmallPayloadFFJsonTz = []byte("Tz")
var ffjKeySmallPayloadFFJsonV = []byte("V")
// UnmarshalJSON umarshall json - template of ffjson
func (j *SmallPayloadFFJson) UnmarshalJSON(input []byte) error {
fs := fflib.NewFFLexer(input)
return j.UnmarshalJSONFFLexer(fs, fflib.FFParse_map_start)
}
// UnmarshalJSONFFLexer fast json unmarshall - template ffjson
func (j *SmallPayloadFFJson) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error {
var err error
currentKey := ffjtSmallPayloadFFJsonbase
_ = currentKey
tok := fflib.FFTok_init
wantedTok := fflib.FFTok_init
mainparse:
for {
tok = fs.Scan()
// println(fmt.Sprintf("debug: tok: %v state: %v", tok, state))
if tok == fflib.FFTok_error {
goto tokerror
}
switch state {
case fflib.FFParse_map_start:
if tok != fflib.FFTok_left_bracket {
wantedTok = fflib.FFTok_left_bracket
goto wrongtokenerror
}
state = fflib.FFParse_want_key
continue
case fflib.FFParse_after_value:
if tok == fflib.FFTok_comma {
state = fflib.FFParse_want_key
} else if tok == fflib.FFTok_right_bracket {
goto done
} else {
wantedTok = fflib.FFTok_comma
goto wrongtokenerror
}
case fflib.FFParse_want_key:
// json {} ended. goto exit. woo.
if tok == fflib.FFTok_right_bracket {
goto done
}
if tok != fflib.FFTok_string {
wantedTok = fflib.FFTok_string
goto wrongtokenerror
}
kn := fs.Output.Bytes()
if len(kn) <= 0 {
// "" case. hrm.
currentKey = ffjtSmallPayloadFFJsonnosuchkey
state = fflib.FFParse_want_colon
goto mainparse
} else {
switch kn[0] {
case 'G':
if bytes.Equal(ffjKeySmallPayloadFFJsonGr, kn) {
currentKey = ffjtSmallPayloadFFJsonGr
state = fflib.FFParse_want_colon
goto mainparse
}
case 'I':
if bytes.Equal(ffjKeySmallPayloadFFJsonIp, kn) {
currentKey = ffjtSmallPayloadFFJsonIp
state = fflib.FFParse_want_colon
goto mainparse
}
case 'S':
if bytes.Equal(ffjKeySmallPayloadFFJsonSt, kn) {
currentKey = ffjtSmallPayloadFFJsonSt
state = fflib.FFParse_want_colon
goto mainparse
} else if bytes.Equal(ffjKeySmallPayloadFFJsonSid, kn) {
currentKey = ffjtSmallPayloadFFJsonSid
state = fflib.FFParse_want_colon
goto mainparse
}
case 'T':
if bytes.Equal(ffjKeySmallPayloadFFJsonTt, kn) {
currentKey = ffjtSmallPayloadFFJsonTt
state = fflib.FFParse_want_colon
goto mainparse
} else if bytes.Equal(ffjKeySmallPayloadFFJsonTz, kn) {
currentKey = ffjtSmallPayloadFFJsonTz
state = fflib.FFParse_want_colon
goto mainparse
}
case 'U':
if bytes.Equal(ffjKeySmallPayloadFFJsonUuid, kn) {
currentKey = ffjtSmallPayloadFFJsonUuid
state = fflib.FFParse_want_colon
goto mainparse
} else if bytes.Equal(ffjKeySmallPayloadFFJsonUa, kn) {
currentKey = ffjtSmallPayloadFFJsonUa
state = fflib.FFParse_want_colon
goto mainparse
}
case 'V':
if bytes.Equal(ffjKeySmallPayloadFFJsonV, kn) {
currentKey = ffjtSmallPayloadFFJsonV
state = fflib.FFParse_want_colon
goto mainparse
}
}
if fflib.SimpleLetterEqualFold(ffjKeySmallPayloadFFJsonV, kn) {
currentKey = ffjtSmallPayloadFFJsonV
state = fflib.FFParse_want_colon
goto mainparse
}
if fflib.SimpleLetterEqualFold(ffjKeySmallPayloadFFJsonTz, kn) {
currentKey = ffjtSmallPayloadFFJsonTz
state = fflib.FFParse_want_colon
goto mainparse
}
if fflib.SimpleLetterEqualFold(ffjKeySmallPayloadFFJsonUa, kn) {
currentKey = ffjtSmallPayloadFFJsonUa
state = fflib.FFParse_want_colon
goto mainparse
}
if fflib.SimpleLetterEqualFold(ffjKeySmallPayloadFFJsonIp, kn) {
currentKey = ffjtSmallPayloadFFJsonIp
state = fflib.FFParse_want_colon
goto mainparse
}
if fflib.SimpleLetterEqualFold(ffjKeySmallPayloadFFJsonUuid, kn) {
currentKey = ffjtSmallPayloadFFJsonUuid
state = fflib.FFParse_want_colon
goto mainparse
}
if fflib.SimpleLetterEqualFold(ffjKeySmallPayloadFFJsonGr, kn) {
currentKey = ffjtSmallPayloadFFJsonGr
state = fflib.FFParse_want_colon
goto mainparse
}
if fflib.SimpleLetterEqualFold(ffjKeySmallPayloadFFJsonTt, kn) {
currentKey = ffjtSmallPayloadFFJsonTt
state = fflib.FFParse_want_colon
goto mainparse
}
if fflib.EqualFoldRight(ffjKeySmallPayloadFFJsonSid, kn) {
currentKey = ffjtSmallPayloadFFJsonSid
state = fflib.FFParse_want_colon
goto mainparse
}
if fflib.EqualFoldRight(ffjKeySmallPayloadFFJsonSt, kn) {
currentKey = ffjtSmallPayloadFFJsonSt
state = fflib.FFParse_want_colon
goto mainparse
}
currentKey = ffjtSmallPayloadFFJsonnosuchkey
state = fflib.FFParse_want_colon
goto mainparse
}
case fflib.FFParse_want_colon:
if tok != fflib.FFTok_colon {
wantedTok = fflib.FFTok_colon
goto wrongtokenerror
}
state = fflib.FFParse_want_value
continue
case fflib.FFParse_want_value:
if tok == fflib.FFTok_left_brace || tok == fflib.FFTok_left_bracket || tok == fflib.FFTok_integer || tok == fflib.FFTok_double || tok == fflib.FFTok_string || tok == fflib.FFTok_bool || tok == fflib.FFTok_null {
switch currentKey {
case ffjtSmallPayloadFFJsonSt:
goto handle_St
case ffjtSmallPayloadFFJsonSid:
goto handle_Sid
case ffjtSmallPayloadFFJsonTt:
goto handle_Tt
case ffjtSmallPayloadFFJsonGr:
goto handle_Gr
case ffjtSmallPayloadFFJsonUuid:
goto handle_Uuid
case ffjtSmallPayloadFFJsonIp:
goto handle_Ip
case ffjtSmallPayloadFFJsonUa:
goto handle_Ua
case ffjtSmallPayloadFFJsonTz:
goto handle_Tz
case ffjtSmallPayloadFFJsonV:
goto handle_V
case ffjtSmallPayloadFFJsonnosuchkey:
err = fs.SkipField(tok)
if err != nil {
return fs.WrapErr(err)
}
state = fflib.FFParse_after_value
goto mainparse
}
} else {
goto wantedvalue
}
}
}
handle_St:
/* handler: j.St type=int kind=int quoted=false*/
{
if tok != fflib.FFTok_integer && tok != fflib.FFTok_null {
return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for int", tok))
}
}
{
if tok == fflib.FFTok_null {
} else {
tval, err := fflib.ParseInt(fs.Output.Bytes(), 10, 64)
if err != nil {
return fs.WrapErr(err)
}
j.St = int(tval)
}
}
state = fflib.FFParse_after_value
goto mainparse
handle_Sid:
/* handler: j.Sid type=int kind=int quoted=false*/
{
if tok != fflib.FFTok_integer && tok != fflib.FFTok_null {
return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for int", tok))
}
}
{
if tok == fflib.FFTok_null {
} else {
tval, err := fflib.ParseInt(fs.Output.Bytes(), 10, 64)
if err != nil {
return fs.WrapErr(err)
}
j.Sid = int(tval)
}
}
state = fflib.FFParse_after_value
goto mainparse
handle_Tt:
/* handler: j.Tt type=string kind=string quoted=false*/
{
{
if tok != fflib.FFTok_string && tok != fflib.FFTok_null {
return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for string", tok))
}
}
if tok == fflib.FFTok_null {
} else {
outBuf := fs.Output.Bytes()
j.Tt = string(string(outBuf))
}
}
state = fflib.FFParse_after_value
goto mainparse
handle_Gr:
/* handler: j.Gr type=int kind=int quoted=false*/
{
if tok != fflib.FFTok_integer && tok != fflib.FFTok_null {
return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for int", tok))
}
}
{
if tok == fflib.FFTok_null {
} else {
tval, err := fflib.ParseInt(fs.Output.Bytes(), 10, 64)
if err != nil {
return fs.WrapErr(err)
}
j.Gr = int(tval)
}
}
state = fflib.FFParse_after_value
goto mainparse
handle_Uuid:
/* handler: j.Uuid type=string kind=string quoted=false*/
{
{
if tok != fflib.FFTok_string && tok != fflib.FFTok_null {
return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for string", tok))
}
}
if tok == fflib.FFTok_null {
} else {
outBuf := fs.Output.Bytes()
j.Uuid = string(string(outBuf))
}
}
state = fflib.FFParse_after_value
goto mainparse
handle_Ip:
/* handler: j.Ip type=string kind=string quoted=false*/
{
{
if tok != fflib.FFTok_string && tok != fflib.FFTok_null {
return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for string", tok))
}
}
if tok == fflib.FFTok_null {
} else {
outBuf := fs.Output.Bytes()
j.Ip = string(string(outBuf))
}
}
state = fflib.FFParse_after_value
goto mainparse
handle_Ua:
/* handler: j.Ua type=string kind=string quoted=false*/
{
{
if tok != fflib.FFTok_string && tok != fflib.FFTok_null {
return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for string", tok))
}
}
if tok == fflib.FFTok_null {
} else {
outBuf := fs.Output.Bytes()
j.Ua = string(string(outBuf))
}
}
state = fflib.FFParse_after_value
goto mainparse
handle_Tz:
/* handler: j.Tz type=int kind=int quoted=false*/
{
if tok != fflib.FFTok_integer && tok != fflib.FFTok_null {
return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for int", tok))
}
}
{
if tok == fflib.FFTok_null {
} else {
tval, err := fflib.ParseInt(fs.Output.Bytes(), 10, 64)
if err != nil {
return fs.WrapErr(err)
}
j.Tz = int(tval)
}
}
state = fflib.FFParse_after_value
goto mainparse
handle_V:
/* handler: j.V type=int kind=int quoted=false*/
{
if tok != fflib.FFTok_integer && tok != fflib.FFTok_null {
return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for int", tok))
}
}
{
if tok == fflib.FFTok_null {
} else {
tval, err := fflib.ParseInt(fs.Output.Bytes(), 10, 64)
if err != nil {
return fs.WrapErr(err)
}
j.V = int(tval)
}
}
state = fflib.FFParse_after_value
goto mainparse
wantedvalue:
return fs.WrapErr(fmt.Errorf("wanted value token, but got token: %v", tok))
wrongtokenerror:
return fs.WrapErr(fmt.Errorf("ffjson: wanted token: %v, but got token: %v output=%s", wantedTok, tok, fs.Output.String()))
tokerror:
if fs.BigError != nil {
return fs.WrapErr(fs.BigError)
}
err = fs.Error.ToError()
if err != nil {
return fs.WrapErr(err)
}
panic("ffjson-generated: unreachable, please report bug.")
done:
return nil
}

BIN
benchmarks/testdata/code.json.gz vendored Normal file

Binary file not shown.