-
Notifications
You must be signed in to change notification settings - Fork 14
/
round_robin_test.go
119 lines (109 loc) · 2.43 KB
/
round_robin_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
package roundrobin
import (
"fmt"
"net/url"
"reflect"
"sync"
"testing"
)
func TestRoundRobin(t *testing.T) {
tests := []struct {
urls []*url.URL
iserr bool
expected []string
want []*url.URL
}{
{
urls: []*url.URL{
{Host: "192.168.33.10"},
{Host: "192.168.33.11"},
{Host: "192.168.33.12"},
},
iserr: false,
want: []*url.URL{
{Host: "192.168.33.10"},
{Host: "192.168.33.11"},
{Host: "192.168.33.12"},
{Host: "192.168.33.10"},
},
},
{
urls: []*url.URL{},
iserr: true,
want: []*url.URL{},
},
}
for i, test := range tests {
rr, err := New(test.urls...)
if got, want := !(err == nil), test.iserr; got != want {
t.Errorf("tests[%d] - RoundRobin iserr is wrong. want: %v, but got: %v", i, test.want, got)
}
gots := make([]*url.URL, 0, len(test.want))
for j := 0; j < len(test.want); j++ {
gots = append(gots, rr.Next())
}
if got, want := gots, test.want; !reflect.DeepEqual(got, want) {
t.Errorf("tests[%d] - RoundRobin is wrong. want: %v, got: %v", i, want, got)
}
}
}
func BenchmarkRoundRobinSync(b *testing.B) {
resources := []*url.URL{
{Host: "127.0.0.1"},
{Host: "127.0.0.2"},
{Host: "127.0.0.3"},
{Host: "127.0.0.4"},
{Host: "127.0.0.5"},
{Host: "127.0.0.6"},
{Host: "127.0.0.7"},
{Host: "127.0.0.8"},
{Host: "127.0.0.9"},
{Host: "127.0.0.10"},
}
for i := 1; i < len(resources)+1; i++ {
b.Run(fmt.Sprintf("RoundRobinSliceOfSize(%d)", i), func(b *testing.B) {
rr, err := New(resources[:i]...)
if err != nil {
b.Fatal(err)
}
// Adding WaitGroup complexity as this helps in comparing Sync and Async RoundRobinAccess (see BenchmarkRoundRobinASync as well)
wg := &sync.WaitGroup{}
for i := 0; i < b.N; i++ {
wg.Add(1)
defer wg.Done()
rr.Next()
}
})
}
}
func BenchmarkRoundRobinASync(b *testing.B) {
resources := []*url.URL{
{Host: "127.0.0.1"},
{Host: "127.0.0.2"},
{Host: "127.0.0.3"},
{Host: "127.0.0.4"},
{Host: "127.0.0.5"},
{Host: "127.0.0.6"},
{Host: "127.0.0.7"},
{Host: "127.0.0.8"},
{Host: "127.0.0.9"},
{Host: "127.0.0.10"},
}
for i := 1; i < len(resources)+1; i++ {
b.Run(fmt.Sprintf("RoundRobinSliceOfSize(%d)", i), func(b *testing.B) {
rr, err := New(resources[:i]...)
if err != nil {
b.Fatal(err)
}
wg := &sync.WaitGroup{}
for i := 0; i < b.N; i++ {
wg.Add(1)
go func() {
defer wg.Done()
rr.Next()
}()
}
wg.Wait()
})
}
}