-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.go
284 lines (244 loc) · 6.72 KB
/
bench.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"context"
"encoding/csv"
"fmt"
"log"
"os"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"streambench/brokers"
"gonum.org/v1/gonum/stat"
)
var (
txN int64
rxN int64
)
type Producer interface {
Produce(ctx context.Context, topic, key, value string) error
}
type Consumer interface {
Consume(ctx context.Context, topic string) (chan brokers.Message, error)
}
func runTopic(ctx context.Context, msgSize int, N, M, rate, producers int, brokerType, brokerURLs, topic string) []time.Duration {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
latencies := make([]time.Duration, 0, N)
start := time.Now().UnixNano()
c := NewClient(brokerType, brokerURLs, topic)
ch, err := c.Consume(ctx, topic)
if err != nil {
panic(err)
}
cwg := sync.WaitGroup{}
cwg.Add(1)
// Consume
go func() {
defer cwg.Done()
i := 0
for msg := range ch {
ns, err := strconv.ParseInt(msg.Value[:19], 10, 64)
if err != nil {
panic(err)
}
// skip stale or future messages
if ns < start || ns >= time.Now().UnixNano() {
continue
}
atomic.AddInt64(&rxN, 1)
latency := time.Since(time.Unix(0, ns))
latencies = append(latencies, latency)
i++
}
}()
// Produce.
pwg := sync.WaitGroup{}
for pidx := 0; pidx <= producers; pidx++ {
pwg.Add(1)
go func() {
defer pwg.Done()
p := NewClient(brokerType, brokerURLs, "")
i := 0
lastProduced := time.Time{}
for {
// Limit produce rate.
if !lastProduced.IsZero() {
diff := time.Until(lastProduced.Add(time.Second / time.Duration(rate)))
if diff > 0 {
time.Sleep(diff)
}
}
var b strings.Builder
b.Grow(msgSize)
ts := time.Now()
tsStr := strconv.FormatInt(ts.UnixNano(), 10)
b.WriteString(tsStr)
for n := 0; n < msgSize-len(tsStr); n++ {
b.WriteByte(42)
}
select {
case <-ctx.Done():
return
default:
}
if err := p.Produce(ctx, topic, "", b.String()); err != nil {
log.Printf("failed to produce: %v", err)
break
}
atomic.AddInt64(&txN, 1)
lastProduced = ts
i++
// Stop if number of messages is reached.
if N > 0 && i >= N {
log.Printf("Producer %s stopping due to message count limit", topic)
break
}
// Stop if number of minutes is reached.
if M > 0 && (time.Now().UnixNano()-start)/int64(time.Minute) >= int64(M) {
log.Printf("Producer %s stopping due to time limit", topic)
break
}
}
}()
}
pwg.Wait() // Wait for producers to finish.
cancel()
// cwg.Wait() // Wait for consumer to finish.
return latencies
}
type Client interface {
Producer
Consumer
}
// NewClient returns Producer it topic is empty, and Consumer otherwize.
func NewClient(brokerType, brokerURLs, topic string) Client {
switch brokerType {
case "pulsar":
k, err := brokers.NewPulsar(brokerURLs, topic)
if err != nil {
log.Fatalf("failed to create Pulsar client: %v", err)
}
return k
case "nats":
n, err := brokers.NewNats(brokerURLs, "s") // hardcoded stream name
if err != nil {
log.Fatalf("failed to create Nats JetStream client: %v", err)
}
return n
case "kafka":
k := brokers.NewKafka(brokerURLs, topic)
return k
case "redpanda":
rp, err := brokers.NewRedPanda(brokerURLs, topic)
if err != nil {
log.Fatalf("failed to create RedPanda client: %v", err)
}
return rp
}
log.Fatalf("unknown broker type: %s", brokerType)
return nil
}
func RunBench(ctx context.Context, msgSize int, N, M, rate, producers int, brokerType, brokerURLs, topics string) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
latencies := make([]time.Duration, 0, N*len(strings.Split(topics, ",")))
start := time.Now()
wg := sync.WaitGroup{}
ch := make(chan []time.Duration, 10)
for _, topic := range strings.Split(topics, ",") {
wg.Add(1)
go func(topic string) {
defer wg.Done()
ch <- runTopic(ctx, msgSize, N, M, rate, producers, brokerType, brokerURLs, topic)
}(topic)
}
// Append latencies.
wgl := sync.WaitGroup{}
wgl.Add(1)
go func() {
defer wgl.Done()
for ls := range ch {
// Trim first 10% and last 10% of latencies slice
// to account for broker "warm up" time and shutdown part (some producers can
// finish earlier than others that will make tail of the latencies more sparse).
cut := len(ls) / 10
latencies = append(latencies, ls[cut:len(ls)-cut]...)
}
}()
// Print progress.
go func() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
}
rx := atomic.LoadInt64(&rxN)
tx := atomic.LoadInt64(&txN)
mps := 0
mbps := 0.0
elapsed := time.Since(start)
if rx > 0 && elapsed.Seconds() >= 1 {
mps = int(float64(rx) / elapsed.Seconds())
mbps = float64(rx*int64(msgSize)) / elapsed.Seconds() / 1024 / 1024
}
log.Printf("Produced: %d, Consumed: %d (%d messages/sec, %.2f Mb/sec, running for %v)", tx, rx, mps, mbps, elapsed)
}
}()
wg.Wait()
cancel()
close(ch)
wgl.Wait()
elapsed := time.Since(start)
N = int(atomic.LoadInt64(&rxN))
if N == 0 {
log.Printf("No messages received in %v", elapsed)
return
}
fmt.Printf("Message throughput: %.2f messages/sec\n", float64(N)/elapsed.Seconds())
fmt.Printf("Data throughput: %f Mb/sec\n", (float64(N*msgSize)/elapsed.Seconds())/1024/1024)
flats := make([]float64, len(latencies))
for i, l := range latencies {
flats[i] = float64(l) / float64(time.Millisecond)
}
sort.Slice(latencies, func(i, j int) bool {
return latencies[i] < latencies[j]
})
N = len(latencies) // This one is smaller than rxN because we trim first 10% of warmup time.
fmt.Printf("Min latency: %v\n", ms(latencies[0]))
fmt.Printf("P50 latency: %v\n", ms(latencies[N/2]))
fmt.Printf("P90 latency: %v\n", ms(latencies[N-N/10]))
fmt.Printf("P99 latency: %v\n", ms(latencies[N-N/100]))
fmt.Printf("P99.9 latency: %v\n", ms(latencies[N-N/1000]))
fmt.Printf("Max latency: %v\n", ms(latencies[N-1]))
stddev := stat.StdDev(flats, nil)
fmt.Printf("Latency StdDev: %.6f\n", stddev)
fmt.Printf("Latency StdErr: %.6f\n", stat.StdErr(stddev, float64(N)))
fmt.Printf("Total elapsed time: %v\n", time.Since(start))
fmt.Printf("Commandline arguments: %s\n", strings.Join(os.Args[1:], " "))
f, err := os.OpenFile("latencies.csv", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
defer f.Close()
if err != nil {
log.Fatalf("failed to open file: %v", err)
}
w := csv.NewWriter(f)
defer w.Flush()
for i, f := range flats {
// Sample 10% of latencies (to moving gigabytes of floats over network).
if i%10 > 0 {
continue
}
if err := w.Write([]string{strconv.FormatFloat(f, 'g', 5, 64)}); err != nil {
log.Fatalf("failed to write csv: %v", err)
}
}
}
func ms(d time.Duration) string {
return fmt.Sprintf("%d ms.", d.Milliseconds())
}