MCPcopy
hub / github.com/grafana/dskit / benchmarkBatch

Function benchmarkBatch

ring/ring_test.go:62–111  ·  view source on GitHub ↗
(b *testing.B, numInstances, numKeys int)

Source from the content-addressed store, hash-verified

60}
61
62func benchmarkBatch(b *testing.B, numInstances, numKeys int) {
63 seed := time.Now().UnixNano()
64 rnd := rand.New(rand.NewSource(seed))
65 gen := NewRandomTokenGeneratorWithSeed(seed)
66
67 // Make a random ring with N instances, and M tokens per ingests
68 desc := NewDesc()
69 var takenTokens []uint32
70 for i := 0; i < numInstances; i++ {
71 tokens := gen.GenerateTokens(numTokens, takenTokens)
72 takenTokens = append(takenTokens, tokens...)
73 desc.AddIngester(fmt.Sprintf("%d", i), fmt.Sprintf("instance-%d", i), strconv.Itoa(i), tokens, ACTIVE, time.Now(), false, time.Time{}, nil)
74 }
75
76 cfg := Config{}
77 flagext.DefaultValues(&cfg)
78 cfg.HeartbeatTimeout = time.Hour // A minute is not enough to run all benchmarks.
79 r, err := NewWithStoreClientAndStrategy(cfg, testRingName, testRingKey, nil, NewDefaultReplicationStrategy(), prometheus.NewRegistry(), log.NewNopLogger())
80 require.NoError(b, err)
81 r.updateRingState(desc)
82
83 ctx := context.Background()
84 callback := func(InstanceDesc, []int) error { return deepStack(64) }
85 keys := make([]uint32, numKeys)
86 // Generate a batch of N random keys, and look them up
87 b.ResetTimer()
88
89 // run with `benchstat -col /go` to get stats on different go= options.
90 // ref: https://pkg.go.dev/golang.org/x/perf/cmd/benchstat
91
92 b.Run("go=default", func(b *testing.B) {
93 for i := 0; i < b.N; i++ {
94 generateKeys(rnd, numKeys, keys)
95 err := DoBatchWithOptions(ctx, Write, r, keys, callback, DoBatchOptions{})
96 require.NoError(b, err)
97 }
98 })
99
100 b.Run("go=concurrency.ReusableGoroutinesPool", func(b *testing.B) {
101 pool := concurrency.NewReusableGoroutinesPool(100)
102 b.Cleanup(pool.Close)
103
104 b.ResetTimer()
105 for i := 0; i < b.N; i++ {
106 generateKeys(rnd, numKeys, keys)
107 err := DoBatchWithOptions(ctx, Write, r, keys, callback, DoBatchOptions{Go: pool.Go})
108 require.NoError(b, err)
109 }
110 })
111}
112
113func generateKeys(r *rand.Rand, numTokens int, dest []uint32) {
114 for i := 0; i < numTokens; i++ {

Callers 3

BenchmarkBatch10x100Function · 0.85
BenchmarkBatch100x100Function · 0.85
BenchmarkBatch100x1000Function · 0.85

Calls 13

AddIngesterMethod · 0.95
DefaultValuesFunction · 0.92
NewDescFunction · 0.85
deepStackFunction · 0.85
generateKeysFunction · 0.85
DoBatchWithOptionsFunction · 0.85
updateRingStateMethod · 0.80
RunMethod · 0.80

Tested by

no test coverage detected