Files
zeekayandHanzo Dev c16ed9fe26 Go 1.27.1, and the code it can say more simply
Modules declare go 1.27.1 and the build images follow. go fix applied the
modernizers; omitzero is not applied, since it changes what the JSON says.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-17 02:34:35 -07:00

97 lines
2.1 KiB
Go

package metadataexporter
import (
"fmt"
"math/rand"
"testing"
"time"
)
func BenchmarkValueTracker_RealisticLoad(b *testing.B) {
const (
numSpans = 100_000
batchSize = 50_000
totalKeys = 1_000
highCardinalKeys = 200 // 20% of keys
normalValuePool = 100
normalUniqueVals = 30
)
tracker := NewValueTracker(1000, 10000, 45*time.Minute)
defer tracker.Close()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
keys := make([]string, totalKeys)
highCardinalKeyMap := make(map[string]bool)
for i := range highCardinalKeys {
keys[i] = fmt.Sprintf("high-card-key-%d", i)
highCardinalKeyMap[keys[i]] = true
}
for i := highCardinalKeys; i < totalKeys; i++ {
keys[i] = fmt.Sprintf("key-%d", i)
}
valuePool := make(map[string][]string)
for _, key := range keys {
if !highCardinalKeyMap[key] {
values := make([]string, normalValuePool)
for i := range normalUniqueVals {
values[i] = fmt.Sprintf("unique-value-%s-%d", key, i)
}
for i := normalUniqueVals; i < normalValuePool; i++ {
values[i] = values[r.Intn(normalUniqueVals)]
}
valuePool[key] = values
}
}
uniqueValueCounter := make(map[string]int)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < numSpans; j += batchSize {
batchSize := min(batchSize, numSpans-j)
for range batchSize {
numAttrs := 10 + r.Intn(41)
for range numAttrs {
key := keys[r.Intn(len(keys))]
var value string
if highCardinalKeyMap[key] {
uniqueValueCounter[key]++
value = fmt.Sprintf("unique-value-%s-%d", key, uniqueValueCounter[key])
} else {
values := valuePool[key]
value = values[r.Intn(len(values))]
}
tracker.AddValue(key, value)
}
}
for range 10 {
key := keys[r.Intn(len(keys))]
tracker.GetUniqueValueCount(key)
}
}
}
}
func BenchmarkValueTracker_HighCardinality(b *testing.B) {
tracker := NewValueTracker(1000, 100000, 45*time.Minute)
defer tracker.Close()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Add 100K unique values to a single key
key := "high-card-key"
for j := range 100000 {
tracker.AddValue(key, fmt.Sprintf("unique-value-%d", j))
}
}
}