github.com/fufuok/balancer@v1.0.0/benchmark_test.go (about) 1 package balancer 2 3 import ( 4 "math/rand" 5 "strconv" 6 "testing" 7 ) 8 9 const ( 10 numMin = 10 11 numMax = 1_000_000 12 ) 13 14 func BenchmarkBalancer(b *testing.B) { 15 for n := numMin; n <= numMax; n *= 10 { 16 b.Run("WRR-"+strconv.Itoa(n), func(b *testing.B) { 17 items := genItemsMap(n) 18 lb := NewWeightedRoundRobin(items) 19 for i := 0; i < 1000; i++ { 20 lb.Select() 21 } 22 b.ResetTimer() 23 for i := 0; i < b.N; i++ { 24 lb.Select() 25 } 26 }) 27 28 b.Run("SWRR-"+strconv.Itoa(n), func(b *testing.B) { 29 items := genItemsMap(n) 30 lb := NewSmoothWeightedRoundRobin(items) 31 for i := 0; i < 1000; i++ { 32 lb.Select() 33 } 34 b.ResetTimer() 35 for i := 0; i < b.N; i++ { 36 lb.Select() 37 } 38 }) 39 40 b.Run("WR-"+strconv.Itoa(n), func(b *testing.B) { 41 items := genItemsMap(n) 42 lb := NewWeightedRand(items) 43 for i := 0; i < 1000; i++ { 44 lb.Select() 45 } 46 b.ResetTimer() 47 for i := 0; i < b.N; i++ { 48 lb.Select() 49 } 50 }) 51 52 b.Run("Hash-"+strconv.Itoa(n), func(b *testing.B) { 53 items := genItemsSlice(n) 54 lb := NewConsistentHash(items) 55 for i := 0; i < 1000; i++ { 56 lb.Select() 57 } 58 b.ResetTimer() 59 for i := 0; i < b.N; i++ { 60 lb.Select("192.168.1.1") 61 } 62 }) 63 64 b.Run("RoundRobin-"+strconv.Itoa(n), func(b *testing.B) { 65 items := genItemsSlice(n) 66 lb := NewRoundRobin(items) 67 for i := 0; i < 1000; i++ { 68 lb.Select() 69 } 70 b.ResetTimer() 71 for i := 0; i < b.N; i++ { 72 lb.Select() 73 } 74 }) 75 76 b.Run("Random-"+strconv.Itoa(n), func(b *testing.B) { 77 items := genItemsSlice(n) 78 lb := NewRandom(items) 79 for i := 0; i < 1000; i++ { 80 lb.Select() 81 } 82 b.ResetTimer() 83 for i := 0; i < b.N; i++ { 84 lb.Select() 85 } 86 }) 87 } 88 } 89 90 func BenchmarkBalancerParallel(b *testing.B) { 91 for n := numMin; n <= numMax; n *= 10 { 92 b.Run("WRR-"+strconv.Itoa(n), func(b *testing.B) { 93 items := genItemsMap(n) 94 lb := NewWeightedRoundRobin(items) 95 for i := 0; i < 1000; i++ { 96 lb.Select() 97 } 98 b.ResetTimer() 99 b.RunParallel(func(pb *testing.PB) { 100 for pb.Next() { 101 lb.Select() 102 } 103 }) 104 }) 105 106 b.Run("SWRR-"+strconv.Itoa(n), func(b *testing.B) { 107 items := genItemsMap(n) 108 lb := NewSmoothWeightedRoundRobin(items) 109 for i := 0; i < 1000; i++ { 110 lb.Select() 111 } 112 b.ResetTimer() 113 b.RunParallel(func(pb *testing.PB) { 114 for pb.Next() { 115 lb.Select() 116 } 117 }) 118 }) 119 120 b.Run("WR-"+strconv.Itoa(n), func(b *testing.B) { 121 items := genItemsMap(n) 122 lb := NewWeightedRand(items) 123 for i := 0; i < 1000; i++ { 124 lb.Select() 125 } 126 b.ResetTimer() 127 b.RunParallel(func(pb *testing.PB) { 128 for pb.Next() { 129 lb.Select() 130 } 131 }) 132 }) 133 134 b.Run("Hash-"+strconv.Itoa(n), func(b *testing.B) { 135 items := genItemsSlice(n) 136 lb := NewConsistentHash(items) 137 for i := 0; i < 1000; i++ { 138 lb.Select() 139 } 140 b.ResetTimer() 141 b.RunParallel(func(pb *testing.PB) { 142 for pb.Next() { 143 lb.Select("192.168.1.1") 144 } 145 }) 146 }) 147 148 b.Run("RoundRobin-"+strconv.Itoa(n), func(b *testing.B) { 149 items := genItemsSlice(n) 150 lb := NewRoundRobin(items) 151 for i := 0; i < 1000; i++ { 152 lb.Select() 153 } 154 b.ResetTimer() 155 b.RunParallel(func(pb *testing.PB) { 156 for pb.Next() { 157 lb.Select() 158 } 159 }) 160 }) 161 162 b.Run("Random-"+strconv.Itoa(n), func(b *testing.B) { 163 items := genItemsSlice(n) 164 lb := NewRandom(items) 165 for i := 0; i < 1000; i++ { 166 lb.Select() 167 } 168 b.ResetTimer() 169 b.RunParallel(func(pb *testing.PB) { 170 for pb.Next() { 171 lb.Select() 172 } 173 }) 174 }) 175 } 176 } 177 178 func genItemsMap(n int) map[string]int { 179 items := make(map[string]int) 180 for i := 0; i < n; i++ { 181 items[strconv.Itoa(i)] = rand.Intn(20) 182 } 183 return items 184 } 185 186 func genItemsSlice(n int) (items []string) { 187 for i := 0; i < n; i++ { 188 items = append(items, strconv.Itoa(i)) 189 } 190 return 191 } 192 193 // go test -run=^$ -benchmem -benchtime=1s -count=1 -bench=. 194 // goos: linux 195 // goarch: amd64 196 // pkg: github.com/fufuok/balancer 197 // cpu: Intel(R) Xeon(R) Gold 6151 CPU @ 3.00GHz 198 // BenchmarkBalancer/WRR-10-4 37112553 30.34 ns/op 0 B/op 0 allocs/op 199 // BenchmarkBalancer/SWRR-10-4 38851680 30.39 ns/op 0 B/op 0 allocs/op 200 // BenchmarkBalancer/WR-10-4 36406916 33.15 ns/op 0 B/op 0 allocs/op 201 // BenchmarkBalancer/Hash-10-4 31506262 37.60 ns/op 0 B/op 0 allocs/op 202 // BenchmarkBalancer/RoundRobin-10-4 53076963 23.86 ns/op 0 B/op 0 allocs/op 203 // BenchmarkBalancer/Random-10-4 64582524 18.02 ns/op 0 B/op 0 allocs/op 204 // 205 // BenchmarkBalancer/WRR-100-4 32221255 37.31 ns/op 0 B/op 0 allocs/op 206 // BenchmarkBalancer/SWRR-100-4 7337542 165.6 ns/op 0 B/op 0 allocs/op 207 // BenchmarkBalancer/WR-100-4 21253034 53.29 ns/op 0 B/op 0 allocs/op 208 // BenchmarkBalancer/Hash-100-4 25851721 46.58 ns/op 0 B/op 0 allocs/op 209 // BenchmarkBalancer/RoundRobin-100-4 51670482 22.59 ns/op 0 B/op 0 allocs/op 210 // BenchmarkBalancer/Random-100-4 66175606 18.14 ns/op 0 B/op 0 allocs/op 211 // 212 // BenchmarkBalancer/WRR-1000-4 28502208 42.09 ns/op 0 B/op 0 allocs/op 213 // BenchmarkBalancer/SWRR-1000-4 872499 1391 ns/op 0 B/op 0 allocs/op 214 // BenchmarkBalancer/WR-1000-4 16595787 71.57 ns/op 0 B/op 0 allocs/op 215 // BenchmarkBalancer/Hash-1000-4 19103568 63.47 ns/op 0 B/op 0 allocs/op 216 // BenchmarkBalancer/RoundRobin-1000-4 52725135 23.05 ns/op 0 B/op 0 allocs/op 217 // BenchmarkBalancer/Random-1000-4 66541184 18.24 ns/op 0 B/op 0 allocs/op 218 // 219 // BenchmarkBalancer/WRR-10000-4 27912939 42.67 ns/op 0 B/op 0 allocs/op 220 // BenchmarkBalancer/SWRR-10000-4 86983 14019 ns/op 0 B/op 0 allocs/op 221 // BenchmarkBalancer/WR-10000-4 12691062 92.73 ns/op 0 B/op 0 allocs/op 222 // BenchmarkBalancer/Hash-10000-4 16084016 73.96 ns/op 0 B/op 0 allocs/op 223 // BenchmarkBalancer/RoundRobin-10000-4 52327888 24.05 ns/op 0 B/op 0 allocs/op 224 // BenchmarkBalancer/Random-10000-4 66457050 18.17 ns/op 0 B/op 0 allocs/op 225 // 226 // BenchmarkBalancer/WRR-100000-4 24896972 43.20 ns/op 0 B/op 0 allocs/op 227 // BenchmarkBalancer/SWRR-100000-4 7046 173884 ns/op 0 B/op 0 allocs/op 228 // BenchmarkBalancer/WR-100000-4 9491140 127.3 ns/op 0 B/op 0 allocs/op 229 // BenchmarkBalancer/Hash-100000-4 16090567 76.46 ns/op 0 B/op 0 allocs/op 230 // BenchmarkBalancer/RoundRobin-100000-4 49422337 24.06 ns/op 0 B/op 0 allocs/op 231 // BenchmarkBalancer/Random-100000-4 62700792 18.52 ns/op 0 B/op 0 allocs/op 232 // 233 // BenchmarkBalancer/WRR-1000000-4 24038544 47.01 ns/op 0 B/op 0 allocs/op 234 // BenchmarkBalancer/SWRR-1000000-4 381 3108476 ns/op 0 B/op 0 allocs/op 235 // BenchmarkBalancer/WR-1000000-4 4863207 259.8 ns/op 0 B/op 0 allocs/op 236 // BenchmarkBalancer/Hash-1000000-4 16194163 74.15 ns/op 0 B/op 0 allocs/op 237 // BenchmarkBalancer/RoundRobin-1000000-4 52971156 22.70 ns/op 0 B/op 0 allocs/op 238 // BenchmarkBalancer/Random-1000000-4 40018782 26.25 ns/op 0 B/op 0 allocs/op 239 // 240 // BenchmarkBalancerParallel/WRR-10-4 12663168 92.42 ns/op 0 B/op 0 allocs/op 241 // BenchmarkBalancerParallel/SWRR-10-4 12709807 94.45 ns/op 0 B/op 0 allocs/op 242 // BenchmarkBalancerParallel/WR-10-4 28055660 68.66 ns/op 0 B/op 0 allocs/op 243 // BenchmarkBalancerParallel/Hash-10-4 16188872 72.30 ns/op 0 B/op 0 allocs/op 244 // BenchmarkBalancerParallel/RoundRobin-10-4 13612167 91.14 ns/op 0 B/op 0 allocs/op 245 // BenchmarkBalancerParallel/Random-10-4 17698065 67.72 ns/op 0 B/op 0 allocs/op 246 // 247 // BenchmarkBalancerParallel/WRR-100-4 13133222 88.99 ns/op 0 B/op 0 allocs/op 248 // BenchmarkBalancerParallel/SWRR-100-4 6563328 181.4 ns/op 0 B/op 0 allocs/op 249 // BenchmarkBalancerParallel/WR-100-4 26578320 79.94 ns/op 0 B/op 0 allocs/op 250 // BenchmarkBalancerParallel/Hash-100-4 16083211 74.67 ns/op 0 B/op 0 allocs/op 251 // BenchmarkBalancerParallel/RoundRobin-100-4 12263182 91.64 ns/op 0 B/op 0 allocs/op 252 // BenchmarkBalancerParallel/Random-100-4 17741816 67.81 ns/op 0 B/op 0 allocs/op 253 // 254 // BenchmarkBalancerParallel/WRR-1000-4 11212780 104.0 ns/op 0 B/op 0 allocs/op 255 // BenchmarkBalancerParallel/SWRR-1000-4 805879 1474 ns/op 0 B/op 0 allocs/op 256 // BenchmarkBalancerParallel/WR-1000-4 15821539 72.37 ns/op 0 B/op 0 allocs/op 257 // BenchmarkBalancerParallel/Hash-1000-4 14478384 81.82 ns/op 0 B/op 0 allocs/op 258 // BenchmarkBalancerParallel/RoundRobin-1000-4 12103447 88.15 ns/op 0 B/op 0 allocs/op 259 // BenchmarkBalancerParallel/Random-1000-4 17729145 67.81 ns/op 0 B/op 0 allocs/op 260 // 261 // BenchmarkBalancerParallel/WRR-10000-4 10567130 105.9 ns/op 0 B/op 0 allocs/op 262 // BenchmarkBalancerParallel/SWRR-10000-4 81170 14685 ns/op 0 B/op 0 allocs/op 263 // BenchmarkBalancerParallel/WR-10000-4 14379578 78.16 ns/op 0 B/op 0 allocs/op 264 // BenchmarkBalancerParallel/Hash-10000-4 14215629 84.50 ns/op 0 B/op 0 allocs/op 265 // BenchmarkBalancerParallel/RoundRobin-10000-4 13372892 90.45 ns/op 0 B/op 0 allocs/op 266 // BenchmarkBalancerParallel/Random-10000-4 17676268 67.92 ns/op 0 B/op 0 allocs/op 267 // 268 // BenchmarkBalancerParallel/WRR-100000-4 11561236 110.4 ns/op 0 B/op 0 allocs/op 269 // BenchmarkBalancerParallel/SWRR-100000-4 6835 175792 ns/op 0 B/op 0 allocs/op 270 // BenchmarkBalancerParallel/WR-100000-4 22145756 54.60 ns/op 0 B/op 0 allocs/op 271 // BenchmarkBalancerParallel/Hash-100000-4 14285690 84.04 ns/op 0 B/op 0 allocs/op 272 // BenchmarkBalancerParallel/RoundRobin-100000-4 12744205 90.57 ns/op 0 B/op 0 allocs/op 273 // BenchmarkBalancerParallel/Random-100000-4 17859376 67.19 ns/op 0 B/op 0 allocs/op 274 // 275 // BenchmarkBalancerParallel/WRR-1000000-4 11473530 109.0 ns/op 0 B/op 0 allocs/op 276 // BenchmarkBalancerParallel/SWRR-1000000-4 379 3127391 ns/op 7 B/op 0 allocs/op 277 // BenchmarkBalancerParallel/WR-1000000-4 10868928 100.4 ns/op 0 B/op 0 allocs/op 278 // BenchmarkBalancerParallel/Hash-1000000-4 14018941 85.23 ns/op 0 B/op 0 allocs/op 279 // BenchmarkBalancerParallel/RoundRobin-1000000-4 11614006 87.05 ns/op 0 B/op 0 allocs/op 280 // BenchmarkBalancerParallel/Random-1000000-4 17565664 68.35 ns/op 0 B/op 0 allocs/op