@@ -14,32 +14,23 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- package scheduling
17
+ package scorer_test
18
18
19
19
import (
20
20
"context"
21
- "testing"
22
- "time"
23
-
24
21
k8stypes "k8s.io/apimachinery/pkg/types"
25
22
"sigs.k8s.io/controller-runtime/pkg/log"
26
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"
27
25
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types"
26
+ "testing"
28
27
)
29
28
30
29
func TestPrefixAwareScorer (t * testing.T ) {
31
30
ctx := context .Background ()
32
31
logger := log .FromContext (ctx )
33
32
ctx = log .IntoContext (ctx , logger )
34
33
35
- // Create a prefix store with test configuration
36
- prefixStore := NewPrefixStore (PrefixStoreConfig {
37
- MaxEntries : 100 ,
38
- MinPrefixLen : 3 ,
39
- MaxPrefixLen : 10 ,
40
- EntryTTL : 1 * time .Hour ,
41
- })
42
-
43
34
// Create test pods
44
35
pod1 := & types.PodMetrics {
45
36
Pod : & backendmetrics.Pod {
@@ -68,7 +59,7 @@ func TestPrefixAwareScorer(t *testing.T) {
68
59
prefixToAdd string
69
60
podToAdd k8stypes.NamespacedName
70
61
prefixModel string // Model name to use when adding the prefix
71
- expectedScores [ ]float64
62
+ expectedScores map [types. Pod ]float64
72
63
}{
73
64
{
74
65
name : "no prompt" ,
@@ -78,17 +69,20 @@ func TestPrefixAwareScorer(t *testing.T) {
78
69
prefixToAdd : "hello" ,
79
70
podToAdd : pod1 .Pod .NamespacedName ,
80
71
prefixModel : "model1" ,
81
- expectedScores : [ ]float64 {0 , 0 }, // No prompt means zero scores
72
+ expectedScores : map [types. Pod ]float64 {}, // No prompt means zero scores
82
73
},
83
74
{
84
- name : "exact prefix match" ,
85
- weight : 1.0 ,
86
- prompt : "hello world" ,
87
- modelName : "model1" ,
88
- prefixToAdd : "hello" ,
89
- podToAdd : pod1 .Pod .NamespacedName ,
90
- prefixModel : "model1" ,
91
- expectedScores : []float64 {1.0 , 0 }, // pod1 matches, pod2 doesn't
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
92
86
},
93
87
{
94
88
name : "no prefix match" ,
@@ -98,7 +92,7 @@ func TestPrefixAwareScorer(t *testing.T) {
98
92
prefixToAdd : "hello" ,
99
93
podToAdd : pod1 .Pod .NamespacedName ,
100
94
prefixModel : "model1" ,
101
- expectedScores : [ ]float64 {0 , 0 }, // No matching prefix
95
+ expectedScores : map [types. Pod ]float64 {}, // No matching prefix
102
96
},
103
97
{
104
98
name : "different model name" ,
@@ -107,63 +101,54 @@ func TestPrefixAwareScorer(t *testing.T) {
107
101
modelName : "model2" , // Try to find with model2
108
102
prefixToAdd : "hello" ,
109
103
podToAdd : pod1 .Pod .NamespacedName ,
110
- prefixModel : "model1" , // But prefix was added with model1
111
- expectedScores : [ ]float64 {0 , 0 }, // Model name mismatch should result in no match
104
+ prefixModel : "model1" , // But prefix was added with model1
105
+ expectedScores : map [types. Pod ]float64 {}, // Model name mismatch should result in no match
112
106
},
113
107
{
114
- name : "custom weight" ,
115
- weight : 0.5 ,
116
- prompt : "hello world" ,
117
- modelName : "model1" ,
118
- prefixToAdd : "hello" ,
119
- podToAdd : pod1 .Pod .NamespacedName ,
120
- prefixModel : "model1" ,
121
- expectedScores : []float64 {0.5 , 0 }, // Weight affects score
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
122
119
},
123
120
}
124
121
125
122
for _ , tt := range tests {
126
123
t .Run (tt .name , func (t * testing.T ) {
127
124
// Reset prefix store for each test
128
- prefixStore = NewPrefixStore (PrefixStoreConfig {
129
- MaxEntries : 100 ,
130
- MinPrefixLen : 3 ,
131
- MaxPrefixLen : 10 ,
132
- EntryTTL : 1 * time .Hour ,
133
- })
125
+ config := scorer .DefaultPrefixStoreConfig ()
126
+ config .BlockSize = 5 // set small chunking for testing
127
+
128
+ s := scorer .NewPrefixAwareScorer (config )
134
129
135
130
// Add prefix if specified
136
131
if tt .prefixToAdd != "" {
137
- err := prefixStore .AddPrefix (ctx , tt .prefixToAdd , tt .podToAdd , tt .prefixModel )
132
+ err := s .GetPrefixStore ().AddEntry (tt .prefixModel ,
133
+ tt .prefixToAdd , & tt .podToAdd )
138
134
if err != nil {
139
135
t .Fatalf ("Failed to add prefix: %v" , err )
140
136
}
141
137
}
142
138
143
- // Create scorer with test weight
144
- scorer := NewPrefixAwareScorer (tt .weight , prefixStore )
145
-
146
139
// Create test context
147
- sCtx := types .NewContext (ctx , & types.LLMRequest {
140
+ sCtx := types .NewSchedulingContext (ctx , & types.LLMRequest {
148
141
Prompt : tt .prompt ,
149
142
ResolvedTargetModel : tt .modelName ,
150
- }, []* types.PodMetrics {} )
143
+ }, []types.Pod {}, 0 )
151
144
152
145
// Score pods
153
- pods := []* types.PodMetrics {pod1 , pod2 }
154
- scores , err := scorer .ScoreTargets (sCtx , pods )
155
- if err != nil {
156
- t .Fatalf ("Unexpected error: %v" , err )
157
- }
158
-
159
- // Verify scores
160
- if len (scores ) != len (tt .expectedScores ) {
161
- t .Fatalf ("Expected %d scores, got %d" , len (tt .expectedScores ), len (scores ))
162
- }
146
+ pods := []types.Pod {pod1 , pod2 }
147
+ scores := s .Score (sCtx , pods )
163
148
164
- for i , score := range scores {
165
- if score . Score != tt .expectedScores [i ] {
166
- t .Errorf ("Pod %d : expected score %v, got %v" , i , tt .expectedScores [i ], score . Score )
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 )
167
152
}
168
153
}
169
154
})
0 commit comments