-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalid.go
62 lines (58 loc) · 1.41 KB
/
valid.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
package json_benchmark
import (
"encoding/json"
"testing"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
"github.com/zerosnake0/jzon"
)
func Test_Valid(t *testing.T, data []byte, valid bool) {
t.Run(pkgJson, func(t *testing.T) {
require.Equal(t, valid, json.Valid(data))
})
t.Run(pkgJsoniter, func(t *testing.T) {
require.Equal(t, valid, jsoniter.Valid(data))
})
t.Run(pkgJsoniterCompatible, func(t *testing.T) {
require.Equal(t, valid, jsoniter.ConfigCompatibleWithStandardLibrary.Valid(data))
})
t.Run(pkgGJson, func(t *testing.T) {
require.Equal(t, valid, gjson.ValidBytes(data))
})
t.Run(pkgJzon, func(t *testing.T) {
require.Equal(t, valid, jzon.Valid(data))
})
}
func Benchmark_Valid(b *testing.B, data []byte) {
b.Run(pkgJson, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
json.Valid(data)
}
})
b.Run(pkgJsoniter, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
jsoniter.Valid(data)
}
})
b.Run(pkgJsoniterCompatible, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
jsoniter.ConfigCompatibleWithStandardLibrary.Valid(data)
}
})
b.Run(pkgGJson, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
gjson.ValidBytes(data)
}
})
b.Run(pkgJzon, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
jzon.Valid(data)
}
})
}