-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbench_test.go
71 lines (59 loc) · 1.71 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
package rejson
import (
"encoding/json"
"testing"
"github.com/tidwall/gjson"
)
const (
benchmarkJSON = `{"widget":{"debug":"on","window":{"title":"Sample Konfabulator Widget","name":"main_window","width":500,"height":500},"image":{"src":"Images/Sun.png","hOffset":250,"vOffset":250,"alignment":"center"},"text":{"data":"Click Here","size":36,"style":"bold","vOffset":100,"alignment":"center","onMouseUp":"sun1.opacity = (sun1.opacity / 100) * 90;"}}}`
)
type benchWidget struct {
Name string `rejson:"widget.window.name"`
HOffset int `rejson:"widget.image.hOffset"`
OnMouseUp string `rejson:"widget.text.onMouseUp"`
}
type benchStruct struct {
Widget struct {
Window struct {
Name string `json:"name"`
} `json:"window"`
Image struct {
HOffset int `json:"hOffset"`
} `json:"image"`
Text struct {
OnMouseUp string `json:"onMouseUp"`
} `json:"text"`
} `json:"widget"`
}
func BenchmarkUnmarshalReJSON(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
w := benchWidget{}
Unmarshal(benchmarkJSON, &w)
}
}
func BenchmarkUnmarshalGJSONGet(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
w := benchWidget{}
r := gjson.Parse(benchmarkJSON)
w.Name = r.Get("widget.window.name").String()
w.HOffset = int(r.Get("widget.image.hOffset").Int())
w.OnMouseUp = r.Get("widget.text.onMouseUp").String()
}
}
func BenchmarkUnmarshalEncodingJSON(b *testing.B) {
data := []byte(benchmarkJSON)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
bs := benchStruct{}
json.Unmarshal(data, &bs)
w := benchWidget{}
w.Name = bs.Widget.Window.Name
w.HOffset = bs.Widget.Image.HOffset
w.OnMouseUp = bs.Widget.Text.OnMouseUp
}
}