-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_selective_test.go
More file actions
289 lines (253 loc) · 8.06 KB
/
benchmark_selective_test.go
File metadata and controls
289 lines (253 loc) · 8.06 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package purejson
import (
"encoding/json"
"fmt"
"sort"
"strconv"
"testing"
gojson "github.com/goccy/go-json"
)
var benchmarkTier3SelectiveResult benchmarkExtractionResult
func BenchmarkTier3SelectivePlaceholder_twitter_json(b *testing.B) {
runTier3SelectivePlaceholderBenchmark(b, benchmarkFixtureTwitter)
}
func BenchmarkTier3SelectivePlaceholder_citm_catalog_json(b *testing.B) {
runTier3SelectivePlaceholderBenchmark(b, benchmarkFixtureCITM)
}
// Tier 3 remains a DOM-era placeholder benchmark. It measures selective reads
// on the current DOM API only and does not imply a new On-Demand or path-query
// surface in v0.1.
func runTier3SelectivePlaceholderBenchmark(b *testing.B, fixtureName string) {
data := loadBenchmarkFixture(b, fixtureName)
for _, comparator := range availableTier2TypedComparators(b) {
comparator := comparator
b.Run(comparator.key, func(b *testing.B) {
b.ReportAllocs()
b.SetBytes(int64(len(data)))
if comparator.key == benchmarkComparatorPureSimdjson {
parser := benchmarkWarmPureParser(b, fixtureName, data)
defer func() {
if err := parser.Close(); err != nil {
b.Fatalf("parser.Close(%s): %v", fixtureName, err)
}
}()
benchmarkRunWithNativeAllocMetrics(b, true, func() {
for i := 0; i < b.N; i++ {
result, err := benchmarkTier3SelectivePlaceholderPureSimdjsonWithParser(parser, fixtureName, data)
if err != nil {
b.Fatalf("%s selective placeholder(%s): %v", comparator.key, fixtureName, err)
}
benchmarkTier3SelectiveResult = result
}
})
return
}
benchmarkRunWithNativeAllocMetrics(b, false, func() {
for i := 0; i < b.N; i++ {
result, err := benchmarkTier3SelectivePlaceholderExtract(comparator.key, fixtureName, data)
if err != nil {
b.Fatalf("%s selective placeholder(%s): %v", comparator.key, fixtureName, err)
}
benchmarkTier3SelectiveResult = result
}
})
})
}
}
func benchmarkTier3SelectivePlaceholderExtract(comparatorKey, fixtureName string, data []byte) (benchmarkExtractionResult, error) {
switch comparatorKey {
case benchmarkComparatorPureSimdjson:
return benchmarkTier3SelectivePlaceholderPureSimdjson(fixtureName, data)
case benchmarkComparatorEncodingStruct:
value, err := benchmarkDecodeSharedSchema(json.Unmarshal, fixtureName, data)
if err != nil {
return benchmarkExtractionResult{}, err
}
return benchmarkTier3SelectivePlaceholderResultFromSharedSchema(value)
case benchmarkComparatorGoccyGoJSON:
value, err := benchmarkDecodeSharedSchema(gojson.Unmarshal, fixtureName, data)
if err != nil {
return benchmarkExtractionResult{}, err
}
return benchmarkTier3SelectivePlaceholderResultFromSharedSchema(value)
case benchmarkComparatorBytedanceSonic:
value, err := benchmarkDecodeSharedSchemaBytedanceSonic(fixtureName, data)
if err != nil {
return benchmarkExtractionResult{}, err
}
return benchmarkTier3SelectivePlaceholderResultFromSharedSchema(value)
default:
return benchmarkExtractionResult{}, fmt.Errorf("selective placeholder unsupported for comparator %q", comparatorKey)
}
}
func benchmarkTier3SelectivePlaceholderPureSimdjson(fixtureName string, data []byte) (result benchmarkExtractionResult, err error) {
parser, err := NewParser()
if err != nil {
return result, err
}
defer func() {
err = benchmarkCloseMaterializeResources(err, nil, parser)
}()
return benchmarkTier3SelectivePlaceholderPureSimdjsonWithParser(parser, fixtureName, data)
}
func benchmarkTier3SelectivePlaceholderPureSimdjsonWithParser(parser *Parser, fixtureName string, data []byte) (result benchmarkExtractionResult, err error) {
var doc *Doc
defer func() {
err = benchmarkCloseMaterializeResources(err, doc, nil)
}()
doc, err = parser.Parse(data)
if err != nil {
return result, err
}
switch fixtureName {
case benchmarkFixtureTwitter:
return benchmarkTier3SelectivePlaceholderTwitterElement(doc.Root())
case benchmarkFixtureCITM:
return benchmarkTier3SelectivePlaceholderCITMElement(doc.Root())
default:
return result, fmt.Errorf("selective placeholder unsupported for fixture %q", fixtureName)
}
}
func benchmarkTier3SelectivePlaceholderResultFromSharedSchema(value any) (benchmarkExtractionResult, error) {
switch row := value.(type) {
case benchTwitterRow:
return benchmarkTier3SelectivePlaceholderTwitterRow(row), nil
case benchCITMRow:
return benchmarkTier3SelectivePlaceholderCITMRow(row)
default:
return benchmarkExtractionResult{}, fmt.Errorf("unsupported selective placeholder value %T", value)
}
}
func benchmarkTier3SelectivePlaceholderTwitterRow(row benchTwitterRow) benchmarkExtractionResult {
result := benchmarkExtractionResult{}
limit := len(row.Statuses)
if limit > 10 {
limit = 10
}
for i := 0; i < limit; i++ {
status := row.Statuses[i]
result.int64Sum += status.User.ID
if status.Retweeted {
result.trueCount++
}
}
return result
}
func benchmarkTier3SelectivePlaceholderCITMRow(row benchCITMRow) (benchmarkExtractionResult, error) {
result := benchmarkExtractionResult{
int64Sum: int64(len(row.AreaNames)),
}
ids, err := benchmarkSortedCITMEventIDs(row.Events)
if err != nil {
return benchmarkExtractionResult{}, err
}
limit := len(ids)
if limit > 20 {
limit = 20
}
for i := 0; i < limit; i++ {
result.int64Sum += ids[i]
}
return result, nil
}
func benchmarkTier3SelectivePlaceholderTwitterElement(root Element) (benchmarkExtractionResult, error) {
rootObject, err := root.AsObject()
if err != nil {
return benchmarkExtractionResult{}, err
}
statusesArray, err := benchmarkObjectFieldAsArray(rootObject, "statuses")
if err != nil {
return benchmarkExtractionResult{}, err
}
result := benchmarkExtractionResult{}
iter := statusesArray.Iter()
for seen := 0; seen < 10 && iter.Next(); seen++ {
statusObject, err := iter.Value().AsObject()
if err != nil {
return benchmarkExtractionResult{}, err
}
userObject, err := benchmarkObjectFieldAsObject(statusObject, "user")
if err != nil {
return benchmarkExtractionResult{}, err
}
userID, err := benchmarkObjectFieldInt64(userObject, "id")
if err != nil {
return benchmarkExtractionResult{}, err
}
retweeted, err := benchmarkObjectFieldBool(statusObject, "retweeted")
if err != nil {
return benchmarkExtractionResult{}, err
}
result.int64Sum += userID
if retweeted {
result.trueCount++
}
}
if err := iter.Err(); err != nil {
return benchmarkExtractionResult{}, err
}
return result, nil
}
func benchmarkTier3SelectivePlaceholderCITMElement(root Element) (benchmarkExtractionResult, error) {
rootObject, err := root.AsObject()
if err != nil {
return benchmarkExtractionResult{}, err
}
areaNames, err := benchmarkObjectFieldAsObject(rootObject, "areaNames")
if err != nil {
return benchmarkExtractionResult{}, err
}
events, err := benchmarkObjectFieldAsObject(rootObject, "events")
if err != nil {
return benchmarkExtractionResult{}, err
}
result := benchmarkExtractionResult{}
areaCount, err := benchmarkCountObjectEntries(areaNames)
if err != nil {
return benchmarkExtractionResult{}, err
}
result.int64Sum = int64(areaCount)
eventIter := events.Iter()
keys := make([]string, 0)
for eventIter.Next() {
keys = append(keys, eventIter.Key())
}
if err := eventIter.Err(); err != nil {
return benchmarkExtractionResult{}, err
}
ids, err := benchmarkSortedCITMEventIDsFromKeys(keys)
if err != nil {
return benchmarkExtractionResult{}, err
}
limit := len(ids)
if limit > 20 {
limit = 20
}
for i := 0; i < limit; i++ {
result.int64Sum += ids[i]
}
return result, nil
}
func benchmarkCountObjectEntries(object Object) (int, error) {
count := 0
iter := object.Iter()
for iter.Next() {
count++
}
if err := iter.Err(); err != nil {
return 0, err
}
return count, nil
}
func benchmarkSortedCITMEventIDsFromKeys(keys []string) ([]int64, error) {
ids := make([]int64, 0, len(keys))
for _, key := range keys {
value, err := strconv.ParseInt(key, 10, 64)
if err != nil {
return nil, fmt.Errorf("parse event id %q: %w", key, err)
}
ids = append(ids, value)
}
sort.Slice(ids, func(i, j int) bool { return ids[i] < ids[j] })
return ids, nil
}