Skip to content

Commit

Permalink
ring/ring_test.go: add a benchmark test for updateRingState
Browse files Browse the repository at this point in the history
This was used to verify assumptions about complexity between
 RingCompare cases and running updateRingState with and without
updateRingMetrics

Signed-off-by: György Krajcsovits <[email protected]>
  • Loading branch information
krajorama committed Dec 29, 2021
1 parent 09cfb8f commit 0f62f3a
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions ring/ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -81,6 +82,72 @@ func generateKeys(r *rand.Rand, numTokens int, dest []uint32) {
}
}

func BenchmarkUpdateRingState(b *testing.B) {
for _, numInstances := range []int{50, 100, 500} {
for _, numZones := range []int{1, 3} {
for _, numTokens := range []int{128, 256, 512} {
for _, updateTokens := range []bool{false, true} {
b.Run(fmt.Sprintf("num instances = %d, num zones = %d, num tokens = %d, update tokens = %t", numInstances, numZones, numTokens, updateTokens), func(b *testing.B) {
benchmarkUpdateRingState(b, numInstances, numZones, numTokens, updateTokens)
})
}
}
}
}
}

func benchmarkUpdateRingState(b *testing.B, numInstances, numZones, numTokens int, updateTokens bool) {
cfg := Config{
KVStore: kv.Config{},
HeartbeatTimeout: 0, // get healthy stats
ReplicationFactor: 3,
ZoneAwarenessEnabled: true,
}

// create the ring to set up metrics, but do not start
registry := prometheus.NewRegistry()
ring, err := NewWithStoreClientAndStrategy(cfg, testRingName, testRingKey, nil, NewDefaultReplicationStrategy(), registry, log.NewNopLogger())
require.NoError(b, err)

// Make a random ring with N instances, and M tokens per ingests
// Also make a copy with different timestamps and one with different tokens
desc := NewDesc()
otherDesc := NewDesc()
takenTokens := []uint32{}
otherTakenTokens := []uint32{}
for i := 0; i < numInstances; i++ {
tokens := GenerateTokens(numTokens, takenTokens)
takenTokens = append(takenTokens, tokens...)
now := time.Now()
id := fmt.Sprintf("%d", i)
desc.AddIngester(id, fmt.Sprintf("instance-%d", i), strconv.Itoa(i), tokens, ACTIVE, now)
if updateTokens {
otherTokens := GenerateTokens(numTokens, otherTakenTokens)
otherTakenTokens = append(otherTakenTokens, otherTokens...)
otherDesc.AddIngester(id, fmt.Sprintf("instance-%d", i), strconv.Itoa(i), otherTokens, ACTIVE, now)
} else {
otherDesc.AddIngester(id, fmt.Sprintf("instance-%d", i), strconv.Itoa(i), tokens, JOINING, now)
}
}

if updateTokens {
require.Equal(b, Different, desc.RingCompare(otherDesc))
} else {
require.Equal(b, EqualButStatesAndTimestamps, desc.RingCompare(otherDesc))
}

flipFlop := true
b.ResetTimer()
for n := 0; n < b.N; n++ {
if flipFlop {
ring.updateRingState(desc)
} else {
ring.updateRingState(otherDesc)
}
flipFlop = !flipFlop
}
}

func TestDoBatchZeroInstances(t *testing.T) {
ctx := context.Background()
numKeys := 10
Expand Down

0 comments on commit 0f62f3a

Please sign in to comment.