|
| 1 | +package vllm |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "inference.networking.x-k8s.io/llm-instance-gateway/pkg/ext-proc/backend" |
| 8 | + |
| 9 | + dto "github.com/prometheus/client_model/go" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "google.golang.org/protobuf/proto" |
| 12 | +) |
| 13 | + |
| 14 | +func TestPromToPodMetrics(t *testing.T) { |
| 15 | + testCases := []struct { |
| 16 | + name string |
| 17 | + metricFamilies map[string]*dto.MetricFamily |
| 18 | + expectedMetrics *backend.Metrics |
| 19 | + expectedErr error |
| 20 | + initialPodMetrics *backend.PodMetrics |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "all metrics available", |
| 24 | + metricFamilies: map[string]*dto.MetricFamily{ |
| 25 | + RunningQueueSizeMetricName: { |
| 26 | + Metric: []*dto.Metric{ |
| 27 | + { |
| 28 | + Gauge: &dto.Gauge{ |
| 29 | + Value: proto.Float64(10), |
| 30 | + }, |
| 31 | + TimestampMs: proto.Int64(100), |
| 32 | + }, |
| 33 | + { |
| 34 | + Gauge: &dto.Gauge{ |
| 35 | + Value: proto.Float64(15), |
| 36 | + }, |
| 37 | + TimestampMs: proto.Int64(200), // This is the latest |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + WaitingQueueSizeMetricName: { |
| 42 | + Metric: []*dto.Metric{ |
| 43 | + { |
| 44 | + Gauge: &dto.Gauge{ |
| 45 | + Value: proto.Float64(20), |
| 46 | + }, |
| 47 | + TimestampMs: proto.Int64(100), |
| 48 | + }, |
| 49 | + { |
| 50 | + Gauge: &dto.Gauge{ |
| 51 | + Value: proto.Float64(25), |
| 52 | + }, |
| 53 | + TimestampMs: proto.Int64(200), // This is the latest |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + KVCacheUsagePercentMetricName: { |
| 58 | + Metric: []*dto.Metric{ |
| 59 | + { |
| 60 | + Gauge: &dto.Gauge{ |
| 61 | + Value: proto.Float64(0.8), |
| 62 | + }, |
| 63 | + TimestampMs: proto.Int64(100), |
| 64 | + }, |
| 65 | + { |
| 66 | + Gauge: &dto.Gauge{ |
| 67 | + Value: proto.Float64(0.9), |
| 68 | + }, |
| 69 | + TimestampMs: proto.Int64(200), // This is the latest |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + LoraRequestInfoMetricName: { |
| 74 | + Metric: []*dto.Metric{ |
| 75 | + { |
| 76 | + Label: []*dto.LabelPair{ |
| 77 | + { |
| 78 | + Name: proto.String(LoraRequestInfoRunningAdaptersMetricName), |
| 79 | + Value: proto.String("lora3,lora4"), |
| 80 | + }, |
| 81 | + { |
| 82 | + Name: proto.String(LoraRequestInfoMaxAdaptersMetricName), |
| 83 | + Value: proto.String("2"), |
| 84 | + }, |
| 85 | + }, |
| 86 | + Gauge: &dto.Gauge{ |
| 87 | + Value: proto.Float64(100), |
| 88 | + }, |
| 89 | + }, |
| 90 | + { |
| 91 | + Label: []*dto.LabelPair{ |
| 92 | + { |
| 93 | + Name: proto.String(LoraRequestInfoRunningAdaptersMetricName), |
| 94 | + Value: proto.String("lora2"), |
| 95 | + }, |
| 96 | + { |
| 97 | + Name: proto.String(LoraRequestInfoMaxAdaptersMetricName), |
| 98 | + Value: proto.String("2"), |
| 99 | + }, |
| 100 | + }, |
| 101 | + Gauge: &dto.Gauge{ |
| 102 | + Value: proto.Float64(90), |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + expectedMetrics: &backend.Metrics{ |
| 109 | + RunningQueueSize: 15, |
| 110 | + WaitingQueueSize: 25, |
| 111 | + KVCacheUsagePercent: 0.9, |
| 112 | + ActiveModels: map[string]int{ |
| 113 | + "lora3": 0, |
| 114 | + "lora4": 0, |
| 115 | + }, |
| 116 | + MaxActiveModels: 2, |
| 117 | + }, |
| 118 | + initialPodMetrics: &backend.PodMetrics{}, |
| 119 | + expectedErr: nil, |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "invalid max lora", |
| 123 | + metricFamilies: map[string]*dto.MetricFamily{ |
| 124 | + RunningQueueSizeMetricName: { |
| 125 | + Metric: []*dto.Metric{ |
| 126 | + { |
| 127 | + Gauge: &dto.Gauge{ |
| 128 | + Value: proto.Float64(10), |
| 129 | + }, |
| 130 | + TimestampMs: proto.Int64(100), |
| 131 | + }, |
| 132 | + { |
| 133 | + Gauge: &dto.Gauge{ |
| 134 | + Value: proto.Float64(15), |
| 135 | + }, |
| 136 | + TimestampMs: proto.Int64(200), // This is the latest |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + WaitingQueueSizeMetricName: { |
| 141 | + Metric: []*dto.Metric{ |
| 142 | + { |
| 143 | + Gauge: &dto.Gauge{ |
| 144 | + Value: proto.Float64(20), |
| 145 | + }, |
| 146 | + TimestampMs: proto.Int64(100), |
| 147 | + }, |
| 148 | + { |
| 149 | + Gauge: &dto.Gauge{ |
| 150 | + Value: proto.Float64(25), |
| 151 | + }, |
| 152 | + TimestampMs: proto.Int64(200), // This is the latest |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + KVCacheUsagePercentMetricName: { |
| 157 | + Metric: []*dto.Metric{ |
| 158 | + { |
| 159 | + Gauge: &dto.Gauge{ |
| 160 | + Value: proto.Float64(0.8), |
| 161 | + }, |
| 162 | + TimestampMs: proto.Int64(100), |
| 163 | + }, |
| 164 | + { |
| 165 | + Gauge: &dto.Gauge{ |
| 166 | + Value: proto.Float64(0.9), |
| 167 | + }, |
| 168 | + TimestampMs: proto.Int64(200), // This is the latest |
| 169 | + }, |
| 170 | + }, |
| 171 | + }, |
| 172 | + LoraRequestInfoMetricName: { |
| 173 | + Metric: []*dto.Metric{ |
| 174 | + { |
| 175 | + Label: []*dto.LabelPair{ |
| 176 | + { |
| 177 | + Name: proto.String(LoraRequestInfoRunningAdaptersMetricName), |
| 178 | + Value: proto.String("lora3,lora4"), |
| 179 | + }, |
| 180 | + { |
| 181 | + Name: proto.String(LoraRequestInfoRunningAdaptersMetricName), |
| 182 | + Value: proto.String("2a"), |
| 183 | + }, |
| 184 | + }, |
| 185 | + Gauge: &dto.Gauge{ |
| 186 | + Value: proto.Float64(100), |
| 187 | + }, |
| 188 | + }, |
| 189 | + { |
| 190 | + Label: []*dto.LabelPair{ |
| 191 | + { |
| 192 | + Name: proto.String(LoraRequestInfoRunningAdaptersMetricName), |
| 193 | + Value: proto.String("lora2"), |
| 194 | + }, |
| 195 | + { |
| 196 | + Name: proto.String(LoraRequestInfoMaxAdaptersMetricName), |
| 197 | + Value: proto.String("2"), |
| 198 | + }, |
| 199 | + }, |
| 200 | + Gauge: &dto.Gauge{ |
| 201 | + Value: proto.Float64(90), |
| 202 | + }, |
| 203 | + }, |
| 204 | + }, |
| 205 | + }, |
| 206 | + }, |
| 207 | + expectedMetrics: &backend.Metrics{ |
| 208 | + RunningQueueSize: 15, |
| 209 | + WaitingQueueSize: 25, |
| 210 | + KVCacheUsagePercent: 0.9, |
| 211 | + ActiveModels: map[string]int{ |
| 212 | + "lora3": 0, |
| 213 | + "lora4": 0, |
| 214 | + }, |
| 215 | + MaxActiveModels: 0, |
| 216 | + }, |
| 217 | + initialPodMetrics: &backend.PodMetrics{}, |
| 218 | + expectedErr: fmt.Errorf("strconv.Atoi: parsing '2a': invalid syntax"), |
| 219 | + }, |
| 220 | + } |
| 221 | + for _, tc := range testCases { |
| 222 | + t.Run(tc.name, func(t *testing.T) { |
| 223 | + updated, err := promToPodMetrics(tc.metricFamilies, tc.initialPodMetrics) |
| 224 | + if tc.expectedErr != nil { |
| 225 | + assert.Error(t, err) |
| 226 | + } else { |
| 227 | + assert.NoError(t, err) |
| 228 | + assert.Equal(t, tc.expectedMetrics, &updated.Metrics) |
| 229 | + } |
| 230 | + }) |
| 231 | + } |
| 232 | +} |
0 commit comments