Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions pkg/epp/scheduling/framework/plugins/picker/picker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package picker

import (
"context"
"math"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -138,8 +139,8 @@ func TestPickMaxScorePicker(t *testing.T) {

func TestPickWeightedRandomPicker(t *testing.T) {
const (
testIterations = 1000
tolerance = 0.2 // 20% tolerance in [0,1] range
testIterations = 10000
tolerance = 0.05 // Verify within tolerance ยฑ5%
)

pod1 := &types.PodMetrics{Pod: &backend.Pod{NamespacedName: k8stypes.NamespacedName{Name: "pod1"}}}
Expand Down Expand Up @@ -197,14 +198,14 @@ func TestPickWeightedRandomPicker(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
picker := NewWeightedRandomPicker(test.maxPods)
selectionCounts := make(map[string]int)

// Calculate expected probabilities based on scores
// Summarize the total score of all pods
totalScore := 0.0
for _, pod := range test.input {
totalScore += pod.Score
}

// Calculate expected probabilities based on scores
expectedProbabilities := make(map[string]float64)
for _, pod := range test.input {
podName := pod.GetPod().NamespacedName.Name
Expand All @@ -216,32 +217,27 @@ func TestPickWeightedRandomPicker(t *testing.T) {
}

// Initialize selection counters for each pod
selectionCounts := make(map[string]int)
for _, pod := range test.input {
podName := pod.GetPod().NamespacedName.Name
selectionCounts[podName] = 0
}

// Run multiple iterations to gather statistical data
for i := 0; i < testIterations; i++ {
for range testIterations {
result := picker.Pick(context.Background(), types.NewCycleState(), test.input)

// Count selections for probability analysis
if len(result.TargetPods) > 0 {
selectedPodName := result.TargetPods[0].GetPod().NamespacedName.Name
selectionCounts[selectedPodName]++
}
selectedPodName := result.TargetPods[0].GetPod().NamespacedName.Name
selectionCounts[selectedPodName]++
}

// Verify probability distribution
for podName, expectedProb := range expectedProbabilities {
actualCount := selectionCounts[podName]
actualProb := float64(actualCount) / float64(testIterations)

toleranceValue := expectedProb * tolerance
lowerBound := expectedProb - toleranceValue
upperBound := expectedProb + toleranceValue

if actualProb < lowerBound || actualProb > upperBound {
if math.Abs(actualProb-expectedProb) > tolerance {
t.Errorf("Pod %s: expected probability %.3f ยฑ%.1f%%, got %.3f (count: %d/%d)",
podName, expectedProb, tolerance*100, actualProb, actualCount, testIterations)
} else {
Expand Down