-
Notifications
You must be signed in to change notification settings - Fork 7
/
bench_test.go
220 lines (200 loc) · 5.1 KB
/
bench_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
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
package jscan_test
import (
"compress/gzip"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/romshark/jscan/v2"
"github.com/stretchr/testify/require"
)
type Stats struct {
TotalStrings int
TotalNulls int
TotalBooleans int
TotalNumbers int
TotalObjects int
TotalArrays int
TotalKeys int
MaxKeyLen int
MaxDepth int
MaxArrayLen int
}
func MustCalcStatsJscan(p *jscan.Parser[[]byte], str []byte) (s Stats) {
if err := p.Scan(
str,
func(i *jscan.Iterator[[]byte]) (err bool) {
if i.KeyIndex() != -1 {
// Calculate key length excluding the quotes
l := i.KeyIndexEnd() - i.KeyIndex() - 2
s.TotalKeys++
if l > s.MaxKeyLen {
s.MaxKeyLen = l
}
}
switch i.ValueType() {
case jscan.ValueTypeObject:
s.TotalObjects++
case jscan.ValueTypeArray:
s.TotalArrays++
case jscan.ValueTypeNull:
s.TotalNulls++
case jscan.ValueTypeFalse, jscan.ValueTypeTrue:
s.TotalBooleans++
case jscan.ValueTypeNumber:
s.TotalNumbers++
case jscan.ValueTypeString:
s.TotalStrings++
}
if i.Level() > s.MaxDepth {
s.MaxDepth = i.Level()
}
if l := i.ArrayIndex() + 1; l > s.MaxArrayLen {
s.MaxArrayLen = l
}
return false
},
); err.IsErr() {
panic(fmt.Errorf("unexpected error: %s", err))
}
return
}
func TestCalcStats(t *testing.T) {
const input = `{
"s":"value",
"t":true,
"f":false,
"0":null,
"n":-9.123e3,
"o0":{},
"a0":[],
"o":{
"k":"\"v\"",
"a":[
true,
null,
"item",
-67.02e9,
["foo"]
]
},
"[abc]":[0]
}`
expect := Stats{
TotalStrings: 4,
TotalNulls: 2,
TotalBooleans: 3,
TotalNumbers: 3,
TotalObjects: 3,
TotalArrays: 4,
MaxDepth: 4,
TotalKeys: 11,
MaxKeyLen: 5,
MaxArrayLen: 5,
}
p := jscan.NewParser[[]byte](64)
require.Equal(t, expect, MustCalcStatsJscan(p, []byte(input)))
}
var gs Stats
func BenchmarkCalcStats(b *testing.B) {
for _, bd := range []struct {
name string
input SourceProvider
}{
{"miniscule_1b__________", SrcFile("miniscule_1b.json")},
{"tiny_8b_______________", SrcFile("tiny_8b.json")},
{"small_336b____________", SrcFile("small_336b.json")},
{"large_26m_____________", SrcFile("large_26m.json.gz")},
{"nasa_SxSW_2016_125k___", SrcFile("nasa_SxSW_2016_125k.json.gz")},
{"escaped_3k____________", SrcFile("escaped_3k.json")},
{"array_int_1024_12k____", SrcFile("array_int_1024_12k.json")},
{"array_dec_1024_10k____", SrcFile("array_dec_1024_10k.json")},
{"array_nullbool_1024_5k", SrcFile("array_nullbool_1024_5k.json")},
{"array_str_1024_639k___", SrcFile("array_str_1024_639k.json")},
} {
b.Run(bd.name, func(b *testing.B) {
src, err := bd.input.GetJSON()
require.NoError(b, err)
p := jscan.NewParser[[]byte](1024)
b.ResetTimer()
for i := 0; i < b.N; i++ {
gs = MustCalcStatsJscan(p, src)
}
})
}
}
func TestValid(t *testing.T) {
j := `[false,[[2, {"[foo]":[{"bar-baz":"fuz"}]}]]]`
require.True(t, json.Valid([]byte(j)))
require.True(t, jscan.Valid(j))
}
var GB bool
func repeat(s string, n int) string {
b := make([]byte, n*len(s))
for i := 0; i < n; i++ {
b = append(b, s...)
}
return string(b)
}
func BenchmarkValid(b *testing.B) {
for _, bd := range []struct {
name string
input SourceProvider
}{
{"deeparray_____________", SrcMake(func() []byte {
return []byte(repeat("[", 1024) + repeat("]", 1024))
})},
{"unwind_stack__________", SrcMake(func() []byte {
return []byte(repeat("[", 1024))
})},
{"miniscule_1b__________", SrcFile("miniscule_1b.json")},
{"tiny_8b_______________", SrcFile("tiny_8b.json")},
{"small_336b____________", SrcFile("small_336b.json")},
{"large_26m_____________", SrcFile("large_26m.json.gz")},
{"nasa_SxSW_2016_125k___", SrcFile("nasa_SxSW_2016_125k.json.gz")},
{"escaped_3k____________", SrcFile("escaped_3k.json")},
{"array_int_1024_12k____", SrcFile("array_int_1024_12k.json")},
{"array_dec_1024_10k____", SrcFile("array_dec_1024_10k.json")},
{"array_nullbool_1024_5k", SrcFile("array_nullbool_1024_5k.json")},
{"array_str_1024_639k___", SrcFile("array_str_1024_639k.json")},
} {
b.Run(bd.name, func(b *testing.B) {
src, err := bd.input.GetJSON()
require.NoError(b, err)
v := jscan.NewValidator[[]byte](1024)
b.ResetTimer()
for i := 0; i < b.N; i++ {
GB = v.Valid(src)
}
})
}
}
type SourceProvider interface{ GetJSON() ([]byte, error) }
type SrcMake func() []byte
func (s SrcMake) GetJSON() ([]byte, error) { return s(), nil }
type SrcFile string
func (s SrcFile) GetJSON() ([]byte, error) {
p := filepath.Join("testdata", string(s))
switch {
case strings.HasSuffix(string(s), ".json"):
return os.ReadFile(p)
case strings.HasSuffix(string(s), ".gz"):
f, err := os.Open(p)
if err != nil {
return nil, fmt.Errorf("opening archive file: %w", err)
}
r, err := gzip.NewReader(f)
if err != nil {
return nil, fmt.Errorf("initializing gzip reader: %w", err)
}
b, err := io.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("reading from gzip reader: %w", err)
}
return b, nil
}
return nil, fmt.Errorf("unsupported file: %q", s)
}