github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/protocol/encoder/prometheus/utils_test.go (about) 1 // Copyright 2024 iLogtail Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package prometheus 16 17 import ( 18 "sort" 19 "testing" 20 21 pb "github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal" 22 "github.com/stretchr/testify/assert" 23 ) 24 25 // 场景:Prometheus label names 字典序排序 26 // 因子:乱序的 Prometheus label names 27 // 预期:Prometheus label names 按字典序排序 28 func TestLexicographicalSort_ShouldSortedInLexicographicalOrder(t *testing.T) { 29 // given 30 labels := []pb.Label{ 31 {Name: "Tutorial", Value: "tutorial"}, 32 {Name: "Point", Value: "point"}, 33 {Name: "Java", Value: "java"}, 34 {Name: "C++", Value: "c++"}, 35 {Name: "Golang", Value: "golang"}, 36 {Name: metricNameKey, Value: "test_metric_name"}, 37 } 38 ans := []pb.Label{ 39 {Name: "C++", Value: "c++"}, 40 {Name: "Golang", Value: "golang"}, 41 {Name: "Java", Value: "java"}, 42 {Name: "Point", Value: "point"}, 43 {Name: "Tutorial", Value: "tutorial"}, 44 {Name: metricNameKey, Value: "test_metric_name"}, 45 } 46 assert.Equal(t, len(ans), len(labels)) 47 48 // when 49 got := lexicographicalSort(labels) 50 51 // then 52 assert.Equal(t, ans, got) 53 } 54 55 // 场景:性能测试,确定 lexicographicalSort 字典序排序方法的性能 56 // 因子:利用 lexicographicalSort(底层基于sort.Sort())对 Prometheus label names 进行字典序排序 57 // 预期:lexicographicalSort 和 sort.Strings 对 Prometheus label names 的字典序排序性能相当(数量级相同) 58 // goos: darwin 59 // goarch: arm64 60 // pkg: github.com/alibaba/ilogtail/pkg/protocol/encoder/prometheus 61 // BenchmarkLexicographicalSort 62 // BenchmarkLexicographicalSort/lexicographicalSort 63 // BenchmarkLexicographicalSort/lexicographicalSort-12 23059904 47.51 ns/op 64 // BenchmarkLexicographicalSort/sort.Strings 65 // BenchmarkLexicographicalSort/sort.Strings-12 25321753 47.30 ns/op 66 // PASS 67 func BenchmarkLexicographicalSort(b *testing.B) { 68 prometheusLabels := []pb.Label{ 69 {Name: "Tutorial", Value: "tutorial"}, 70 {Name: "Point", Value: "point"}, 71 {Name: "Java", Value: "java"}, 72 {Name: "C++", Value: "c++"}, 73 {Name: "Golang", Value: "golang"}, 74 {Name: metricNameKey, Value: "test_metric_name"}, 75 } 76 stringLabels := []string{ 77 "Tutorial", 78 "Point", 79 "Java", 80 "C++", 81 "Golang", 82 metricNameKey, 83 } 84 85 b.Run("lexicographicalSort", func(b *testing.B) { 86 for i := 0; i < b.N; i++ { 87 lexicographicalSort(prometheusLabels) 88 } 89 }) 90 91 b.Run("sort.Strings", func(b *testing.B) { 92 for i := 0; i < b.N; i++ { 93 sort.Strings(stringLabels) 94 } 95 }) 96 }