-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_easyjson_test.go
172 lines (151 loc) · 4.82 KB
/
bench_easyjson_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
// go test -bench=. -benchmem -cpu 1,4,8 -benchtime 10s -count 1 -cpuprofile=./results/cpu.out -memprofile=./results/mem.out bench_test.go > ./results/old.txt
// go test -bench=. -benchmem -cpu 1,4,8 -benchtime 10s -count 1 -cpuprofile=./results/cpu.out -memprofile=./results/mem.out bench_test.go > ./results/new.txt
// Compare by golang.org/x/tools/cmd/benchcmp
// benchcmp -changed ./results/go1.11.txt ./results/go1.12.txt > ./results/cmp_1.11-1.12.txt
// benchcmp -changed ./results/go1.12.txt ./results/go1.13.txt > ./results/cmp_1.12-1.13.txt
// benchcmp -changed ./results/go1.13.txt ./results/go.tip.txt > ./results/cmp_1.13-tip.txt
// benchcmp -changed ./results/go1.11.txt ./results/go1.13.txt > ./results/cmp_1.11-1.13.txt
// benchcmp -changed ./results/go1.11.txt ./results/go.tip.txt > ./results/cmp_1.11-tip.txt
package jsonbench_test
import (
"fmt"
"log"
"runtime"
"testing"
"github.com/riftbit/protocol_benches/structs"
"github.com/riftbit/protocol_benches/vars"
"github.com/mailru/easyjson/jlexer"
"github.com/mailru/easyjson/jwriter"
)
var (
EasyJSONExampleJsonBytes []byte
EasyJSONExampleJsonMinBytes []byte
EasyJSONFilledEasyStruct structs.EasedJSON
)
func init() {
EasyJSONExampleJsonBytes = []byte(vars.ExampleJsonString)
EasyJSONExampleJsonMinBytes = []byte(vars.ExampleJsonMinString)
err := EasyJSONFilledEasyStruct.UnmarshalJSON(EasyJSONExampleJsonMinBytes)
if err != nil {
log.Fatal(err)
}
if EasyJSONFilledEasyStruct.Aliceblue != "#f0f8ff" {
log.Fatal(fmt.Errorf(`wrong data in Aliceblue - got "%s" but expected "#f0f8ff"`, EasyJSONFilledEasyStruct.Aliceblue))
}
runtime.GC()
}
// Benchmark_Marshal_easyjson ...
func Benchmark_easyjson_Marshal(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf, err := EasyJSONFilledEasyStruct.MarshalJSON()
if err != nil {
b.Fatal(err)
}
if len(buf) < 1000 {
b.Fatal(fmt.Errorf(`wrong size of buf - got %d`, len(buf)))
}
}
}
// Benchmark_Marshal_easyjson_lexer ...
func Benchmark_easyjson_Marshal_lexer(b *testing.B) {
w := jwriter.Writer{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
EasyJSONFilledEasyStruct.MarshalEasyJSON(&w)
buf := w.Buffer.BuildBytes()
if w.Error != nil {
b.Fatal(w.Error)
}
if len(buf) < 1000 {
b.Fatal(fmt.Errorf(`wrong size of buf - got %d`, len(buf)))
}
}
}
// Benchmark_Unmarshal_Full_Bytes_easyjson ...
func Benchmark_easyjson_Unmarshal_Full(b *testing.B) {
// b.SetBytes(int64(len(EJUExampleJsonBytes)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
var buf structs.EasedJSON
if err := buf.UnmarshalJSON(EasyJSONExampleJsonBytes); err != nil {
b.Fatal(err)
}
if buf.Aliceblue != "#f0f8ff" {
b.Fatal(fmt.Errorf(`wrong data in Aliceblue - got "%s" but expected "#f0f8ff"`, buf.Aliceblue))
}
}
}
// Benchmark_Unmarshal_Min_Bytes_easyjson ...
func Benchmark_easyjson_Unmarshal_Min(b *testing.B) {
// b.SetBytes(int64(len(EJUExampleJsonMinBytes)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
var buf structs.EasedJSON
err := buf.UnmarshalJSON(EasyJSONExampleJsonMinBytes)
if err != nil {
b.Fatal(err)
}
if buf.Aliceblue != "#f0f8ff" {
b.Fatal(fmt.Errorf(`wrong data in Aliceblue - got "%s" but expected "#f0f8ff"`, buf.Aliceblue))
}
}
}
// Benchmark_Unmarshal_Full_Bytes_easyjson ...
func Benchmark_easyjson_Unmarshal_Full_lexer(b *testing.B) {
// b.SetBytes(int64(len(EJUExampleJsonBytes)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
var buf structs.EasedJSON
r := jlexer.Lexer{Data: EasyJSONExampleJsonBytes}
buf.UnmarshalEasyJSON(&r)
if r.Error() != nil {
b.Fatal(r.Error())
}
if buf.Aliceblue != "#f0f8ff" {
b.Fatal(fmt.Errorf(`wrong data in Aliceblue - got "%s" but expected "#f0f8ff"`, buf.Aliceblue))
}
}
}
// Benchmark_Unmarshal_Min_Bytes_easyjson ...
func Benchmark_easyjson_Unmarshal_Min_lexer(b *testing.B) {
// b.SetBytes(int64(len(EJUExampleJsonMinBytes)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
var buf structs.EasedJSON
r := jlexer.Lexer{Data: EasyJSONExampleJsonMinBytes}
buf.UnmarshalEasyJSON(&r)
if r.Error() != nil {
b.Fatal(r.Error())
}
if buf.Aliceblue != "#f0f8ff" {
b.Fatal(fmt.Errorf(`wrong data in Aliceblue - got "%s" but expected "#f0f8ff"`, buf.Aliceblue))
}
}
}
/*
func Benchmark_Marshal_By_Lexer(b *testing.B) {
b.ResetTimer()
w := jwriter.Writer{}
for i := 0; i < b.N; i++ {
messageBaseJson.MarshalEasyJSON(&w)
}
}
*/
/*
func Benchmark_Marshal_By_Lexer(b *testing.B) {
b.ResetTimer()
w := jwriter.Writer{}
for i := 0; i < b.N; i++ {
messageBaseJson.MarshalEasyJSON(&w)
}
}
// MarshalEasyJSON supports easyjson.Marshaler interface
func (v Snippet) MarshalEasyJSON(w *jwriter.Writer) {
easyjson6a975c40EncodeGithubComRiftbitJsonBenches3(w, v)
}
// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
func (v *Snippet) UnmarshalEasyJSON(l *jlexer.Lexer) {
easyjson6a975c40DecodeGithubComRiftbitJsonBenches3(l, v)
}
*/