|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package scorer_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + k8stypes "k8s.io/apimachinery/pkg/types" |
| 22 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 23 | + backendmetrics "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend/metrics" |
| 24 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/plugins/scorer" |
| 25 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types" |
| 26 | + "testing" |
| 27 | +) |
| 28 | + |
| 29 | +func TestPrefixAwareScorer(t *testing.T) { |
| 30 | + ctx := context.Background() |
| 31 | + logger := log.FromContext(ctx) |
| 32 | + ctx = log.IntoContext(ctx, logger) |
| 33 | + |
| 34 | + // Create test pods |
| 35 | + pod1 := &types.PodMetrics{ |
| 36 | + Pod: &backendmetrics.Pod{ |
| 37 | + NamespacedName: k8stypes.NamespacedName{ |
| 38 | + Name: "pod1", |
| 39 | + Namespace: "default", |
| 40 | + }, |
| 41 | + }, |
| 42 | + Metrics: &backendmetrics.Metrics{}, |
| 43 | + } |
| 44 | + pod2 := &types.PodMetrics{ |
| 45 | + Pod: &backendmetrics.Pod{ |
| 46 | + NamespacedName: k8stypes.NamespacedName{ |
| 47 | + Name: "pod2", |
| 48 | + Namespace: "default", |
| 49 | + }, |
| 50 | + }, |
| 51 | + Metrics: &backendmetrics.Metrics{}, |
| 52 | + } |
| 53 | + |
| 54 | + tests := []struct { |
| 55 | + name string |
| 56 | + weight float64 |
| 57 | + prompt string |
| 58 | + modelName string |
| 59 | + prefixToAdd string |
| 60 | + podToAdd k8stypes.NamespacedName |
| 61 | + prefixModel string // Model name to use when adding the prefix |
| 62 | + expectedScores map[types.Pod]float64 |
| 63 | + }{ |
| 64 | + { |
| 65 | + name: "no prompt", |
| 66 | + weight: 1.0, |
| 67 | + prompt: "", |
| 68 | + modelName: "model1", |
| 69 | + prefixToAdd: "hello", |
| 70 | + podToAdd: pod1.Pod.NamespacedName, |
| 71 | + prefixModel: "model1", |
| 72 | + expectedScores: map[types.Pod]float64{}, // No prompt means zero scores |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "exact prefix match", |
| 76 | + weight: 1.0, |
| 77 | + prompt: "hello world", |
| 78 | + modelName: "model1", |
| 79 | + prefixToAdd: "hello", |
| 80 | + podToAdd: pod1.Pod.NamespacedName, |
| 81 | + prefixModel: "model1", |
| 82 | + expectedScores: map[types.Pod]float64{ |
| 83 | + pod1: 1.0, |
| 84 | + pod2: 0.0, |
| 85 | + }, // pod1 matches, pod2 doesn't |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "no prefix match", |
| 89 | + weight: 1.0, |
| 90 | + prompt: "goodbye", |
| 91 | + modelName: "model1", |
| 92 | + prefixToAdd: "hello", |
| 93 | + podToAdd: pod1.Pod.NamespacedName, |
| 94 | + prefixModel: "model1", |
| 95 | + expectedScores: map[types.Pod]float64{}, // No matching prefix |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "different model name", |
| 99 | + weight: 1.0, |
| 100 | + prompt: "hello world", |
| 101 | + modelName: "model2", // Try to find with model2 |
| 102 | + prefixToAdd: "hello", |
| 103 | + podToAdd: pod1.Pod.NamespacedName, |
| 104 | + prefixModel: "model1", // But prefix was added with model1 |
| 105 | + expectedScores: map[types.Pod]float64{}, // Model name mismatch should result in no match |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "custom weight", |
| 109 | + weight: 0.5, |
| 110 | + prompt: "hello world", |
| 111 | + modelName: "model1", |
| 112 | + prefixToAdd: "hello", |
| 113 | + podToAdd: pod1.Pod.NamespacedName, |
| 114 | + prefixModel: "model1", |
| 115 | + expectedScores: map[types.Pod]float64{ |
| 116 | + pod1: 0.5, // Pod1 matches with weight |
| 117 | + pod2: 0.0, // Pod2 doesn't match |
| 118 | + }, // Weight affects score |
| 119 | + }, |
| 120 | + } |
| 121 | + |
| 122 | + for _, tt := range tests { |
| 123 | + t.Run(tt.name, func(t *testing.T) { |
| 124 | + // Reset prefix store for each test |
| 125 | + config := scorer.DefaultPrefixStoreConfig() |
| 126 | + config.BlockSize = 5 // set small chunking for testing |
| 127 | + |
| 128 | + s := scorer.NewPrefixAwareScorer(config) |
| 129 | + |
| 130 | + // Add prefix if specified |
| 131 | + if tt.prefixToAdd != "" { |
| 132 | + err := s.GetPrefixStore().AddEntry(tt.prefixModel, |
| 133 | + tt.prefixToAdd, &tt.podToAdd) |
| 134 | + if err != nil { |
| 135 | + t.Fatalf("Failed to add prefix: %v", err) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // Create test context |
| 140 | + sCtx := types.NewSchedulingContext(ctx, &types.LLMRequest{ |
| 141 | + Prompt: tt.prompt, |
| 142 | + ResolvedTargetModel: tt.modelName, |
| 143 | + }, []types.Pod{}, 0) |
| 144 | + |
| 145 | + // Score pods |
| 146 | + pods := []types.Pod{pod1, pod2} |
| 147 | + scores := s.Score(sCtx, pods) |
| 148 | + |
| 149 | + for p, score := range scores { |
| 150 | + if score != tt.expectedScores[p] { |
| 151 | + t.Errorf("Pod %v: expected score %v, got %v", p, tt.expectedScores[p], score) |
| 152 | + } |
| 153 | + } |
| 154 | + }) |
| 155 | + } |
| 156 | +} |
0 commit comments