Files
metrics/lib/storage/date_metric_id_cache_timing_test.go
Artem FetishevandGitHub ce45fa4f4a lib/storage: shard dateMetricIDCache (#10486)
Use the same sharded implementation as in metricIDCache. The change is
basically a copy-paste. The only difference is that the rotation period
remains `1h` instead `1m` in order not to break the fix for #10064.

Signed-off-by: Artem Fetishev <[email protected]>
2026-02-18 18:16:37 +01:00

99 lines
2.6 KiB
Go

package storage
import (
"fmt"
"math/rand"
"testing"
"time"
)
type benchDateMetricIDCacheState int
const (
benchDateMetricIDCacheStateCold benchDateMetricIDCacheState = iota
benchDateMetricIDCacheStateWarm
benchDateMetricIDCacheStateRotated
)
var benchDateMetricIDCacheStates = [...]benchDateMetricIDCacheState{
benchDateMetricIDCacheStateCold,
benchDateMetricIDCacheStateWarm,
benchDateMetricIDCacheStateRotated,
}
func (s benchDateMetricIDCacheState) String() string {
return [...]string{" cold", " warm", "rotated"}[s]
}
func BenchmarkDateMetricIDCache_Has(b *testing.B) {
f := func(b *testing.B, numMetricIDs, distance int64, hitsOnly bool, state benchDateMetricIDCacheState) {
b.Helper()
c := newDateMetricIDCache()
defer c.MustStop()
const date = 12345
warmUp := state == benchDateMetricIDCacheStateWarm || state == benchDateMetricIDCacheStateRotated
rotate := state == benchDateMetricIDCacheStateRotated
metricIDMin := time.Now().UnixNano()
metricIDMax := metricIDMin + numMetricIDs*distance
for metricID := metricIDMin; metricID <= metricIDMax; metricID += distance {
c.Set(date, uint64(metricID))
if warmUp && !c.Has(date, uint64(metricID)) {
b.Fatalf("metricID not in cache: %d", metricID)
}
}
if rotate {
c.shards[rand.Intn(dateMetricIDCacheShardCount)].rotate()
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
if hitsOnly {
metricID := metricIDMin + rand.Int63n(numMetricIDs)*distance
for pb.Next() {
if !c.Has(date, uint64(metricID)) {
b.Fatalf("metricID not in cache: %d", metricID)
}
metricID += distance
if metricID > metricIDMax {
metricID = metricIDMin
}
}
} else {
// misses only
metricID := metricIDMax + distance
for pb.Next() {
if c.Has(date, uint64(metricID)) {
b.Fatalf("metricID is in cache: %d", metricID)
}
metricID += distance
}
}
})
b.ReportAllocs()
b.ReportMetric(float64(c.Stats().SizeBytes), "sizeBytes")
}
subB := func(numMetricIDs, distance int64, hitsOnly bool, state benchDateMetricIDCacheState) {
hitsOrMisses := " hits-only"
if !hitsOnly {
hitsOrMisses = "misses-only"
}
name := fmt.Sprintf("%s/%s/n%d/d%d", hitsOrMisses, state, numMetricIDs, distance)
b.Run(name, func(b *testing.B) {
f(b, numMetricIDs, distance, hitsOnly, state)
})
}
for _, hitsOnly := range []bool{true, false} {
for _, state := range benchDateMetricIDCacheStates {
for _, numMetricIDs := range []int64{100_000, 1_000_000, 10_000_000} {
for _, distance := range []int64{1, 10, 100} {
subB(numMetricIDs, distance, hitsOnly, state)
}
}
}
}
}