(b *testing.B, numInstances, numKeys int)
| 60 | } |
| 61 | |
| 62 | func 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 | |
| 113 | func generateKeys(r *rand.Rand, numTokens int, dest []uint32) { |
| 114 | for i := 0; i < numTokens; i++ { |
no test coverage detected