|
14 | 14 | package prometheus
|
15 | 15 |
|
16 | 16 | import (
|
| 17 | + "reflect" |
17 | 18 | "testing"
|
18 | 19 | "time"
|
19 | 20 |
|
20 | 21 | dto "github.com/prometheus/client_model/go"
|
| 22 | + "google.golang.org/protobuf/proto" |
21 | 23 | "google.golang.org/protobuf/types/known/timestamppb"
|
22 | 24 | )
|
23 | 25 |
|
@@ -108,3 +110,130 @@ func TestNewConstMetricWithCreatedTimestamp(t *testing.T) {
|
108 | 110 | })
|
109 | 111 | }
|
110 | 112 | }
|
| 113 | + |
| 114 | +func TestMakeLabelPairs(t *testing.T) { |
| 115 | + tests := []struct { |
| 116 | + name string |
| 117 | + desc *Desc |
| 118 | + labelValues []string |
| 119 | + want []*dto.LabelPair |
| 120 | + }{ |
| 121 | + { |
| 122 | + name: "no labels", |
| 123 | + desc: NewDesc("metric-1", "", nil, nil), |
| 124 | + labelValues: nil, |
| 125 | + want: nil, |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "only constant labels", |
| 129 | + desc: NewDesc("metric-1", "", nil, map[string]string{ |
| 130 | + "label-1": "1", |
| 131 | + "label-2": "2", |
| 132 | + "label-3": "3"}), |
| 133 | + labelValues: nil, |
| 134 | + want: []*dto.LabelPair{ |
| 135 | + {Name: proto.String("label-1"), Value: proto.String("1")}, |
| 136 | + {Name: proto.String("label-2"), Value: proto.String("2")}, |
| 137 | + {Name: proto.String("label-3"), Value: proto.String("3")}, |
| 138 | + }, |
| 139 | + }, |
| 140 | + { |
| 141 | + name: "only variable labels", |
| 142 | + desc: NewDesc("metric-1", "", []string{"var-label-1", "var-label-2", "var-label-3"}, nil), |
| 143 | + labelValues: []string{"1", "2", "3"}, |
| 144 | + want: []*dto.LabelPair{ |
| 145 | + {Name: proto.String("var-label-1"), Value: proto.String("1")}, |
| 146 | + {Name: proto.String("var-label-2"), Value: proto.String("2")}, |
| 147 | + {Name: proto.String("var-label-3"), Value: proto.String("3")}, |
| 148 | + }, |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "variable and const labels", |
| 152 | + desc: NewDesc("metric-1", "", []string{"var-label-1", "var-label-2", "var-label-3"}, map[string]string{ |
| 153 | + "label-1": "1", |
| 154 | + "label-2": "2", |
| 155 | + "label-3": "3"}), |
| 156 | + labelValues: []string{"1", "2", "3"}, |
| 157 | + want: []*dto.LabelPair{ |
| 158 | + {Name: proto.String("label-1"), Value: proto.String("1")}, |
| 159 | + {Name: proto.String("label-2"), Value: proto.String("2")}, |
| 160 | + {Name: proto.String("label-3"), Value: proto.String("3")}, |
| 161 | + {Name: proto.String("var-label-1"), Value: proto.String("1")}, |
| 162 | + {Name: proto.String("var-label-2"), Value: proto.String("2")}, |
| 163 | + {Name: proto.String("var-label-3"), Value: proto.String("3")}, |
| 164 | + }, |
| 165 | + }, |
| 166 | + { |
| 167 | + name: "unsorted variable and const labels are sorted", |
| 168 | + desc: NewDesc("metric-1", "", []string{"var-label-3", "var-label-2", "var-label-1"}, map[string]string{ |
| 169 | + "label-3": "3", |
| 170 | + "label-2": "2", |
| 171 | + "label-1": "1", |
| 172 | + }), |
| 173 | + labelValues: []string{"3", "2", "1"}, |
| 174 | + want: []*dto.LabelPair{ |
| 175 | + {Name: proto.String("label-1"), Value: proto.String("1")}, |
| 176 | + {Name: proto.String("label-2"), Value: proto.String("2")}, |
| 177 | + {Name: proto.String("label-3"), Value: proto.String("3")}, |
| 178 | + {Name: proto.String("var-label-1"), Value: proto.String("1")}, |
| 179 | + {Name: proto.String("var-label-2"), Value: proto.String("2")}, |
| 180 | + {Name: proto.String("var-label-3"), Value: proto.String("3")}, |
| 181 | + }, |
| 182 | + }, |
| 183 | + } |
| 184 | + for _, tt := range tests { |
| 185 | + t.Run(tt.name, func(t *testing.T) { |
| 186 | + if got := MakeLabelPairs(tt.desc, tt.labelValues); !reflect.DeepEqual(got, tt.want) { |
| 187 | + t.Errorf("%v != %v", got, tt.want) |
| 188 | + } |
| 189 | + }) |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +func Benchmark_MakeLabelPairs(b *testing.B) { |
| 194 | + benchFunc := func(desc *Desc, variableLabelValues []string) { |
| 195 | + MakeLabelPairs(desc, variableLabelValues) |
| 196 | + } |
| 197 | + |
| 198 | + benchmarks := []struct { |
| 199 | + name string |
| 200 | + bench func(desc *Desc, variableLabelValues []string) |
| 201 | + desc *Desc |
| 202 | + variableLabelValues []string |
| 203 | + }{ |
| 204 | + { |
| 205 | + name: "1 label", |
| 206 | + desc: NewDesc( |
| 207 | + "metric", |
| 208 | + "help", |
| 209 | + []string{"var-label-1"}, |
| 210 | + Labels{"const-label-1": "value"}), |
| 211 | + variableLabelValues: []string{"value"}, |
| 212 | + }, |
| 213 | + { |
| 214 | + name: "3 labels", |
| 215 | + desc: NewDesc( |
| 216 | + "metric", |
| 217 | + "help", |
| 218 | + []string{"var-label-1", "var-label-3", "var-label-2"}, |
| 219 | + Labels{"const-label-1": "value", "const-label-3": "value", "const-label-2": "value"}), |
| 220 | + variableLabelValues: []string{"value", "value", "value"}, |
| 221 | + }, |
| 222 | + { |
| 223 | + name: "10 labels", |
| 224 | + desc: NewDesc( |
| 225 | + "metric", |
| 226 | + "help", |
| 227 | + []string{"var-label-5", "var-label-1", "var-label-3", "var-label-2", "var-label-10", "var-label-4", "var-label-7", "var-label-8", "var-label-9"}, |
| 228 | + Labels{"const-label-4": "value", "const-label-1": "value", "const-label-7": "value", "const-label-2": "value", "const-label-9": "value", "const-label-8": "value", "const-label-10": "value", "const-label-3": "value", "const-label-6": "value", "const-label-5": "value"}), |
| 229 | + variableLabelValues: []string{"value", "value", "value", "value", "value", "value", "value", "value", "value", "value"}, |
| 230 | + }, |
| 231 | + } |
| 232 | + for _, bm := range benchmarks { |
| 233 | + b.Run(bm.name, func(b *testing.B) { |
| 234 | + for i := 0; i < b.N; i++ { |
| 235 | + benchFunc(bm.desc, bm.variableLabelValues) |
| 236 | + } |
| 237 | + }) |
| 238 | + } |
| 239 | +} |
0 commit comments