-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstandard_test.go
69 lines (54 loc) · 1.43 KB
/
standard_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
package json
import (
"encoding/json"
"strconv"
"testing"
)
func BenchmarkStdjsonUnmarshal(b *testing.B) {
b.Run(strconv.Itoa(len(smallStructText)), func(b *testing.B) { benchmarkStdUnmarshalSmallData(b) })
b.Run(strconv.Itoa(len(largeStructText)), func(b *testing.B) { benchmarkStdUnmarshalLargeData(b) })
}
func benchmarkStdUnmarshalLargeData(b *testing.B) {
b.SetBytes(int64(len(largeStructText)))
for i := 0; i < b.N; i++ {
var s LargeStruct
if err := json.Unmarshal(largeStructText, &s); err != nil {
b.Error(err)
}
}
}
func benchmarkStdUnmarshalSmallData(b *testing.B) {
b.SetBytes(int64(len(smallStructText)))
for i := 0; i < b.N; i++ {
var s Entities
if err := json.Unmarshal(smallStructText, &s); err != nil {
b.Error(err)
}
}
}
func BenchmarkStdjsonMarshal(b *testing.B) {
b.Run(strconv.Itoa(len(smallStructText)), func(b *testing.B) { benchmarkStdMarshalSmallData(b) })
b.Run(strconv.Itoa(len(largeStructText)), func(b *testing.B) { benchmarkStdMarshalLargeData(b) })
}
func benchmarkStdMarshalLargeData(b *testing.B) {
var l int64
for i := 0; i < b.N; i++ {
data, err := json.Marshal(&largeStructData)
if err != nil {
b.Error(err)
}
l = int64(len(data))
}
b.SetBytes(l)
}
func benchmarkStdMarshalSmallData(b *testing.B) {
var l int64
for i := 0; i < b.N; i++ {
data, err := json.Marshal(&smallStructData)
if err != nil {
b.Error(err)
}
l = int64(len(data))
}
b.SetBytes(l)
}