Skip to content

Commit 03905a4

Browse files
committed
Added ObjectEach benchmarks, expanded/improved EachKey benchmarks
1 parent 09116d6 commit 03905a4

2 files changed

Lines changed: 152 additions & 26 deletions

File tree

benchmark/benchmark_medium_payload_test.go

Lines changed: 96 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/ugorji/go/codec"
1717
"testing"
1818
// "fmt"
19+
"bytes"
20+
"errors"
1921
)
2022

2123
/*
@@ -35,22 +37,22 @@ func BenchmarkJsonParserMedium(b *testing.B) {
3537
}
3638

3739
func BenchmarkJsonParserEachKeyManualMedium(b *testing.B) {
38-
for i := 0; i < b.N; i++ {
39-
paths := [][]string{
40-
[]string{"person", "name", "fullName"},
41-
[]string{"person", "github", "followers"},
42-
[]string{"company"},
43-
[]string{"person", "gravatar", "avatars"},
44-
}
40+
paths := [][]string{
41+
[]string{"person", "name", "fullName"},
42+
[]string{"person", "github", "followers"},
43+
[]string{"company"},
44+
[]string{"person", "gravatar", "avatars"},
45+
}
4546

46-
jsonparser.EachKey(mediumFixture, func(idx int, value []byte, vt jsonparser.ValueType, err error){
47+
for i := 0; i < b.N; i++ {
48+
jsonparser.EachKey(mediumFixture, func(idx int, value []byte, vt jsonparser.ValueType, err error) {
4749
switch idx {
4850
case 0:
49-
// jsonparser.ParseString(value)
51+
// jsonparser.ParseString(value)
5052
case 1:
5153
jsonparser.ParseInt(value)
5254
case 2:
53-
// jsonparser.ParseString(value)
55+
// jsonparser.ParseString(value)
5456
case 3:
5557
jsonparser.ArrayEach(value, func(avalue []byte, dataType jsonparser.ValueType, offset int, err error) {
5658
jsonparser.Get(avalue, "url")
@@ -60,6 +62,90 @@ func BenchmarkJsonParserEachKeyManualMedium(b *testing.B) {
6062
}
6163
}
6264

65+
func BenchmarkJsonParserEachKeyStructMedium(b *testing.B) {
66+
paths := [][]string{
67+
[]string{"person", "name", "fullName"},
68+
[]string{"person", "github", "followers"},
69+
[]string{"company"},
70+
[]string{"person", "gravatar", "avatars"},
71+
}
72+
73+
for i := 0; i < b.N; i++ {
74+
data := MediumPayload{
75+
Person: &CBPerson{
76+
Name: &CBName{},
77+
Github: &CBGithub{},
78+
Gravatar: &CBGravatar{},
79+
},
80+
}
81+
82+
jsonparser.EachKey(mediumFixture, func(idx int, value []byte, vt jsonparser.ValueType, err error) {
83+
switch idx {
84+
case 0:
85+
data.Person.Name.FullName, _ = jsonparser.ParseString(value)
86+
case 1:
87+
v, _ := jsonparser.ParseInt(value)
88+
data.Person.Github.Followers = int(v)
89+
case 2:
90+
json.Unmarshal(value, &data.Company) // we don't have a JSON -> map[string]interface{} function yet, so use standard encoding/json here
91+
case 3:
92+
var avatars []*CBAvatar
93+
jsonparser.ArrayEach(value, func(avalue []byte, dataType jsonparser.ValueType, offset int, err error) {
94+
url, _ := jsonparser.ParseString(avalue)
95+
avatars = append(avatars, &CBAvatar{Url: url})
96+
})
97+
data.Person.Gravatar.Avatars = avatars
98+
}
99+
}, paths...)
100+
}
101+
}
102+
103+
func BenchmarkJsonParserObjectEachStructMedium(b *testing.B) {
104+
nameKey, githubKey, gravatarKey := []byte("name"), []byte("github"), []byte("gravatar")
105+
errStop := errors.New("stop")
106+
107+
for i := 0; i < b.N; i++ {
108+
data := MediumPayload{
109+
Person: &CBPerson{
110+
Name: &CBName{},
111+
Github: &CBGithub{},
112+
Gravatar: &CBGravatar{},
113+
},
114+
}
115+
116+
missing := 3
117+
118+
jsonparser.ObjectEach(mediumFixture, func(k, v []byte, vt jsonparser.ValueType, o int) error {
119+
switch {
120+
case bytes.Equal(k, nameKey):
121+
data.Person.Name.FullName, _ = jsonparser.GetString(v, "fullName")
122+
missing--
123+
case bytes.Equal(k, githubKey):
124+
x, _ := jsonparser.GetInt(v, "followers")
125+
data.Person.Github.Followers = int(x)
126+
missing--
127+
case bytes.Equal(k, gravatarKey):
128+
var avatars []*CBAvatar
129+
jsonparser.ArrayEach(v, func(avalue []byte, dataType jsonparser.ValueType, offset int, err error) {
130+
url, _ := jsonparser.ParseString(avalue)
131+
avatars = append(avatars, &CBAvatar{Url: url})
132+
}, "avatars")
133+
data.Person.Gravatar.Avatars = avatars
134+
missing--
135+
}
136+
137+
if missing == 0 {
138+
return errStop
139+
} else {
140+
return nil
141+
}
142+
}, "person")
143+
144+
cv, _, _, _ := jsonparser.Get(mediumFixture, "company")
145+
json.Unmarshal(cv, &data.Company)
146+
}
147+
}
148+
63149
/*
64150
encoding/json
65151
*/

benchmark/benchmark_small_payload_test.go

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/ugorji/go/codec"
1717
"testing"
1818
// "fmt"
19+
"bytes"
20+
"errors"
1921
)
2022

2123
// Just for emulating field access, so it will not throw "evaluated but not used"
@@ -36,15 +38,15 @@ func BenchmarkJsonParserSmall(b *testing.B) {
3638
}
3739

3840
func BenchmarkJsonParserEachKeyManualSmall(b *testing.B) {
41+
paths := [][]string{
42+
[]string{"uuid"},
43+
[]string{"tz"},
44+
[]string{"ua"},
45+
[]string{"st"},
46+
}
47+
3948
for i := 0; i < b.N; i++ {
40-
paths := [][]string{
41-
[]string{"uuid"},
42-
[]string{"tz"},
43-
[]string{"ua"},
44-
[]string{"st"},
45-
}
46-
47-
jsonparser.EachKey(smallFixture, func(idx int, value []byte, vt jsonparser.ValueType, err error){
49+
jsonparser.EachKey(smallFixture, func(idx int, value []byte, vt jsonparser.ValueType, err error) {
4850
switch idx {
4951
case 0:
5052
// jsonparser.ParseString(value)
@@ -59,18 +61,18 @@ func BenchmarkJsonParserEachKeyManualSmall(b *testing.B) {
5961
}
6062
}
6163

62-
6364
func BenchmarkJsonParserEachKeyStructSmall(b *testing.B) {
65+
paths := [][]string{
66+
[]string{"uuid"},
67+
[]string{"tz"},
68+
[]string{"ua"},
69+
[]string{"st"},
70+
}
71+
6472
for i := 0; i < b.N; i++ {
65-
paths := [][]string{
66-
[]string{"uuid"},
67-
[]string{"tz"},
68-
[]string{"ua"},
69-
[]string{"st"},
70-
}
7173
var data SmallPayload
7274

73-
jsonparser.EachKey(smallFixture, func(idx int, value []byte, vt jsonparser.ValueType, err error){
75+
jsonparser.EachKey(smallFixture, func(idx int, value []byte, vt jsonparser.ValueType, err error) {
7476
switch idx {
7577
case 0:
7678
data.Uuid, _ = jsonparser.ParseString(value)
@@ -89,6 +91,44 @@ func BenchmarkJsonParserEachKeyStructSmall(b *testing.B) {
8991
}
9092
}
9193

94+
func BenchmarkJsonParserObjectEachStructSmall(b *testing.B) {
95+
uuidKey, tzKey, uaKey, stKey := []byte("uuid"), []byte("tz"), []byte("ua"), []byte("st")
96+
errStop := errors.New("stop")
97+
98+
for i := 0; i < b.N; i++ {
99+
var data SmallPayload
100+
101+
missing := 4
102+
103+
jsonparser.ObjectEach(smallFixture, func(key, value []byte, vt jsonparser.ValueType, off int) error {
104+
switch {
105+
case bytes.Equal(key, uuidKey):
106+
data.Uuid, _ = jsonparser.ParseString(value)
107+
missing--
108+
case bytes.Equal(key, tzKey):
109+
v, _ := jsonparser.ParseInt(value)
110+
data.Tz = int(v)
111+
missing--
112+
case bytes.Equal(key, uaKey):
113+
data.Ua, _ = jsonparser.ParseString(value)
114+
missing--
115+
case bytes.Equal(key, stKey):
116+
v, _ := jsonparser.ParseInt(value)
117+
data.St = int(v)
118+
missing--
119+
}
120+
121+
if missing == 0 {
122+
return errStop
123+
} else {
124+
return nil
125+
}
126+
})
127+
128+
nothing(data.Uuid, data.Tz, data.Ua, data.St)
129+
}
130+
}
131+
92132
/*
93133
encoding/json
94134
*/

0 commit comments

Comments
 (0)