From 99b55652462782bab431e479e349991c13486009 Mon Sep 17 00:00:00 2001 From: dravenk Date: Mon, 18 Mar 2019 22:51:52 +0800 Subject: [PATCH 1/4] :zap: Improving performance. Improving of method gcb --- roundrobin_weighted.go | 12 +++--------- roundrobin_weighted_test.go | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/roundrobin_weighted.go b/roundrobin_weighted.go index 4213acf..498658c 100644 --- a/roundrobin_weighted.go +++ b/roundrobin_weighted.go @@ -93,14 +93,8 @@ func (w *RRW) Next() interface{} { } func gcd(x, y int) int { - var t int - for { - t = (x % y) - if t > 0 { - x = y - y = t - } else { - return y - } + for y != 0 { + x, y = y, x%y } + return x } diff --git a/roundrobin_weighted_test.go b/roundrobin_weighted_test.go index 0f156f4..84ce4ec 100644 --- a/roundrobin_weighted_test.go +++ b/roundrobin_weighted_test.go @@ -47,3 +47,23 @@ func TestRRW_Next(t *testing.T) { t.Error("the algorithm is wrong", results) } } + +func TestGCB(t *testing.T) { + tests := []struct { + name string + args [2]int + want int + }{ + {"0,0", [2]int{0, 0}, 0}, + {"1997,615", [2]int{1997, 6150}, 1}, + {"481,221", [2]int{481, 221}, 13}, + {"12,18", [2]int{12, 18}, 6}, + } + for _, tt := range tests { + n1 := tt.args[0] + n2 := tt.args[1] + if got := gcd(n1, n2); got != tt.want { + t.Errorf("gcb(%v, %v) = %v ; want = %v", n1, n2, got, tt.want) + } + } +} From 7c6b7a24e10f3fa71d40d68a558fe23be4020ff0 Mon Sep 17 00:00:00 2001 From: dravenk Date: Tue, 19 Mar 2019 00:16:16 +0800 Subject: [PATCH 2/4] :fire: Removing code or files. --- smooth_weighted.go | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/smooth_weighted.go b/smooth_weighted.go index 0633c46..cbb8bcb 100644 --- a/smooth_weighted.go +++ b/smooth_weighted.go @@ -66,23 +66,14 @@ func (w *SW) All() map[interface{}]int { // Next returns next selected server. func (w *SW) Next() interface{} { - i := w.nextWeighted() - if i == nil { + switch w.n { + case 0: return nil + case 1: + return w.items[0].Item + default: + return nextSmoothWeighted(w.items).Item } - return i.Item -} - -// nextWeighted returns next selected weighted object. -func (w *SW) nextWeighted() *smoothWeighted { - if w.n == 0 { - return nil - } - if w.n == 1 { - return w.items[0] - } - - return nextSmoothWeighted(w.items) } //https://github.com/phusion/nginx/commit/27e94984486058d73157038f7950a0a36ecc6e35 @@ -92,10 +83,6 @@ func nextSmoothWeighted(items []*smoothWeighted) (best *smoothWeighted) { for i := 0; i < len(items); i++ { w := items[i] - if w == nil { - continue - } - w.CurrentWeight += w.EffectiveWeight total += w.EffectiveWeight if w.EffectiveWeight < w.Weight { From 92dc122695aca118806d1fb3532afd6d99163c91 Mon Sep 17 00:00:00 2001 From: dravenk Date: Tue, 19 Mar 2019 00:48:51 +0800 Subject: [PATCH 3/4] :white_check_mark: Updating tests. --- smooth_weighted_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/smooth_weighted_test.go b/smooth_weighted_test.go index ee288e8..317dfb1 100644 --- a/smooth_weighted_test.go +++ b/smooth_weighted_test.go @@ -31,6 +31,19 @@ func TestSW_Next(t *testing.T) { t.Error("the algorithm is wrong") } + all := w.All() + countOK := 0 + for index := range all { + if (index == "server1" && all[index] == 5) || + (index == "server2" && all[index] == 2) || + (index == "server3" && all[index] == 3) { + countOK++ + } + } + if countOK != 3 { + t.Error("the algorithm is wrong") + } + w.RemoveAll() w.Add("server1", 7) w.Add("server2", 9) From 263d5faf821ece11e6dfcc86cc6b3c3b4f9cc220 Mon Sep 17 00:00:00 2001 From: dravenk Date: Tue, 19 Mar 2019 01:41:17 +0800 Subject: [PATCH 4/4] :white_check_mark: Updating tests. --- random_weighted.go | 2 ++ random_weighted_test.go | 24 ++++++++++++++++++++++++ roundrobin_weighted_test.go | 24 ++++++++++++++++++++++++ smooth_weighted_test.go | 11 +++++++++++ 4 files changed, 61 insertions(+) diff --git a/random_weighted.go b/random_weighted.go index 61ee717..d5802a8 100644 --- a/random_weighted.go +++ b/random_weighted.go @@ -62,6 +62,8 @@ func (rw *RandW) All() map[interface{}]int { func (rw *RandW) RemoveAll() { rw.items = make([]*randWeighted, 0) rw.r = rand.New(rand.NewSource(time.Now().Unix())) + rw.sumOfWeights = 0 + rw.n = 0 } // Reset resets the balancing algorithm. diff --git a/random_weighted_test.go b/random_weighted_test.go index 5b4d51a..d161b08 100644 --- a/random_weighted_test.go +++ b/random_weighted_test.go @@ -31,6 +31,19 @@ func TestRandW_Next(t *testing.T) { t.Error("the algorithm is wrong", results) } + all := w.All() + countOK := 0 + for index := range all { + if (index == "server1" && all[index] == 5) || + (index == "server2" && all[index] == 2) || + (index == "server3" && all[index] == 3) { + countOK++ + } + } + if countOK != 3 { + t.Error("the algorithm is wrong") + } + w.RemoveAll() w.Add("server1", 7) w.Add("server2", 9) @@ -48,6 +61,17 @@ func TestRandW_Next(t *testing.T) { // } t.Log("the results: ", results) + + w.RemoveAll() + next := w.Next() + if next != nil { + t.Error("the algorithm is wrong") + } + w.Add("server1", 3) + next = w.Next() + if next == nil { + t.Error("the algorithm is wrong") + } } func checkResults(v, min, max int) bool { diff --git a/roundrobin_weighted_test.go b/roundrobin_weighted_test.go index 84ce4ec..cecc952 100644 --- a/roundrobin_weighted_test.go +++ b/roundrobin_weighted_test.go @@ -31,6 +31,19 @@ func TestRRW_Next(t *testing.T) { t.Error("the algorithm is wrong", results) } + all := w.All() + countOK := 0 + for index := range all { + if (index == "server1" && all[index] == 5) || + (index == "server2" && all[index] == 2) || + (index == "server3" && all[index] == 3) { + countOK++ + } + } + if countOK != 3 { + t.Error("the algorithm is wrong") + } + w.RemoveAll() w.Add("server1", 7) w.Add("server2", 9) @@ -46,6 +59,17 @@ func TestRRW_Next(t *testing.T) { if results["server1"] != 7000 || results["server2"] != 9000 || results["server3"] != 13000 { t.Error("the algorithm is wrong", results) } + + w.RemoveAll() + next := w.Next() + if next != nil { + t.Error("the algorithm is wrong") + } + w.Add("server1", 3) + next = w.Next() + if next == nil { + t.Error("the algorithm is wrong") + } } func TestGCB(t *testing.T) { diff --git a/smooth_weighted_test.go b/smooth_weighted_test.go index 317dfb1..2f9d2c0 100644 --- a/smooth_weighted_test.go +++ b/smooth_weighted_test.go @@ -59,4 +59,15 @@ func TestSW_Next(t *testing.T) { if results["server1"] != 7000 || results["server2"] != 9000 || results["server3"] != 13000 { t.Error("the algorithm is wrong") } + + w.RemoveAll() + next := w.Next() + if next != nil { + t.Error("the algorithm is wrong") + } + w.Add("server1", 3) + next = w.Next() + if next == nil { + t.Error("the algorithm is wrong") + } }