github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/storage/segment/key_bech_test.go (about) 1 package segment 2 3 import ( 4 "math/rand" 5 "testing" 6 ) 7 8 func BenchmarkKey_Parse(b *testing.B) { 9 const ( 10 labelsSize = 10 11 minLen = 6 12 maxLen = 16 13 ) 14 15 // Duplicates are okay. 16 labels := make(map[string]string, labelsSize+1) 17 for i := 0; i < labelsSize; i++ { 18 labels[randString(randInt(minLen, maxLen))] = randString(randInt(minLen, maxLen)) 19 } 20 21 labels["__name__"] = "benchmark.key.parse" 22 keyStr := NewKey(labels).Normalized() 23 24 b.ReportAllocs() 25 b.ResetTimer() 26 27 for i := 0; i < b.N; i++ { 28 if _, err := ParseKey(keyStr); err != nil { 29 b.Fatal(err) 30 } 31 } 32 } 33 34 // TODO(kolesnikovae): This is not near perfect way of generating strings. 35 // It makes sense to create a package for util functions like this. 36 37 const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 38 39 func randString(n int) string { 40 b := make([]byte, n) 41 for i := range b { 42 b[i] = letterBytes[rand.Intn(len(letterBytes))] 43 } 44 return string(b) 45 }