-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.go
125 lines (108 loc) · 2.41 KB
/
benchmark.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"fmt"
"github.com/Shopify/sarama"
"github.com/akamensky/argparse"
"github.com/akamensky/go-kafka"
"os"
"strings"
"sync"
"time"
)
func main() {
p := argparse.NewParser("benchmark", "Tool to benchmark this library")
brokersArg := p.String("b", "brokers", &argparse.Options{
Required: false,
Help: "Comma separated list of brokers",
Default: "localhost:9092",
})
topicArg := p.String("t", "topic-name", &argparse.Options{
Required: false,
Help: "Name of topic to use for benchmarking",
Default: "benchmark",
})
err := p.Parse(os.Args)
if err != nil {
panic(err)
}
brokers := strings.Split(*brokersArg, ",")
topic := *topicArg
go produceTracking()
go produce(brokers, topic)
go consumeTracking()
go consume(brokers, topic)
wg := &sync.WaitGroup{}
wg.Add(1)
wg.Wait()
}
var produceCounter uint64 = 0
func produce(brokers []string, topic string) {
config := sarama.NewConfig()
config.Version = sarama.V2_3_0_0
config.Producer.Partitioner = sarama.NewRoundRobinPartitioner
config.Producer.Return.Errors = true
config.Producer.RequiredAcks = sarama.WaitForLocal
producer, err := kafka.NewAsyncProducer(
&kafka.ProducerOptions{Topic: topic, Brokers: brokers},
config,
)
if err != nil {
panic(err)
}
go func() {
for err := range producer.Errors() {
if err != nil {
panic(err)
}
}
}()
for {
producer.Send(&kafka.Message{
Key: nil,
Value: []byte("test"),
})
produceCounter++
}
}
func produceTracking() {
for {
time.Sleep(1 * time.Second)
fmt.Printf("Produced: %d\n", produceCounter)
produceCounter = 0
}
}
var consumeCounter uint64 = 0
func consume(brokers []string, topic string) {
config := sarama.NewConfig()
config.Version = sarama.V2_3_0_0
config.Consumer.Return.Errors = true
config.Consumer.Group.Rebalance.Strategy = sarama.BalanceStrategySticky
config.Consumer.Offsets.AutoCommit.Enable = true
config.Consumer.Offsets.Initial = sarama.OffsetOldest
consumer, err := kafka.NewConsumer(
&kafka.ConsumerOptions{Topic: topic, Brokers: brokers, GroupName: "benchmark"},
config,
)
if err != nil {
panic(err)
}
go func() {
for err := range consumer.Errors() {
if err != nil {
panic(err)
}
}
}()
for {
for range consumer.Receive() {
consumeCounter++
}
}
}
func consumeTracking() {
for {
time.Sleep(1 * time.Second)
fmt.Printf("Consumed: %d\n", consumeCounter)
consumeCounter = 0
}
}