forked from smallnest/weighted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmooth_weighted_test.go
127 lines (107 loc) · 2.85 KB
/
smooth_weighted_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
package weighted
import (
"strconv"
"testing"
"time"
)
func TestSW_Next(t *testing.T) {
w := &SW{}
w.Add("server1", 5)
w.Add("server2", 2)
w.Add("server3", 3)
results := make(map[string]int)
for i := 0; i < 100; i++ {
s := w.Next().(string)
results[s]++
}
if results["server1"] != 50 || results["server2"] != 20 || results["server3"] != 30 {
t.Error("the algorithm is wrong")
}
w.Reset()
results = make(map[string]int)
for i := 0; i < 100; i++ {
s := w.Next().(string)
results[s]++
}
if results["server1"] != 50 || results["server2"] != 20 || results["server3"] != 30 {
t.Error("the algorithm is wrong")
}
w.RemoveAll()
w.Add("server1", 7)
w.Add("server2", 9)
w.Add("server3", 13)
results = make(map[string]int)
for i := 0; i < 29000; i++ {
s := w.Next().(string)
results[s]++
}
if results["server1"] != 7000 || results["server2"] != 9000 || results["server3"] != 13000 {
t.Error("the algorithm is wrong")
}
}
func TestSW_NextWithCallback(t *testing.T) {
const forcount = 999
// positive: weight more, cost less
var doFuncPositive = func(input string) int {
index, _ := strconv.Atoi(input[len(input)-1:])
index = 10 - index
// time.Sleep(time.Duration(index) * time.Microsecond)
return index
}
// negative: weight more, cost more
var doFuncNegative = func(input string) int {
index, _ := strconv.Atoi(input[len(input)-1:])
time.Sleep(time.Duration(index) * time.Microsecond)
return index
}
// with no callback for dynamic adjustment
w := &SW{}
w.Add("server5", 100)
w.Add("server2", 100)
w.Add("server3", 100)
results := make(map[string]int)
for i := 0; i < forcount; i++ {
so, _ := w.NextWithCallback()
s, _ := so.(string)
results[s]++
_ = doFuncPositive(s)
}
t.Log("non-callback", w.All(), results)
if !(results["server5"] == results["server3"] && results["server3"] == results["server2"]) {
t.Error("the algorithm is wrong")
}
// with positive callback
w2 := &SW{}
w2.Add("server5", 100)
w2.Add("server2", 100)
w2.Add("server3", 100)
results2 := make(map[string]int)
for i := 0; i < forcount; i++ {
so, f := w2.NextWithCallback()
s, _ := so.(string)
results2[s]++
callback := doFuncPositive(s)
f(1000 / callback)
}
t.Log("positive-callback", w2.All(), results2)
if !(results2["server5"] > results2["server3"] && results2["server3"] > results2["server2"]) {
t.Error("the algorithm is wrong")
}
// with negative callback
w3 := &SW{}
w3.Add("server5", 100)
w3.Add("server2", 100)
w3.Add("server3", 100)
results3 := make(map[string]int)
for i := 0; i < forcount; i++ {
so, f := w3.NextWithCallback()
s, _ := so.(string)
results3[s]++
callback := doFuncNegative(s)
f(1000 / callback)
}
t.Log("negative-callback", w3.All(), results3)
if !(results3["server5"] < results3["server3"] && results3["server3"] < results3["server2"]) {
t.Error("the algorithm is wrong")
}
}