forked from hailocab/go-hostpool
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhostpool_test.go
265 lines (214 loc) · 6.52 KB
/
hostpool_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package hostpool
import (
"errors"
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestHostPool(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
dummyErr := errors.New("Dummy Error")
p := New([]string{"a", "b", "c"})
assert.Equal(t, p.Get().Host(), "a")
assert.Equal(t, p.Get().Host(), "b")
assert.Equal(t, p.Get().Host(), "c")
respA := p.Get()
assert.Equal(t, respA.Host(), "a")
respA.Mark(dummyErr)
respB := p.Get()
respB.Mark(dummyErr)
respC := p.Get()
assert.Equal(t, respC.Host(), "c")
respC.Mark(nil)
// get again, and verify that it's still c
assert.Equal(t, p.Get().Host(), "c")
// now try to mark b as success; should fail because already marked
respB.Mark(nil)
assert.Equal(t, p.Get().Host(), "c") // would be b if it were not dead
// now restore a
respA = &standardHostPoolResponse{host: "a", pool: p}
respA.Mark(nil)
assert.Equal(t, p.Get().Host(), "a")
assert.Equal(t, p.Get().Host(), "c")
// ensure that we get *something* back when all hosts fail
for _, host := range []string{"a", "b", "c"} {
response := &standardHostPoolResponse{host: host, pool: p}
response.Mark(dummyErr)
}
resp := p.Get()
assert.NotEqual(t, resp, nil)
}
type mockTimer struct {
t int // the time it will always return
}
func (t *mockTimer) between(start time.Time, end time.Time) time.Duration {
return time.Duration(t.t) * time.Millisecond
}
func TestEpsilonGreedy(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
rand.Seed(10)
iterations := 12000
p := NewEpsilonGreedy([]string{"a", "b"}, 0, &LinearEpsilonValueCalculator{}).(*epsilonGreedyHostPool)
timings := make(map[string]int64)
timings["a"] = 200
timings["b"] = 300
hitCounts := make(map[string]int)
hitCounts["a"] = 0
hitCounts["b"] = 0
log.Printf("starting first run (a, b)")
for i := 0; i < iterations; i += 1 {
if i != 0 && i%100 == 0 {
p.performEpsilonGreedyDecay()
}
hostR := p.Get()
host := hostR.Host()
hitCounts[host]++
timing := timings[host]
p.timer = &mockTimer{t: int(timing)}
hostR.Mark(nil)
}
for host := range hitCounts {
log.Printf("host %s hit %d times (%0.2f percent)", host, hitCounts[host], (float64(hitCounts[host])/float64(iterations))*100.0)
}
assert.Equal(t, hitCounts["a"] > hitCounts["b"], true)
hitCounts["a"] = 0
hitCounts["b"] = 0
log.Printf("starting second run (b, a)")
timings["a"] = 500
timings["b"] = 100
for i := 0; i < iterations; i += 1 {
if i != 0 && i%100 == 0 {
p.performEpsilonGreedyDecay()
}
hostR := p.Get()
host := hostR.Host()
hitCounts[host]++
timing := timings[host]
p.timer = &mockTimer{t: int(timing)}
hostR.Mark(nil)
}
for host := range hitCounts {
log.Printf("host %s hit %d times (%0.2f percent)", host, hitCounts[host], (float64(hitCounts[host])/float64(iterations))*100.0)
}
assert.Equal(t, hitCounts["b"] > hitCounts["a"], true)
}
func BenchmarkEpsilonGreedy(b *testing.B) {
b.StopTimer()
// Make up some response times
zipfDist := rand.NewZipf(rand.New(rand.NewSource(0)), 1.1, 5, 5000)
timings := make([]uint64, b.N)
for i := 0; i < b.N; i++ {
timings[i] = zipfDist.Uint64()
}
// Make the hostpool with a few hosts
p := NewEpsilonGreedy([]string{"a", "b"}, 0, &LinearEpsilonValueCalculator{}).(*epsilonGreedyHostPool)
b.StartTimer()
for i := 0; i < b.N; i++ {
if i != 0 && i%100 == 0 {
p.performEpsilonGreedyDecay()
}
hostR := p.Get()
p.timer = &mockTimer{t: int(timings[i])}
hostR.Mark(nil)
}
}
func BenchmarkEpsilonGreedyManyHosts(topB *testing.B) {
bench := func(hostCount int, enableDecay bool) func(*testing.B) {
return func(b *testing.B) {
b.StopTimer()
// Make up some response times
zipfDist := rand.NewZipf(rand.New(rand.NewSource(0)), 1.1, 5, 5000)
timings := make([]uint64, b.N)
for i := 0; i < b.N; i++ {
timings[i] = zipfDist.Uint64()
}
// Make the hostpool with a few hosts
hosts := make([]string, 0, hostCount)
for i := 0; i < hostCount; i++ {
hosts = append(hosts, fmt.Sprintf("%d", i))
}
p := NewEpsilonGreedy(hosts, 0, &LinearEpsilonValueCalculator{}).(*epsilonGreedyHostPool)
b.StartTimer()
for i := 0; i < b.N; i++ {
if enableDecay && i != 0 && i%100 == 0 {
p.performEpsilonGreedyDecay()
}
hostR := p.Get()
p.timer = &mockTimer{t: int(timings[i])}
hostR.Mark(nil)
}
}
}
topB.Run("Hosts10/NoDecay", bench(10, false))
topB.Run("Hosts25/NoDecay", bench(25, false))
topB.Run("Hosts50/NoDecay", bench(50, false))
topB.Run("Hosts100/NoDecay", bench(100, false))
topB.Run("Hosts250/NoDecay", bench(250, false))
topB.Run("Hosts10/WithDecay", bench(10, true))
topB.Run("Hosts25/WithDecay", bench(25, true))
topB.Run("Hosts50/WithDecay", bench(50, true))
topB.Run("Hosts100/WithDecay", bench(100, true))
topB.Run("Hosts250/WithDecay", bench(250, true))
}
func TestHostPoolErrorBudget(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
dummyErr := errors.New("Dummy Error")
p := NewWithOptions([]string{"a", "b"}, StandardHostPoolOptions{
MaxFailures: 2,
FailureWindow: 60 * time.Second,
})
// Initially both hosts are available.
assert.Equal(t, p.Get().Host(), "a")
assert.Equal(t, p.Get().Host(), "b")
// Mark an error against a.
respA := p.Get()
assert.Equal(t, respA.Host(), "a")
respA.Mark(dummyErr)
assert.Equal(t, p.Get().Host(), "b")
// a should still be available (second failure)
respA = p.Get()
assert.Equal(t, respA.Host(), "a")
respA.Mark(dummyErr)
assert.Equal(t, p.Get().Host(), "b")
respA = p.Get()
// a should be marked as down (third failure)
assert.Equal(t, respA.Host(), "a")
respA.Mark(dummyErr)
// Host a should not be available.
assert.Equal(t, p.Get().Host(), "b")
}
func TestHostPoolErrorBudgetReset(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
dummyErr := errors.New("Dummy Error")
p := NewWithOptions([]string{"a", "b"}, StandardHostPoolOptions{
MaxFailures: 1,
FailureWindow: 1 * time.Second,
})
// Initially both hosts are available
assert.Equal(t, p.Get().Host(), "a")
assert.Equal(t, p.Get().Host(), "b")
// Mark an error against a
respA := p.Get()
assert.Equal(t, respA.Host(), "a")
respA.Mark(dummyErr)
// Fetch next host
assert.Equal(t, p.Get().Host(), "b")
// Ensure failure window is exceeded
time.Sleep(time.Second * 2)
// Mark another error
respA = p.Get()
assert.Equal(t, respA.Host(), "a")
respA.Mark(dummyErr)
assert.Equal(t, p.Get().Host(), "b")
// a should still be available
assert.Equal(t, p.Get().Host(), "a")
}