mirror of
https://github.com/prometheus/prometheus.git
synced 2025-08-05 21:57:09 +02:00
Merge pull request #16807 from bboreham/test-sizeoflabels
[TESTS] Labels: Add a test for SizeOfLabels
This commit is contained in:
commit
74aca682b7
@ -21,6 +21,15 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var expectedSizeOfLabels = []uint64{ // Values must line up with testCaseLabels.
|
||||
16,
|
||||
0,
|
||||
41,
|
||||
270,
|
||||
271,
|
||||
325,
|
||||
}
|
||||
|
||||
func TestVarint(t *testing.T) {
|
||||
cases := []struct {
|
||||
v int
|
||||
|
25
model/labels/labels_slicelabels_test.go
Normal file
25
model/labels/labels_slicelabels_test.go
Normal file
@ -0,0 +1,25 @@
|
||||
// Copyright 2025 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build slicelabels
|
||||
|
||||
package labels
|
||||
|
||||
var expectedSizeOfLabels = []uint64{ // Values must line up with testCaseLabels.
|
||||
72,
|
||||
0,
|
||||
97,
|
||||
326,
|
||||
327,
|
||||
549,
|
||||
}
|
25
model/labels/labels_stringlabels_test.go
Normal file
25
model/labels/labels_stringlabels_test.go
Normal file
@ -0,0 +1,25 @@
|
||||
// Copyright 2025 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build !slicelabels && !dedupelabels
|
||||
|
||||
package labels
|
||||
|
||||
var expectedSizeOfLabels = []uint64{ // Values must line up with testCaseLabels.
|
||||
12,
|
||||
0,
|
||||
37,
|
||||
266,
|
||||
270,
|
||||
309,
|
||||
}
|
@ -26,37 +26,33 @@ import (
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
var (
|
||||
s254 = strings.Repeat("x", 254) // Edge cases for stringlabels encoding.
|
||||
s255 = strings.Repeat("x", 255)
|
||||
)
|
||||
|
||||
var testCaseLabels = []Labels{
|
||||
FromStrings("t1", "t1", "t2", "t2"),
|
||||
{},
|
||||
FromStrings("service.name", "t1", "whatever\\whatever", "t2"),
|
||||
FromStrings("aaa", "111", "xx", s254),
|
||||
FromStrings("aaa", "111", "xx", s255),
|
||||
FromStrings("__name__", "kube_pod_container_status_last_terminated_exitcode", "cluster", "prod-af-north-0", " container", "prometheus", "instance", "kube-state-metrics-0:kube-state-metrics:ksm", "job", "kube-state-metrics/kube-state-metrics", " namespace", "observability-prometheus", "pod", "observability-prometheus-0", "uid", "d3ec90b2-4975-4607-b45d-b9ad64bb417e"),
|
||||
}
|
||||
|
||||
func TestLabels_String(t *testing.T) {
|
||||
s254 := strings.Repeat("x", 254) // Edge cases for stringlabels encoding.
|
||||
s255 := strings.Repeat("x", 255)
|
||||
cases := []struct {
|
||||
labels Labels
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
labels: FromStrings("t1", "t1", "t2", "t2"),
|
||||
expected: "{t1=\"t1\", t2=\"t2\"}",
|
||||
},
|
||||
{
|
||||
labels: Labels{},
|
||||
expected: "{}",
|
||||
},
|
||||
{
|
||||
labels: FromStrings("service.name", "t1", "whatever\\whatever", "t2"),
|
||||
expected: `{"service.name"="t1", "whatever\\whatever"="t2"}`,
|
||||
},
|
||||
{
|
||||
labels: FromStrings("aaa", "111", "xx", s254),
|
||||
expected: `{aaa="111", xx="` + s254 + `"}`,
|
||||
},
|
||||
{
|
||||
labels: FromStrings("aaa", "111", "xx", s255),
|
||||
expected: `{aaa="111", xx="` + s255 + `"}`,
|
||||
},
|
||||
expected := []string{ // Values must line up with testCaseLabels.
|
||||
"{t1=\"t1\", t2=\"t2\"}",
|
||||
"{}",
|
||||
`{"service.name"="t1", "whatever\\whatever"="t2"}`,
|
||||
`{aaa="111", xx="` + s254 + `"}`,
|
||||
`{aaa="111", xx="` + s255 + `"}`,
|
||||
`{" container"="prometheus", " namespace"="observability-prometheus", __name__="kube_pod_container_status_last_terminated_exitcode", cluster="prod-af-north-0", instance="kube-state-metrics-0:kube-state-metrics:ksm", job="kube-state-metrics/kube-state-metrics", pod="observability-prometheus-0", uid="d3ec90b2-4975-4607-b45d-b9ad64bb417e"}`,
|
||||
}
|
||||
for _, c := range cases {
|
||||
str := c.labels.String()
|
||||
require.Equal(t, c.expected, str)
|
||||
require.Len(t, expected, len(testCaseLabels))
|
||||
for i, c := range expected {
|
||||
str := testCaseLabels[i].String()
|
||||
require.Equal(t, c, str)
|
||||
}
|
||||
}
|
||||
|
||||
@ -67,6 +63,17 @@ func BenchmarkString(b *testing.B) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSizeOfLabels(t *testing.T) {
|
||||
require.Len(t, expectedSizeOfLabels, len(testCaseLabels))
|
||||
for i, c := range expectedSizeOfLabels { // Declared in build-tag-specific files, e.g. labels_slicelabels_test.go.
|
||||
var total uint64
|
||||
testCaseLabels[i].Range(func(l Label) {
|
||||
total += SizeOfLabels(l.Name, l.Value, 1)
|
||||
})
|
||||
require.Equal(t, c, total)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLabels_MatchLabels(t *testing.T) {
|
||||
labels := FromStrings(
|
||||
"__name__", "ALERTS",
|
||||
|
Loading…
Reference in New Issue
Block a user