Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: optimize segment key parser allocations (#1625)
* perf: optimize segment key parser allocations * perf: replace strings builder with bytes buffer
- Loading branch information
1 parent
9b6e6db
commit 7c83b32
Showing
2 changed files
with
78 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package segment | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
) | ||
|
||
func BenchmarkKey_Parse(b *testing.B) { | ||
const ( | ||
labelsSize = 10 | ||
minLen = 6 | ||
maxLen = 16 | ||
) | ||
|
||
// Duplicates are okay. | ||
labels := make(map[string]string, labelsSize+1) | ||
for i := 0; i < labelsSize; i++ { | ||
labels[randString(randInt(minLen, maxLen))] = randString(randInt(minLen, maxLen)) | ||
} | ||
|
||
labels["__name__"] = "benchmark.key.parse" | ||
keyStr := NewKey(labels).Normalized() | ||
|
||
b.ReportAllocs() | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
if _, err := ParseKey(keyStr); err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
} | ||
|
||
// TODO(kolesnikovae): This is not near perfect way of generating strings. | ||
// It makes sense to create a package for util functions like this. | ||
|
||
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
|
||
func randString(n int) string { | ||
b := make([]byte, n) | ||
for i := range b { | ||
b[i] = letterBytes[rand.Intn(len(letterBytes))] | ||
} | ||
return string(b) | ||
} |