-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_bench_test.go
60 lines (48 loc) · 1.04 KB
/
json_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
package yajson
import (
"encoding/json"
"github.com/indigo-web/utils/uf"
"github.com/romshark/jscan/v2"
"testing"
)
func BenchmarkJSON(b *testing.B) {
j := `
{
"foo": "any text here",
"some_thing": "some text inside of the string",
"Nothing": "Hello, world!",
"any string": "This must never appear"
}
`
parser := New[basicStringModel]()
m := basicStringModel{}
b.Run("my parser", func(b *testing.B) {
b.SetBytes(int64(len(j)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m, _ = parser.Parse(j)
}
})
b.Run("without my parser", func(b *testing.B) {
b.SetBytes(int64(len(j)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = jscan.Scan(j, func(i *jscan.Iterator[string]) (err bool) {
return false
})
}
})
b.Run("std", func(b *testing.B) {
model := any(&basicStringModel{})
b.SetBytes(int64(len(j)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = json.Unmarshal(uf.S2B(j), model)
}
})
keepalive(m)
}
func keepalive(basicStringModel) {}