func loadBalanceFor(strategy string, b *testing.B) { pool := route.NewPool(2*time.Minute, "") total := 5 endpoints := make([]*route.Endpoint, 0) for i := 0; i < total; i++ { ip := fmt.Sprintf("10.0.1.%d", i) e := route.NewEndpoint("", ip, 60000, "", "", nil, -1, "", models.ModificationTag{}) endpoints = append(endpoints, e) pool.Put(e) } var lb route.EndpointIterator switch strategy { case "round-robin": lb = route.NewRoundRobin(pool, "") case "least-connection": lb = route.NewLeastConnection(pool, "") default: panic("invalid load balancing strategy") } for n := 0; n < b.N; n++ { loadBalance(lb) } }
}) Describe("Next", func() { It("performs round-robin through the endpoints", func() { e1 := route.NewEndpoint("", "1.2.3.4", 5678, "", "", nil, -1, "", modTag) e2 := route.NewEndpoint("", "5.6.7.8", 1234, "", "", nil, -1, "", modTag) e3 := route.NewEndpoint("", "1.2.7.8", 1234, "", "", nil, -1, "", modTag) endpoints := []*route.Endpoint{e1, e2, e3} for _, e := range endpoints { pool.Put(e) } counts := make([]int, len(endpoints)) iter := route.NewRoundRobin(pool, "") loops := 50 for i := 0; i < len(endpoints)*loops; i += 1 { n := iter.Next() for j, e := range endpoints { if e == n { counts[j]++ break } } } for i := 0; i < len(endpoints); i++ { Expect(counts[i]).To(Equal(loops)) }