@@ -38,3 +38,59 @@ func TestFactorConnCount(t *testing.T) {
3838 require .Equal (t , test .expectedScore , backends [i ].score (), "test idx: %d" , i )
3939 }
4040}
41+
42+ func TestFactorConnSpeed (t * testing.T ) {
43+ tests := []struct {
44+ score1 int
45+ score2 int
46+ targetRange [2 ]int
47+ }{
48+ {
49+ score1 : 120 ,
50+ score2 : 100 ,
51+ targetRange : [2 ]int {100 , 100 },
52+ },
53+ {
54+ score1 : 150 ,
55+ score2 : 100 ,
56+ targetRange : [2 ]int {110 , 114 },
57+ },
58+ {
59+ score1 : 10000 ,
60+ score2 : 0 ,
61+ targetRange : [2 ]int {3500 , 4550 },
62+ },
63+ }
64+
65+ factor := NewFactorConnCount ()
66+ backend1 := newMockBackend (true , 0 )
67+ backend2 := newMockBackend (true , 0 )
68+ scoredBackend1 := newScoredBackend (backend1 , zap .NewNop ())
69+ scoredBackend2 := newScoredBackend (backend2 , zap .NewNop ())
70+ for i , test := range tests {
71+ backend1 .connScore = test .score1
72+ backend2 .connScore = test .score2
73+ lastRedirectTime := 0
74+ // Simulate rebalance for 5 minutes.
75+ for j := 0 ; j < 30000 ; j ++ {
76+ balanceCount , _ := factor .BalanceCount (scoredBackend1 , scoredBackend2 )
77+ if balanceCount < 0.0001 {
78+ break
79+ }
80+ migrationInterval := 100 / balanceCount
81+ count := 0
82+ if migrationInterval < 2 {
83+ count = int (1 / migrationInterval )
84+ } else if float64 (j - lastRedirectTime ) >= migrationInterval {
85+ count = 1
86+ }
87+ if count > 0 {
88+ lastRedirectTime = j
89+ backend1 .connScore -= count
90+ backend2 .connScore += count
91+ }
92+ }
93+ require .GreaterOrEqual (t , backend2 .connScore , test .targetRange [0 ], "case id: %d" , i )
94+ require .LessOrEqual (t , backend2 .connScore , test .targetRange [1 ], "case id: %d" , i )
95+ }
96+ }
0 commit comments