MCPcopy Create free account
hub / github.com/zalando/skipper / TestConsistentHashBoundedLoadDistribution

Function TestConsistentHashBoundedLoadDistribution

loadbalancer/algorithm_test.go:420–460  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

418}
419
420func TestConsistentHashBoundedLoadDistribution(t *testing.T) {
421 endpoints := []string{"http://127.0.0.1:8080", "http://127.0.0.2:8080", "http://127.0.0.3:8080"}
422 r, _ := http.NewRequest("GET", "http://127.0.0.1:1234/foo", nil)
423 route := NewAlgorithmProvider().Do([]*routing.Route{{
424 Route: eskip.Route{
425 BackendType: eskip.LBBackend,
426 LBAlgorithm: ConsistentHash.String(),
427 LBEndpoints: eskip.NewLBEndpoints(endpoints),
428 },
429 }})[0]
430
431 ch := route.LBAlgorithm.(*consistentHash)
432 balanceFactor := 1.25
433 ctx := &routing.LBContext{
434 Request: r,
435 Route: route,
436 LBEndpoints: route.LBEndpoints,
437 Params: map[string]interface{}{ConsistentHashBalanceFactor: balanceFactor},
438 }
439 endpointRegistry := routing.NewEndpointRegistry(routing.RegistryOptions{})
440 defer endpointRegistry.Close()
441 endpointRegistry.Do([]*routing.Route{route})
442
443 for i := 0; i < 100; i++ {
444 ep := ch.Apply(ctx)
445 ifr0 := route.LBEndpoints[0].Metrics.InflightRequests()
446 ifr1 := route.LBEndpoints[1].Metrics.InflightRequests()
447 ifr2 := route.LBEndpoints[2].Metrics.InflightRequests()
448
449 assert.Equal(t, int64(ifr0), endpointRegistry.GetMetrics(route.LBEndpoints[0].Host).InflightRequests())
450 assert.Equal(t, int64(ifr1), endpointRegistry.GetMetrics(route.LBEndpoints[1].Host).InflightRequests())
451 assert.Equal(t, int64(ifr2), endpointRegistry.GetMetrics(route.LBEndpoints[2].Host).InflightRequests())
452
453 avg := float64(ifr0+ifr1+ifr2) / 3.0
454 limit := int64(avg*balanceFactor) + 1
455 if ifr0 > limit || ifr1 > limit || ifr2 > limit {
456 t.Errorf("Expected in-flight requests for each endpoint to be less than %d. In-flight request counts: %d, %d, %d", limit, ifr0, ifr1, ifr2)
457 }
458 endpointRegistry.GetMetrics(ep.Host).IncInflightRequest()
459 }
460}
461
462func TestConsistentHashKeyDistribution(t *testing.T) {
463 endpoints := []string{"http://10.2.0.1:8080", "http://10.2.0.2:8080", "http://10.2.0.3:8080", "http://10.2.0.4:8080", "http://10.2.0.5:8080", "http://10.2.0.6:8080", "http://10.2.0.7:8080", "http://10.2.0.8:8080", "http://10.2.0.9:8080", "http://10.2.0.10:8080"}

Callers

nothing calls this directly

Calls 12

CloseMethod · 0.95
DoMethod · 0.95
GetMetricsMethod · 0.95
NewLBEndpointsFunction · 0.92
NewEndpointRegistryFunction · 0.92
NewAlgorithmProviderFunction · 0.85
DoMethod · 0.65
StringMethod · 0.65
ApplyMethod · 0.65
InflightRequestsMethod · 0.65
ErrorfMethod · 0.65
IncInflightRequestMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…