Skip to content

Commit

Permalink
Add Unit Tests that verify stats reporting in autoscaler
Browse files Browse the repository at this point in the history
  • Loading branch information
MIBc committed Feb 20, 2020
1 parent ce240ab commit 34bb03f
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/autoscaler/scaling/autoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
. "knative.dev/pkg/logging/testing"
"knative.dev/pkg/metrics/metricstest"
"knative.dev/serving/pkg/autoscaler/fake"
autoscalerfake "knative.dev/serving/pkg/autoscaler/fake"
"knative.dev/serving/pkg/autoscaler/metrics"
Expand Down Expand Up @@ -65,6 +66,46 @@ func expectedEBC(totCap, targetBC, recordedConcurrency, numPods float64) int32 {
return int32(math.Floor(totCap/targetUtilization*numPods - targetBC - recordedConcurrency))
}

func expectedExcessBCF(originalReadyPodsCount int, totalValue, observedStableValue, targetBurstCapacity float64) float64 {
excessBCF := math.Floor(float64(originalReadyPodsCount)*totalValue - observedStableValue -
targetBurstCapacity)
return excessBCF
}

func TestAutoscalerMetrics(t *testing.T) {
metricClient := &autoscalerfake.MetricClient{StableConcurrency: 50.0}
a := newTestAutoscaler(t, 10, 100, metricClient)
a.expectScale(t, time.Now(), 5, expectedEBC(10, 100, 50, 1), true)

spec, podCounter := a.currentSpecAndPC()
originalReadyPodsCount, _ := podCounter.ReadyCount()
excessBCF := expectedExcessBCF(originalReadyPodsCount, spec.TotalValue, metricClient.StableConcurrency, spec.TargetBurstCapacity)

wantTags := map[string]string{}
metricstest.CheckLastValueData(t, stableRequestConcurrencyM.Name(), wantTags, 50.0)
metricstest.CheckLastValueData(t, panicRequestConcurrencyM.Name(), wantTags, 0)
metricstest.CheckLastValueData(t, desiredPodCountM.Name(), wantTags, 5)
metricstest.CheckLastValueData(t, targetRequestConcurrencyM.Name(), wantTags, spec.TotalValue)
metricstest.CheckLastValueData(t, excessBurstCapacityM.Name(), wantTags, excessBCF)
}

func TestAutoscalerMetricsWithRPS(t *testing.T) {
metricClient := &autoscalerfake.MetricClient{StableRPS: 50.0}
a := newTestAutoscalerWithScalingMetric(t, 10, 101, metricClient, "rps")
a.expectScale(t, time.Now(), 5, expectedEBC(10, 101, 50, 1), true)

spec, podCounter := a.currentSpecAndPC()
originalReadyPodsCount, _ := podCounter.ReadyCount()
excessBCF := expectedExcessBCF(originalReadyPodsCount, spec.TotalValue, metricClient.StableRPS, spec.TargetBurstCapacity)

wantTags := map[string]string{}
metricstest.CheckLastValueData(t, stableRPSM.Name(), wantTags, 50.0)
metricstest.CheckLastValueData(t, panicRPSM.Name(), wantTags, 50)
metricstest.CheckLastValueData(t, desiredPodCountM.Name(), wantTags, 5)
metricstest.CheckLastValueData(t, targetRPSM.Name(), wantTags, spec.TargetValue)
metricstest.CheckLastValueData(t, excessBurstCapacityM.Name(), wantTags, excessBCF)
}

func TestAutoscalerChangeOfPodCountService(t *testing.T) {
metrics := &autoscalerfake.MetricClient{StableConcurrency: 50.0}
a := newTestAutoscaler(t, 10, 100, metrics)
Expand Down

0 comments on commit 34bb03f

Please sign in to comment.