-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbalancer_test.go
131 lines (108 loc) · 2.63 KB
/
balancer_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
package consul
import (
"sort"
"testing"
)
var balancers = []struct {
name string
new func() Balancer
}{
{
name: "NullBalancer",
new: func() Balancer { return &NullBalancer{} },
},
{
name: "RoundRobin",
new: func() Balancer { return &RoundRobin{} },
},
{
name: "Rotator",
new: func() Balancer { return &Rotator{} },
},
{
name: "PreferTags(us-west-2a)",
new: func() Balancer { return PreferTags{"us-west-2a"} },
},
{
name: "PreferTags(us-west-2b)",
new: func() Balancer { return PreferTags{"us-west-2b"} },
},
{
name: "PreferTags(us-west-2a)+RoundRobin",
new: func() Balancer {
return MultiBalancer(PreferTags{"us-west-2a"}, &RoundRobin{})
},
},
{
name: "PreferTags(us-west-2b)+RoundRobin",
new: func() Balancer {
return MultiBalancer(PreferTags{"us-west-2b"}, &RoundRobin{})
},
},
{
name: "Shuffler",
new: func() Balancer { return &Shuffler{} },
},
{
name: "WeightedShufflerOnRTT",
new: func() Balancer { return &WeightedShuffler{WeightOf: WeightRTT} },
},
{
name: "LoadBlancer+RoundRobin",
new: func() Balancer { return &LoadBalancer{New: func() Balancer { return &RoundRobin{} }} },
},
}
func TestBalancer(t *testing.T) {
for _, balancer := range balancers {
t.Run(balancer.name, func(t *testing.T) {
testBalancer(t, balancer.new())
})
}
}
func testBalancer(t *testing.T, balancer Balancer) {
const endpointsCount = 30
const draws = 200
type counter struct {
index int
value int
}
// Ensure that the balance doesn't crash when it's given an empty list of
// endpoints.
balancer.Balance("empty-service", nil)
base := generateTestEndpoints(endpointsCount)
counters := make([]counter, endpointsCount)
for i := range counters {
counters[i].index = i
}
endpoints := make([]Endpoint, endpointsCount)
for i := 0; i != draws; i++ {
copy(endpoints, base)
balanced := balancer.Balance("test-service", endpoints)
for i := range base {
if balanced[0].ID == base[i].ID {
counters[i].value++
break
}
}
}
sort.Slice(counters, func(i int, j int) bool {
return counters[i].value > counters[j].value
})
for _, c := range counters {
endpoint := base[c.index]
t.Logf("ID = % s, RTT = % 5s, Tags = %s: % 3d\t(%g%%)", endpoint.ID, endpoint.RTT, endpoint.Tags, c.value, float64(c.value)*100.0/draws)
}
}
func BenchmarkBalancer(b *testing.B) {
for _, balancer := range balancers {
b.Run(balancer.name, func(b *testing.B) {
benchmarkBalancer(b, balancer.new())
})
}
}
func benchmarkBalancer(b *testing.B, balancer Balancer) {
endpoints := generateTestEndpoints(300)
for i := 0; i != b.N; i++ {
balancer.Balance("service-A", endpoints)
}
}