|
| 1 | +package benchmark_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "github.com/testcontainers/testcontainers-go/modules/redpanda" |
| 11 | + "github.com/twmb/franz-go/pkg/kadm" |
| 12 | + "github.com/twmb/franz-go/pkg/kgo" |
| 13 | + |
| 14 | + "crypto/rand" |
| 15 | + "github.com/google/uuid" |
| 16 | + "github.com/testcontainers/testcontainers-go" |
| 17 | + "io" |
| 18 | + "log" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + totalEntries = 500000 |
| 23 | +) |
| 24 | + |
| 25 | +func BenchmarkPollRecords(b *testing.B) { |
| 26 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute) |
| 27 | + b.Cleanup(cancel) |
| 28 | + |
| 29 | + testcontainers.Logger = log.New(io.Discard, "", log.LstdFlags) |
| 30 | + |
| 31 | + ctnr, err := redpanda.RunContainer(ctx, redpanda.WithAutoCreateTopics()) |
| 32 | + require.NoError(b, err) |
| 33 | + |
| 34 | + broker, err := ctnr.KafkaSeedBroker(ctx) |
| 35 | + require.NoError(b, err) |
| 36 | + |
| 37 | + b.Cleanup(func() { |
| 38 | + if err := ctnr.Terminate(context.Background()); err != nil { |
| 39 | + b.Logf("failed terminating container: %v", err) |
| 40 | + } |
| 41 | + }) |
| 42 | + |
| 43 | + topicName := newRandomName("test-topic") |
| 44 | + require.NoError(b, populateTopic(ctx, b, broker, topicName)) |
| 45 | + |
| 46 | + b.ResetTimer() |
| 47 | + consumer, err := kgo.NewClient( |
| 48 | + kgo.SeedBrokers(broker), |
| 49 | + kgo.ConsumerGroup(newRandomName("test-consumer")), |
| 50 | + kgo.ConsumeTopics(topicName), |
| 51 | + kgo.BlockRebalanceOnPoll(), |
| 52 | + // use 10MB |
| 53 | + //kgo.FetchMaxPartitionBytes(10*1024*1024), |
| 54 | + ) |
| 55 | + |
| 56 | + require.NoError(b, err) |
| 57 | + defer consumer.CloseAllowingRebalance() |
| 58 | + |
| 59 | + consumedTotal := 0 |
| 60 | + |
| 61 | + for i := 0; i < b.N; i++ { |
| 62 | + fetches := consumer.PollRecords(ctx, 1000) |
| 63 | + require.NoError(b, fetches.Err0()) |
| 64 | + consumedTotal += len(fetches.Records()) |
| 65 | + if consumedTotal >= totalEntries { |
| 66 | + return |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func populateTopic(ctx context.Context, t *testing.B, broker string, topicName string) error { |
| 72 | + t.Log("Start topic population") |
| 73 | + producer, err := kgo.NewClient( |
| 74 | + kgo.SeedBrokers(broker), |
| 75 | + kgo.DefaultProduceTopic(topicName), |
| 76 | + ) |
| 77 | + require.NoError(t, err) |
| 78 | + defer producer.Close() |
| 79 | + |
| 80 | + adm := kadm.NewClient(producer) |
| 81 | + partitions := 10 |
| 82 | + tr, err := adm.CreateTopic(ctx, int32(partitions), 1, nil, topicName) |
| 83 | + require.NoError(t, tr.Err) |
| 84 | + require.NoError(t, err) |
| 85 | + |
| 86 | + data := make([]byte, 10240) // 10KB per message |
| 87 | + _, err = rand.Read(data) |
| 88 | + require.NoError(t, err) |
| 89 | + |
| 90 | + batchLen := 5000 |
| 91 | + for it := 0; it < totalEntries/batchLen; it++ { |
| 92 | + recs := make([]*kgo.Record, batchLen) |
| 93 | + for i := 0; i < batchLen; i++ { |
| 94 | + recs[i] = &kgo.Record{ |
| 95 | + Value: data, |
| 96 | + Key: []byte(fmt.Sprintf("key-%d", i%partitions)), |
| 97 | + } |
| 98 | + } |
| 99 | + require.NoError(t, producer.ProduceSync(ctx, recs...).FirstErr()) |
| 100 | + } |
| 101 | + return err |
| 102 | +} |
| 103 | + |
| 104 | +func newRandomName(baseName string) string { |
| 105 | + return baseName + "-" + uuid.NewString() |
| 106 | +} |
0 commit comments