-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_test.go
More file actions
148 lines (128 loc) · 3 KB
/
bench_test.go
File metadata and controls
148 lines (128 loc) · 3 KB
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
package dig_test
import (
"testing"
"github.com/bold-minds/dig"
)
var (
shallowData = map[string]any{
"name": "alice",
}
deepData = map[string]any{
"level1": map[string]any{
"level2": map[string]any{
"level3": map[string]any{
"level4": map[string]any{
"level5": "deep-value",
},
},
},
},
}
mixedData = map[string]any{
"users": []any{
map[string]any{"name": "alice", "role": "admin"},
map[string]any{"name": "bob", "role": "editor"},
map[string]any{"name": "carol", "role": "admin"},
},
}
// mapAnyAnyData exercises the map[any]any path, which pays the cost
// of the extra key-type whitelist switch. Benchmarked so regressions
// in that guard are caught alongside the hot string-map path.
mapAnyAnyData = map[any]any{
"name": "alice",
int(1): "one",
int64(2): "two",
float64(3.5): "three-point-five",
true: "yes",
}
)
func BenchmarkDig_Shallow(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, _ = dig.Dig[string](shallowData, "name")
}
}
func BenchmarkDig_Deep(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, _ = dig.Dig[string](deepData, "level1", "level2", "level3", "level4", "level5")
}
}
func BenchmarkDig_Mixed(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, _ = dig.Dig[string](mixedData, "users", 1, "name")
}
}
func BenchmarkDig_Miss(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, _ = dig.Dig[string](shallowData, "missing")
}
}
func BenchmarkDigOr_Hit(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = dig.DigOr(shallowData, "default", "name")
}
}
func BenchmarkDigOr_Miss(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = dig.DigOr(shallowData, "default", "missing")
}
}
func BenchmarkHas_Hit(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = dig.Has(shallowData, "name")
}
}
func BenchmarkHas_Miss(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = dig.Has(shallowData, "missing")
}
}
func BenchmarkAt_Hit(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, _ = dig.At(shallowData, "name")
}
}
func BenchmarkAt_Miss(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, _ = dig.At(shallowData, "missing")
}
}
func BenchmarkDig_MapAnyAny_StringKey(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, _ = dig.Dig[string](mapAnyAnyData, "name")
}
}
func BenchmarkDig_MapAnyAny_IntKey(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, _ = dig.Dig[string](mapAnyAnyData, int(1))
}
}
func BenchmarkDig_MapAnyAny_Miss(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, _ = dig.Dig[string](mapAnyAnyData, "missing")
}
}
// BenchmarkDig_MapAnyAny_NonWhitelistedKey measures the failure path
// through the whitelist switch. If a regression accidentally reflects
// on the key type (instead of using the compile-time type switch), the
// alloc count here will jump from 0 and catch it.
func BenchmarkDig_MapAnyAny_NonWhitelistedKey(b *testing.B) {
b.ReportAllocs()
type k struct{ ID int }
key := k{ID: 1}
for b.Loop() {
_, _ = dig.Dig[string](mapAnyAnyData, key)
}
}