|
| 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 scheduling |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/google/go-cmp/cmp" |
| 24 | + k8stypes "k8s.io/apimachinery/pkg/types" |
| 25 | + backendmetrics "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend/metrics" // Import config for thresholds |
| 26 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/plugins" |
| 27 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/plugins/picker" |
| 28 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/plugins/scorers" |
| 29 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types" |
| 30 | +) |
| 31 | + |
| 32 | +func TestScorers(t *testing.T) { |
| 33 | + tests := []struct { |
| 34 | + name string |
| 35 | + scorer plugins.Scorer |
| 36 | + req *types.LLMRequest |
| 37 | + input []*backendmetrics.FakePodMetrics |
| 38 | + wantRes *types.Result |
| 39 | + err bool |
| 40 | + }{ |
| 41 | + { |
| 42 | + name: "load based scorer", |
| 43 | + scorer: &scorers.LoadBasedScorer{}, |
| 44 | + req: &types.LLMRequest{ |
| 45 | + Model: "critical", |
| 46 | + ResolvedTargetModel: "critical", |
| 47 | + Critical: true, |
| 48 | + }, |
| 49 | + // pod2 will be picked because it has the shortest queue |
| 50 | + input: []*backendmetrics.FakePodMetrics{ |
| 51 | + { |
| 52 | + Pod: &backendmetrics.Pod{NamespacedName: k8stypes.NamespacedName{Name: "pod1"}}, |
| 53 | + Metrics: &backendmetrics.Metrics{ |
| 54 | + WaitingQueueSize: 2, |
| 55 | + KVCacheUsagePercent: 0.2, |
| 56 | + MaxActiveModels: 2, |
| 57 | + ActiveModels: map[string]int{ |
| 58 | + "foo": 1, |
| 59 | + "bar": 1, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + { |
| 64 | + Pod: &backendmetrics.Pod{NamespacedName: k8stypes.NamespacedName{Name: "pod2"}}, |
| 65 | + Metrics: &backendmetrics.Metrics{ |
| 66 | + WaitingQueueSize: 0, |
| 67 | + KVCacheUsagePercent: 0.2, |
| 68 | + MaxActiveModels: 2, |
| 69 | + ActiveModels: map[string]int{ |
| 70 | + "foo": 1, |
| 71 | + "bar": 1, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + { |
| 76 | + Pod: &backendmetrics.Pod{NamespacedName: k8stypes.NamespacedName{Name: "pod3"}}, |
| 77 | + Metrics: &backendmetrics.Metrics{ |
| 78 | + WaitingQueueSize: 5, |
| 79 | + KVCacheUsagePercent: 0.2, |
| 80 | + MaxActiveModels: 2, |
| 81 | + ActiveModels: map[string]int{ |
| 82 | + "foo": 1, |
| 83 | + "bar": 1, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + wantRes: &types.Result{ |
| 89 | + TargetPod: &types.PodMetrics{ |
| 90 | + Pod: &backendmetrics.Pod{NamespacedName: k8stypes.NamespacedName{Name: "pod2"}}, |
| 91 | + Metrics: &backendmetrics.Metrics{ |
| 92 | + WaitingQueueSize: 0, |
| 93 | + KVCacheUsagePercent: 0.2, |
| 94 | + MaxActiveModels: 2, |
| 95 | + ActiveModels: map[string]int{ |
| 96 | + "foo": 1, |
| 97 | + "bar": 1, |
| 98 | + }, |
| 99 | + WaitingModels: map[string]int{}, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + for _, test := range tests { |
| 107 | + t.Run(test.name, func(t *testing.T) { |
| 108 | + scheduler := NewScheduler(&fakeDataStore{pods: test.input}) |
| 109 | + scheduler.scorers = map[plugins.Scorer]int{test.scorer: 1} |
| 110 | + scheduler.picker = &picker.MaxScorePicker{} |
| 111 | + got, err := scheduler.Schedule(context.Background(), test.req) |
| 112 | + if test.err != (err != nil) { |
| 113 | + t.Errorf("Unexpected error, got %v, want %v", err, test.err) |
| 114 | + } |
| 115 | + |
| 116 | + opt := cmp.AllowUnexported(types.PodMetrics{}) |
| 117 | + if diff := cmp.Diff(test.wantRes, got, opt); diff != "" { |
| 118 | + t.Errorf("Unexpected output (-want +got): %v", diff) |
| 119 | + } |
| 120 | + }) |
| 121 | + } |
| 122 | +} |
0 commit comments