Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ Improving performance. #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions random_weighted.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions random_weighted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
12 changes: 3 additions & 9 deletions roundrobin_weighted.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
44 changes: 44 additions & 0 deletions roundrobin_weighted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -46,4 +59,35 @@ 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) {
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)
}
}
}
25 changes: 6 additions & 19 deletions smooth_weighted.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,14 @@ func (w *SW) All() map[interface{}]int {

// Next returns next selected server.
func (w *SW) Next() interface{} {
i := w.nextWeighted()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if w.n == 0; just return; No additional method calls are required.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why removed this line?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because can be return nextSmoothWeighted(w.items).Item
look blow:

// Next returns next selected server.
func (w *SW) Next() interface{} {
	switch w.n {
	case 0:
		return nil
	case 1:
		return w.items[0].Item
	default:
		return nextSmoothWeighted(w.items).Item
	}
}

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 {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more concise to make these judgments in next.

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
Expand All @@ -92,10 +83,6 @@ func nextSmoothWeighted(items []*smoothWeighted) (best *smoothWeighted) {
for i := 0; i < len(items); i++ {
w := items[i]

if w == nil {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the parameters call into the method nextSmoothWeighted() always is greater than 1. Another way, whether it is len(items) =0; i !< 0; len(items) > 1 , items[i] != nil; So, those code never gets executed.

continue
}

w.CurrentWeight += w.EffectiveWeight
total += w.EffectiveWeight
if w.EffectiveWeight < w.Weight {
Expand Down
24 changes: 24 additions & 0 deletions smooth_weighted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -46,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")
}
}