func BenchmarkRoundRobinRemove(b *testing.B) {
	for k := 0.; k <= 10; k++ {
		n := int(math.Pow(2, k))
		b.Run(fmt.Sprintf("remove/%d", n), func(b *testing.B) {
			b.ReportAllocs()
			balancer := NewRoundRobinBalancer()
			b.ResetTimer()
			for i := 0; i < b.N; i++ {
				b.StopTimer()
				records := make([]string, n)
				for index := 0; index < n; index++ {
					ip := IPv4Address(int64(index))
					records[index] = ip
					balancer.Add(testRoute(ip))
				}
				b.StartTimer()
				for index := 0; index < n; index++ {
					err := balancer.Remove(testRoute(records[index]))
					if err != nil {
						panic(err)
					}
				}
			}
		})
	}
}
Processor Speed: 2.8 GHz
Number of Processors: 1
Total Number of Cores: 4
L2 Cache (per Core): 256 KB
L3 Cache: 6 MB
Memory: 16 GB
Version: OS X 10.13.5

BenchmarkRoundRobinRemove/remove/1-8   	 1000000	      1206 ns/op	      48 B/op	       3 allocs/op
BenchmarkRoundRobinRemove/remove/2-8   	 1000000	      1685 ns/op	      96 B/op	       6 allocs/op
BenchmarkRoundRobinRemove/remove/4-8   	  500000	      2411 ns/op	     192 B/op	      12 allocs/op
BenchmarkRoundRobinRemove/remove/8-8   	  300000	      4299 ns/op	     384 B/op	      24 allocs/op
BenchmarkRoundRobinRemove/remove/16-8  	  200000	      9864 ns/op	     768 B/op	      48 allocs/op
BenchmarkRoundRobinRemove/remove/32-8  	   50000	     24964 ns/op	    1536 B/op	      96 allocs/op
BenchmarkRoundRobinRemove/remove/64-8  	   20000	     66353 ns/op	    3072 B/op	     192 allocs/op
BenchmarkRoundRobinRemove/remove/128-8 	   10000	    213464 ns/op	    6144 B/op	     384 allocs/op
BenchmarkRoundRobinRemove/remove/256-8 	    2000	    737753 ns/op	   12288 B/op	     768 allocs/op
BenchmarkRoundRobinRemove/remove/512-8 	     500	   2773625 ns/op	   24576 B/op	    1536 allocs/op
BenchmarkRoundRobinRemove/remove/1024-8      100	  10768748 ns/op	   49152 B/op	    3072 allocs/op
package xresolver
import (
	"errors"
	"fmt"
	"github.com/stretchr/testify/assert"
	"math"
	"strconv"
	"testing"
)
func testRoute(ip string) Route {
	return Route{
		Host:   ip,
		Scheme: "http",
	}
}
func TestRoundRobinOperations(t *testing.T) {
	assert := assert.New(t)
	balancer := NewRoundRobinBalancer()
	expected := testRoute("127.0.0.1")
	records, err := balancer.Get()
	assert.Error(err)
	assert.Empty(records)
	err = balancer.Remove(expected)
	assert.Error(err)
	err = balancer.Add(expected)
	assert.NoError(err)
	err = balancer.Add(expected)
	assert.Error(err)
	records, err = balancer.Get()
	assert.NoError(err)
	assert.Equal(1, len(records))
	assert.Equal(expected, records[0])
	err = balancer.Remove(expected)
	assert.NoError(err)
	records, err = balancer.Get()
	assert.Error(err)
	assert.Empty(records)
	balancer.Update([]Route{
		expected,
		testRoute("127.0.0.1"),
		testRoute("8.8.8.8"),
		testRoute("1.1.1.1"),
		testRoute("127.0.0.1"),
	})
	records, err = balancer.Get()
	assert.NoError(err)
	assert.Equal(3, len(records))
	assert.Equal([]Route{
		expected,
		testRoute("8.8.8.8"),
		testRoute("1.1.1.1"),
	}, records)
}
func TestRoundRobinOrder(t *testing.T) {
	assert := assert.New(t)
	balancer := NewRoundRobinBalancer()
	localAddress := testRoute("127.0.0.1")
	googleAddres := testRoute("8.8.8.8")
	addressOnes := testRoute("1.1.1.1")
	balancer.Add(localAddress)
	balancer.Add(googleAddres)
	balancer.Add(addressOnes)
	records, err := balancer.Get()
	assert.NoError(err)
	assert.Equal([]Route{localAddress, googleAddres, addressOnes}, records, "records are assumed to be the order in which added")
	records, err = balancer.Get()
	assert.NoError(err)
	assert.Equal([]Route{googleAddres, addressOnes, localAddress}, records, "records should rotate on another Get()")
	records, err = balancer.Get()
	assert.NoError(err)
	assert.Equal([]Route{addressOnes, localAddress, googleAddres}, records, "records should rotate on another Get()")
	err = balancer.Remove(googleAddres)
	assert.NoError(err)
	records, err = balancer.Get()
	assert.NoError(err)
	assert.Equal([]Route{localAddress, addressOnes}, records, "records should rotate on another Get()")
}
func BenchmarkRoundRobinAdd(b *testing.B) {
	for k := 0.; k <= 10; k++ {
		n := int(math.Pow(2, k))
		b.Run(fmt.Sprintf("add/%d", n), func(b *testing.B) {
			balancer := NewRoundRobinBalancer()
			b.ReportAllocs()
			b.ResetTimer()
			for i := 0; i < b.N; i++ {
				for index := 0; index < n; index++ {
					balancer.Add(testRoute(IPv4Address(int64(index))))
				}
			}
		})
	}
}