-
Notifications
You must be signed in to change notification settings - Fork 3
/
engine_bench_test.go
152 lines (129 loc) · 3.75 KB
/
engine_bench_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package gokaf
import (
"bytes"
"fmt"
"testing"
)
var messageBytes = []struct {
input int
}{
{input: 2048}, // 2kB
{input: 32768}, // 32kB
{input: 131072}, // 128kB
}
var topicBufferSize = []struct {
input int
}{
{input: 0},
{input: 2048},
{input: 8192},
}
var topicsConsumersProdcuers = []struct {
numTopics int
numProducers int
numConsumers int
}{
{numTopics: 2, numProducers: 1, numConsumers: 2},
{numTopics: 2, numProducers: 2, numConsumers: 1},
{numTopics: 5, numProducers: 3, numConsumers: 6},
{numTopics: 5, numProducers: 6, numConsumers: 3},
}
func BenchmarkSingleTopic(b *testing.B) {
for _, ms := range messageBytes {
mbTitle := fmt.Sprintf("msg_%dkB", ms.input/1024)
for _, bts := range topicBufferSize {
b.Run(fmt.Sprintf("%s/buffer_%dkB", mbTitle, bts.input/1024), func(b *testing.B) {
// Create a new Engine
engine := NewEngine(mockLogger)
topicName := "BenchTopic"
_ = engine.RegisterTopic(topicName, bts.input)
counter := &counter{}
done := make(chan struct{})
mockHandler := func(receivedMsg interface{}) {
counter.Increment()
if counter.Value() == b.N {
close(done)
}
}
consumer, _ := engine.GetConsumer(topicName, mockHandler)
producer, _ := engine.GetProducer(topicName)
consumer.Run()
// Run the benchmark
b.ResetTimer()
go func() {
for i := 0; i < b.N; i++ {
go func() {
// Publish messages to topics
_ = producer.Publish(bytes.Repeat([]byte{'1'}, ms.input))
}()
}
}()
<-done
engine.Stop()
})
}
}
}
func BenchmarkMultipleTopics(b *testing.B) {
for _, ms := range messageBytes {
mbTitle := fmt.Sprintf("msg_%dkB", ms.input/1024)
for _, bts := range topicBufferSize {
btsTitle := fmt.Sprintf("buffer_%dkB", bts.input/1024)
for _, tpc := range topicsConsumersProdcuers {
tpcTitle := fmt.Sprintf("%dtop_%dprod_%dcon", tpc.numTopics, tpc.numProducers, tpc.numConsumers)
b.Run(fmt.Sprintf("%s/%s/%s", mbTitle, btsTitle, tpcTitle), func(b *testing.B) {
// Create a new Engine
engine := NewEngine(mockLogger)
counters := []*counter{}
dones := []chan struct{}{}
for topicIndex := 0; topicIndex < tpc.numTopics; topicIndex++ {
// Create a shared counter for the topic and consumers
topicName := fmt.Sprintf("Topic%d", topicIndex)
_ = engine.RegisterTopic(topicName, bts.input)
counters = append(counters, &counter{})
dones = append(dones, make(chan struct{}))
// Initialize consumers
for i := 0; i < tpc.numConsumers; i++ {
go func(topicIndex int) {
mockHandler := func(receivedMsg interface{}) {
counters[topicIndex].Increment()
if counters[topicIndex].Value() == tpc.numProducers*b.N {
select {
case <-dones[topicIndex]:
// Channel is already closed
default:
// Channel is open, so close it
close(dones[topicIndex])
}
}
}
consumer, _ := engine.GetConsumer(topicName, mockHandler)
consumer.Run()
<-dones[topicIndex]
}(topicIndex)
}
}
b.ResetTimer()
for topicIndex := 0; topicIndex < tpc.numTopics; topicIndex++ {
topicName := fmt.Sprintf("Topic%d", topicIndex)
// Initialize producers
for i := 0; i < tpc.numProducers; i++ {
go func(topicName string) {
for i := 0; i < b.N; i++ {
// Publish messages to topics
producer, _ := engine.GetProducer(topicName)
_ = producer.Publish(bytes.Repeat([]byte{'1'}, ms.input))
}
}(topicName)
}
}
for topicIndex := 0; topicIndex < tpc.numTopics; topicIndex++ {
<-dones[topicIndex]
}
// Stop the engine
engine.Stop()
})
}
}
}
}