-
Notifications
You must be signed in to change notification settings - Fork 89
/
benchmarks_test.go
167 lines (157 loc) · 3.86 KB
/
benchmarks_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* MinIO Cloud Storage, (C) 2020 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package simdjson
import (
"testing"
)
func benchmarkFromFile(b *testing.B, filename string) {
if !SupportedCPU() {
b.SkipNow()
}
msg := loadCompressed(b, filename)
b.Run("copy", func(b *testing.B) {
pj := &ParsedJson{}
b.SetBytes(int64(len(msg)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Reset tape
var err error
pj, err = Parse(msg, pj, WithCopyStrings(true))
if err != nil {
b.Fatal(err)
}
}
})
b.Run("nocopy", func(b *testing.B) {
pj := &ParsedJson{}
b.SetBytes(int64(len(msg)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Reset tape
var err error
pj, err = Parse(msg, pj, WithCopyStrings(false))
if err != nil {
b.Fatal(err)
}
}
})
b.Run("nocopy-par", func(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
pj := &ParsedJson{}
b.SetBytes(int64(len(msg)))
b.ReportAllocs()
b.ResetTimer()
for pb.Next() {
// Reset tape
var err error
pj, err = Parse(msg, pj, WithCopyStrings(false))
if err != nil {
b.Fatal(err)
}
}
})
})
}
func BenchmarkParseSmall(b *testing.B) { benchmarkFromFile(b, "payload-small") }
func BenchmarkParseMedium(b *testing.B) { benchmarkFromFile(b, "payload-medium") }
func BenchmarkParseLarge(b *testing.B) { benchmarkFromFile(b, "payload-large") }
func BenchmarkJsonParserLarge(b *testing.B) {
largeFixture := loadCompressed(b, "payload-large")
b.Run("nocopy", func(b *testing.B) {
pj := &ParsedJson{}
b.SetBytes(int64(len(largeFixture)))
b.ReportAllocs()
b.ResetTimer()
var elem *Element
var ar *Array
var obj *Object
var onlyKeys = map[string]struct{}{
"id": {},
"slug": {},
}
const checkErrs = false
for i := 0; i < b.N; i++ {
// Reset tape
var err error
pj, err = Parse(largeFixture, pj, WithCopyStrings(false))
if checkErrs && err != nil {
b.Fatal(err)
}
iter := pj.Iter()
elem, err = iter.FindElement(elem, "users")
if checkErrs && err != nil {
b.Fatal(err)
}
ar, err = elem.Iter.Array(ar)
if checkErrs && err != nil {
b.Fatal(err)
}
ar.ForEach(func(i Iter) {
elem, err = i.FindElement(elem, "username")
if checkErrs && err != nil {
b.Fatal(err)
}
_, _ = elem.Iter.StringBytes()
})
elem, err = iter.FindElement(elem, "topics", "topics")
if checkErrs && err != nil {
b.Fatal(err)
}
ar, err = elem.Iter.Array(ar)
if checkErrs && err != nil {
b.Fatal(err)
}
ar.ForEach(func(i Iter) {
if true {
// Use foreach...
obj, err = i.Object(obj)
if checkErrs && err != nil {
b.Fatal(err)
}
obj.ForEach(func(key []byte, i Iter) {
if string(key) == "id" {
_, err = i.Int()
if checkErrs && err != nil {
b.Fatal(err)
}
}
if string(key) == "slug" {
_, err = i.StringBytes()
if checkErrs && err != nil {
b.Fatal(err)
}
}
}, onlyKeys)
} else {
elem, err = i.FindElement(elem, "id")
if checkErrs && err != nil {
b.Fatal(err)
}
_, _ = elem.Iter.Int()
//b.Log(elem.Iter.Int())
elem, err = i.FindElement(elem, "slug")
if checkErrs && err != nil {
b.Fatal(err)
}
_, _ = elem.Iter.StringBytes()
//b.Log(elem.Iter.String())
}
})
}
})
}