Skip to content

Commit

Permalink
Merge pull request #84 from goccy/feature/add-benchmark-target
Browse files Browse the repository at this point in the history
Add benchmark target
  • Loading branch information
goccy committed Jan 15, 2021
2 parents 2bec292 + 0d97f97 commit dc52214
Show file tree
Hide file tree
Showing 10 changed files with 1,301 additions and 7 deletions.
63 changes: 59 additions & 4 deletions benchmarks/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (

"github.com/goccy/go-json"
jsoniter "github.com/json-iterator/go"
segmentiojson "github.com/segmentio/encoding/json"
"github.com/wI2L/jettison"
)

type codeResponse struct {
Expand Down Expand Up @@ -94,6 +96,24 @@ func codeInit() {
}
}

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(ioutil.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 {
Expand Down Expand Up @@ -131,15 +151,15 @@ func Benchmark_EncodeBigData_JsonIter(b *testing.B) {
b.SetBytes(int64(len(codeJSON)))
}

func Benchmark_EncodeBigData_GoJson(b *testing.B) {
func Benchmark_EncodeBigData_SegmentioJson(b *testing.B) {
b.ReportAllocs()
if codeJSON == nil {
b.StopTimer()
codeInit()
b.StartTimer()
}
b.RunParallel(func(pb *testing.PB) {
enc := json.NewEncoder(ioutil.Discard)
enc := segmentiojson.NewEncoder(ioutil.Discard)
for pb.Next() {
if err := enc.Encode(&codeStruct); err != nil {
b.Fatal("Encode:", err)
Expand All @@ -149,6 +169,24 @@ func Benchmark_EncodeBigData_GoJson(b *testing.B) {
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 {
Expand Down Expand Up @@ -184,7 +222,7 @@ func Benchmark_MarshalBigData_JsonIter(b *testing.B) {
b.SetBytes(int64(len(codeJSON)))
}

func Benchmark_MarshalBigData_GoJson(b *testing.B) {
func Benchmark_MarshalBigData_Jettison(b *testing.B) {
b.ReportAllocs()
if codeJSON == nil {
b.StopTimer()
Expand All @@ -193,7 +231,24 @@ func Benchmark_MarshalBigData_GoJson(b *testing.B) {
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err := json.Marshal(&codeStruct); err != nil {
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)
}
}
Expand Down
115 changes: 115 additions & 0 deletions benchmarks/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
gojson "github.com/goccy/go-json"
jsoniter "github.com/json-iterator/go"
segmentiojson "github.com/segmentio/encoding/json"
"github.com/wI2L/jettison"
)

func Benchmark_Encode_SmallStruct_EncodingJson(b *testing.B) {
Expand All @@ -29,6 +30,24 @@ func Benchmark_Encode_SmallStruct_JsonIter(b *testing.B) {
}
}

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++ {
Expand Down Expand Up @@ -86,6 +105,26 @@ func Benchmark_Encode_SmallStructCached_JsonIter(b *testing.B) {
}
}

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()
Expand Down Expand Up @@ -145,6 +184,24 @@ func Benchmark_Encode_MediumStruct_JsonIter(b *testing.B) {
}
}

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++ {
Expand Down Expand Up @@ -202,6 +259,26 @@ func Benchmark_Encode_MediumStructCached_JsonIter(b *testing.B) {
}
}

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()
Expand Down Expand Up @@ -261,6 +338,24 @@ func Benchmark_Encode_LargeStruct_JsonIter(b *testing.B) {
}
}

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++ {
Expand Down Expand Up @@ -318,6 +413,26 @@ func Benchmark_Encode_LargeStructCached_JsonIter(b *testing.B) {
}
}

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()
Expand Down
2 changes: 2 additions & 0 deletions benchmarks/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ 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.9
github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe
github.com/segmentio/encoding v0.2.4
github.com/wI2L/jettison v0.7.1
)

replace github.com/goccy/go-json => ../
10 changes: 10 additions & 0 deletions benchmarks/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBT
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=
Expand Down Expand Up @@ -54,7 +55,9 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
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=
Expand All @@ -66,6 +69,7 @@ github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9
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=
Expand All @@ -75,6 +79,7 @@ github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:
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=
Expand Down Expand Up @@ -106,9 +111,12 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
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/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
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=
Expand Down Expand Up @@ -166,9 +174,11 @@ google.golang.org/grpc v1.16.0/go.mod h1:0JHn/cJsOMiMfNA9+DeHDlAU7KAAB5GDlYFpa9M
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=
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=
Expand Down
35 changes: 34 additions & 1 deletion benchmarks/large_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ type LargePayload struct {
Topics *DSTopicsList
}

//easyjson:json
type LargePayloadEasyJson struct {
Users DSUsers
Topics *DSTopicsList
}

func (m *LargePayload) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
switch key {
case "users":
Expand All @@ -134,7 +140,6 @@ func (m *LargePayload) NKeys() int {
return 2
}

//easyjson:json
func (m *LargePayload) MarshalJSONObject(enc *gojay.Encoder) {
enc.AddArrayKey("users", &m.Users)
enc.AddObjectKey("topics", m.Topics)
Expand Down Expand Up @@ -175,3 +180,31 @@ func NewLargePayload() *LargePayload {
},
}
}

func NewLargePayloadEasyJson() *LargePayloadEasyJson {
dsUsers := DSUsers{}
dsTopics := DSTopics{}
for i := 0; i < 100; i++ {
str := "test" + strconv.Itoa(i)
dsUsers = append(
dsUsers,
&DSUser{
Username: str,
},
)
dsTopics = append(
dsTopics,
&DSTopic{
Id: i,
Slug: str,
},
)
}
return &LargePayloadEasyJson{
Users: dsUsers,
Topics: &DSTopicsList{
Topics: dsTopics,
MoreTopicsUrl: "http://test.com",
},
}
}

0 comments on commit dc52214

Please sign in to comment.