forked from gguridi/benchmark-kafka-go-clients
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
129 lines (119 loc) · 3.53 KB
/
main_test.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
126
127
128
129
package main
import (
"flag"
"fmt"
"github.com/gguridi/benchmark-kafka-go-clients/confluent"
"github.com/gguridi/benchmark-kafka-go-clients/kafkago"
"github.com/gguridi/benchmark-kafka-go-clients/sarama"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
var _ = Describe("Benchmarks", func() {
It("checks lag", func() {
flag.Parse()
consumer := confluent.NewConsumer(viper.GetString("kafka.brokers"), true)
confluent.PreparePoll(consumer, 1)()
Expect(confluent.Lag(consumer)).To(Equal(NumMessages))
})
Measure("producer", func(b Benchmarker) {
flag.Parse()
name := fmt.Sprintf("%s producing %d messages of %d bytes size", Library, NumMessages, MessageSize)
switch Library {
case "confluent":
producer := confluent.NewProducer(viper.GetString("kafka.brokers"))
process := confluent.Prepare(producer, GenMessage(), NumMessages)
b.Time(name, func() {
process()
<-confluent.Done
})
producer.Close()
break
case "sarama":
producer := sarama.NewProducer(viper.GetString("kafka.brokers"))
process := sarama.Prepare(producer, GenMessage(), NumMessages)
b.Time(name, func() {
process()
<-sarama.Done
})
if err := producer.Close(); err != nil {
log.WithError(err).Panic("Unable to close the producer")
}
break
case "kafkago":
writer := kafkago.NewProducer(viper.GetString("kafka.brokers"))
process := kafkago.Prepare(writer, GenMessage(), NumMessages)
b.Time(name, func() {
process()
})
if err := writer.Close(); err != nil {
log.WithError(err).Panic("Unable to close the producer")
}
break
default:
log.Panicf("Unable to find the libray %+v", Library)
}
}, 3)
Context("prepopulate", func() {
var (
initialised = false
)
BeforeEach(func() {
if !initialised {
log.Infof("Prepopulating Kafka with %d messages of %d bytes", NumMessages, MessageSize)
producer := sarama.NewProducer(viper.GetString("kafka.brokers"))
process := sarama.Prepare(producer, GenMessage(), int(float64(NumMessages)*1.5))
process()
<-sarama.Done
producer.Close()
log.Infof("Finished prepopulating Kafka")
}
initialised = true
})
Measure("consumer", func(b Benchmarker) {
flag.Parse()
name := fmt.Sprintf("%s consuming %d messages", Library, NumMessages)
switch Library {
case "confluent-poll":
consumer := confluent.NewConsumer(viper.GetString("kafka.brokers"), true)
process := confluent.PreparePoll(consumer, NumMessages)
b.Time(name, func() {
process()
})
consumer.Close()
break
case "confluent-channel":
consumer := confluent.NewConsumer(viper.GetString("kafka.brokers"), false)
process := confluent.PrepareChannel(consumer, NumMessages)
b.Time(name, func() {
process()
})
consumer.Close()
break
case "sarama":
consumer := sarama.NewConsumer(viper.GetString("kafka.brokers"), NumMessages)
process := sarama.PrepareConsume(consumer)
b.Time(name, func() {
process()
})
if err := consumer.Client.Close(); err != nil {
log.WithError(err).Panic("Unable to close the consumer")
}
break
case "kafkago":
reader := kafkago.NewReader(viper.GetString("kafka.brokers"))
process := kafkago.PrepareReader(reader, NumMessages)
b.Time(name, func() {
process()
})
if err := reader.Close(); err != nil {
log.WithError(err).Panic("Unable to close the consumer")
}
break
default:
log.Panicf("Unable to find the libray %+v", Library)
}
}, 1)
})
})