diff --git a/model/labels/labels_dedupelabels_test.go b/model/labels/labels_dedupelabels_test.go index 5ef9255c21..baf0423db2 100644 --- a/model/labels/labels_dedupelabels_test.go +++ b/model/labels/labels_dedupelabels_test.go @@ -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 diff --git a/model/labels/labels_slicelabels_test.go b/model/labels/labels_slicelabels_test.go new file mode 100644 index 0000000000..e9edc9152d --- /dev/null +++ b/model/labels/labels_slicelabels_test.go @@ -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, +} diff --git a/model/labels/labels_stringlabels_test.go b/model/labels/labels_stringlabels_test.go new file mode 100644 index 0000000000..aaa8b52415 --- /dev/null +++ b/model/labels/labels_stringlabels_test.go @@ -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, +} diff --git a/model/labels/labels_test.go b/model/labels/labels_test.go index 1a4e2b29d7..feee476dcc 100644 --- a/model/labels/labels_test.go +++ b/model/labels/labels_test.go @@ -37,6 +37,7 @@ var testCaseLabels = []Labels{ 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) { @@ -46,6 +47,7 @@ func TestLabels_String(t *testing.T) { `{"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"}`, } require.Equal(t, len(testCaseLabels), len(expected)) for i, c := range expected { @@ -61,6 +63,17 @@ func BenchmarkString(b *testing.B) { } } +func TestSizeOfLabels(t *testing.T) { + require.Equal(t, len(testCaseLabels), len(expectedSizeOfLabels)) + 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",