forked from nats-io/latency-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
latency_new.go
435 lines (379 loc) · 13.6 KB
/
latency_new.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package main
import (
"crypto/rand"
"encoding/binary"
"flag"
"fmt"
"io"
"log"
"math"
"os"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/codahale/hdrhistogram"
"github.com/nats-io/go-nats"
"github.com/tylertreat/hdrhistogram-writer"
)
// Test Parameters
var (
ServerA string
ServerB string
TargetPubRate int
MsgSize int
NumPubs int
TestDuration time.Duration
HistFile string
Secure bool
TLSca string
TLSkey string
TLScert string
Publishers int
Subjects int
MetricsFile string
)
var usageStr = `
Usage: latency-tests [options]
Test Options:
-sa <url> ServerA (Publish) (default: nats://localhost:4222)
-sb <url> ServerB (Subscribe) (default: nats://localhost:4222)
-sz <int> Message size in bytes (default: 8)
-tr <int> Rate in msgs/sec (default: 1000)
-tt <string> Test duration (default: 5s)
-hist <file> Histogram output file
-secure Enable TLS without verfication (default: false)
-tls_ca <string> TLS Certificate CA file
-tls_key <file> TLS Private Key
-tls_cert <file> TLS Certificate
-pubs <int> The number of publishers
-subjs <int> The numer of subjects
-filename <string> The name of file to store the latency metrics
`
func usage() {
log.Fatalf(usageStr + "\n")
}
// waitForRoute tests a subscription in the server to ensure subject interest
// has been propagated between servers. Otherwise, we may miss early messages
// when testing with clustered servers and the test will hang.
func waitForRoute(pnc, snc *nats.Conn) {
// No need to continue if using one server
if strings.Compare(pnc.ConnectedServerId(), snc.ConnectedServerId()) == 0 {
return
}
// Setup a test subscription to let us know when a message has been received.
// Use a new inbox subject as to not skew results
var routed int32
subject := nats.NewInbox()
sub, err := snc.Subscribe(subject, func(msg *nats.Msg) {
atomic.AddInt32(&routed, 1)
})
if err != nil {
log.Fatalf("Couldn't subscribe to test subject %s: %v", subject, err)
}
defer sub.Unsubscribe()
snc.Flush()
// Periodically send messages until the test subscription receives
// a message. Allow for two seconds.
start := time.Now()
for atomic.LoadInt32(&routed) == 0 {
if time.Since(start) > (time.Second * 2) {
log.Fatalf("Couldn't receive end-to-end test message.")
}
if err = pnc.Publish(subject, nil); err != nil {
log.Fatalf("Couldn't publish to test subject %s: %v", subject, err)
}
time.Sleep(10 * time.Millisecond)
}
}
func main() {
start := time.Now()
flag.StringVar(&ServerA, "sa", nats.DefaultURL, "ServerA - Publisher")
flag.StringVar(&ServerB, "sb", nats.DefaultURL, "ServerB - Subscriber")
flag.IntVar(&TargetPubRate, "tr", 1000, "Target Publish Rate")
flag.IntVar(&MsgSize, "sz", 8, "Message Payload Size")
flag.DurationVar(&TestDuration, "tt", 5*time.Second, "Target Test Time")
flag.StringVar(&HistFile, "hist", "", "Histogram and Raw Output")
flag.BoolVar(&Secure, "secure", false, "Use a TLS Connection w/o verification")
flag.StringVar(&TLSkey, "tls_key", "", "Private key file")
flag.StringVar(&TLScert, "tls_cert", "", "Certificate file")
flag.StringVar(&TLSca, "tls_ca", "", "Certificate CA file")
flag.IntVar(&Publishers, "pubs", 10, "Number of Publishers")
flag.IntVar(&Subjects, "subjs", 10, "Number of subjects")
flag.StringVar(&MetricsFile, "filename", "file.txt", "Metrics output filename")
log.SetFlags(0)
flag.Usage = usage
flag.Parse()
NumPubs = int(TestDuration/time.Second) * TargetPubRate
if MsgSize < 8 {
log.Fatalf("Message Payload Size must be at least %d bytes\n", 8)
}
// Setup connection options
var opts []nats.Option
if Secure {
opts = append(opts, nats.Secure())
}
if TLSca != "" {
opts = append(opts, nats.RootCAs(TLSca))
}
if TLScert != "" {
opts = append(opts, nats.ClientCert(TLScert, TLSkey))
}
c1, err := nats.Connect(ServerA, opts...)
if err != nil {
log.Fatalf("Could not connect to ServerA: %v", err)
}
c2, err := nats.Connect(ServerB, opts...)
if err != nil {
log.Fatalf("Could not connect to ServerB: %v", err)
}
// Do some quick RTT calculations
log.Println("==============================")
now := time.Now()
c1.Flush()
log.Printf("Pub Server RTT : %v\n", fmtDur(time.Since(now)))
now = time.Now()
c2.Flush()
log.Printf("Sub Server RTT : %v\n", fmtDur(time.Since(now)))
// Duration tracking
durations := make([]time.Duration, 0, NumPubs)
latestDurations := make([]time.Duration, 0, NumPubs)
// Wait for all messages to be received.
var wg sync.WaitGroup
wg.Add(1)
//Random subject (to run multiple tests in parallel)
subject := nats.NewInbox()
//Topics for stress
subjects := make([]string, Subjects)
for i := 0; i < Subjects; i++ {
subjects[i] = nats.NewInbox()
}
// Count the messages.
received := 0
// Async Subscriber (Runs in its own Goroutine)
c2.Subscribe(subject, func(msg *nats.Msg) {
sendTime := int64(binary.LittleEndian.Uint64(msg.Data))
durations = append(durations, time.Duration(time.Now().UnixNano()-sendTime))
latestDurations = append(latestDurations, time.Duration(time.Now().UnixNano()-sendTime))
received++
if received >= NumPubs {
wg.Done()
}
})
// One subscriber for each subject
for i := 0; i < Subjects; i++ {
c2.Subscribe(subjects[i], func(msg *nats.Msg) {
sendTime := int64(binary.LittleEndian.Uint64(msg.Data))
if sendTime == 0 {
log.Printf("I am subject : %v\n", sendTime)
}
})
}
// Make sure interest is set for subscribe before publish since a different connection.
c2.Flush()
// wait for routes to be established so we get every message
waitForRoute(c1, c2)
log.Printf("Message Payload: %v\n", byteSize(MsgSize))
log.Printf("Target Duration: %v\n", TestDuration)
log.Printf("Target Msgs/Sec: %v\n", TargetPubRate)
log.Printf("Target Band/Sec: %v\n", byteSize(TargetPubRate*MsgSize*2))
log.Println("==============================")
// Random payload
data := make([]byte, MsgSize)
io.ReadFull(rand.Reader, data)
// Set ticker to print latency metrics dynamically
ticker := time.NewTicker(3000 * time.Millisecond)
stop := make(chan bool, 1)
go func() {
for {
select {
case <-ticker.C:
sort.Slice(latestDurations, func(i, j int) bool { return latestDurations[i] < latestDurations[j] })
if len(latestDurations) == 0 {
continue
}
h := hdrhistogram.New(1, int64(latestDurations[len(latestDurations)-1]), 5)
for _, d := range latestDurations {
h.RecordValue(int64(d))
}
f, err := os.Create(MetricsFile)
if err != nil {
log.Println("Error: %v", err)
return
}
avg_latency := averageLatency(latestDurations)
latestDurations = make([]time.Duration, 0, NumPubs)
fmt.Fprintf(f, "AverageLatency %v\n", avg_latency)
fmt.Fprintf(f, "Percentile10 %v\n", float64(time.Duration(h.ValueAtQuantile(10)).Nanoseconds())/1000000.0)
fmt.Fprintf(f, "Percentile50 %v\n", float64(time.Duration(h.ValueAtQuantile(50)).Nanoseconds())/1000000.0)
fmt.Fprintf(f, "Percentile75 %v\n", float64(time.Duration(h.ValueAtQuantile(75)).Nanoseconds())/1000000.0)
fmt.Fprintf(f, "Percentile90 %v\n", float64(time.Duration(h.ValueAtQuantile(90)).Nanoseconds())/1000000.0)
fmt.Fprintf(f, "Percentile99 %v\n", float64(time.Duration(h.ValueAtQuantile(99)).Nanoseconds())/1000000.0)
fmt.Fprintf(f, "Percentile99.99 %v\n", float64(time.Duration(h.ValueAtQuantile(99.99)).Nanoseconds())/1000000.0)
fmt.Fprintf(f, "Percentile99.999 %v\n", float64(time.Duration(h.ValueAtQuantile(99.999)).Nanoseconds())/1000000.0)
fmt.Fprintf(f, "Percentile99.9999 %v\n", float64(time.Duration(h.ValueAtQuantile(99.9999)).Nanoseconds())/1000000.0)
fmt.Fprintf(f, "Percentile99.99999 %v\n", float64(time.Duration(h.ValueAtQuantile(99.99999)).Nanoseconds())/1000000.0)
fmt.Fprintf(f, "Percentile100.0 %v\n", float64(time.Duration(h.ValueAtQuantile(100.0)).Nanoseconds())/1000000.0)
f.Close()
log.Printf("HDR Percentiles:\n")
log.Printf("10: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(10))))
log.Printf("50: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(50))))
log.Printf("75: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(75))))
log.Printf("90: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(90))))
log.Printf("99: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(99))))
log.Printf("99.99: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(99.99))))
log.Printf("99.999: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(99.999))))
log.Printf("99.9999: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(99.9999))))
log.Printf("99.99999: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(99.99999))))
log.Printf("100: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(100.0))))
log.Printf("Average Latency: %v\n", avg_latency)
log.Println("==============================")
case <-stop:
log.Println("EXIT")
return
}
}
}()
// For publish throttling
delay := time.Second / time.Duration(TargetPubRate)
pubStart := time.Now()
// Throttle logic, crude I know, but works better then time.Ticker.
adjustAndSleep := func(count int) {
r := rps(count, time.Since(pubStart))
adj := delay / 20 // 5%
if adj == 0 {
adj = 1 // 1ns min
}
if r < TargetPubRate {
delay -= adj
} else if r > TargetPubRate {
delay += adj
}
if delay < 0 {
delay = 0
}
time.Sleep(delay)
}
for j := 0; j < Publishers; j++ {
go func() {
for i := 0; i < NumPubs; i++ {
now := time.Now()
// Place the send time in the front of the payload.
binary.LittleEndian.PutUint64(data[0:], uint64(now.UnixNano()))
for k := 0; k < Subjects; k++ {
c1.Publish(subjects[k], data)
}
}
}()
}
for i := 0; i < NumPubs; i++ {
now := time.Now()
// Place the send time in the front of the payload.
binary.LittleEndian.PutUint64(data[0:], uint64(now.UnixNano()))
c1.Publish(subject, data)
adjustAndSleep(i + 1)
}
pubDur := time.Since(pubStart)
wg.Wait()
subDur := time.Since(pubStart)
ticker.Stop()
stop <- true
time.Sleep(1000 * time.Millisecond)
// If we are writing to files, save the original unsorted data
if HistFile != "" {
if err := writeRawFile(HistFile+".raw", durations); err != nil {
log.Printf("Unable to write raw output file: %v", err)
}
}
sort.Slice(durations, func(i, j int) bool { return durations[i] < durations[j] })
h := hdrhistogram.New(1, int64(durations[len(durations)-1]), 5)
for _, d := range durations {
h.RecordValue(int64(d))
}
log.Printf("HDR Percentiles:\n")
log.Printf("10: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(10))))
log.Printf("50: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(50))))
log.Printf("75: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(75))))
log.Printf("90: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(90))))
log.Printf("99: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(99))))
log.Printf("99.99: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(99.99))))
log.Printf("99.999: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(99.999))))
log.Printf("99.9999: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(99.9999))))
log.Printf("99.99999: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(99.99999))))
log.Printf("100: %v\n", fmtDur(time.Duration(h.ValueAtQuantile(100.0))))
log.Println("==============================")
if HistFile != "" {
pctls := histwriter.Percentiles{10, 25, 50, 75, 90, 99, 99.9, 99.99, 99.999, 99.9999, 99.99999, 100.0}
histwriter.WriteDistributionFile(h, pctls, 1.0/1000000.0, HistFile+".histogram")
}
// Print results
log.Printf("Actual Msgs/Sec: %d\n", rps(NumPubs, pubDur))
log.Printf("Actual Band/Sec: %v\n", byteSize(rps(NumPubs, pubDur)*MsgSize*2))
log.Printf("Minimum Latency: %v", fmtDur(durations[0]))
log.Printf("Median Latency : %v", fmtDur(getMedian(durations)))
log.Printf("Maximum Latency: %v", fmtDur(durations[len(durations)-1]))
log.Printf("1st Sent Wall Time : %v", fmtDur(pubStart.Sub(start)))
log.Printf("Last Sent Wall Time: %v", fmtDur(pubDur))
log.Printf("Last Recv Wall Time: %v", fmtDur(subDur))
}
const fsecs = float64(time.Second)
func rps(count int, elapsed time.Duration) int {
return int(float64(count) / (float64(elapsed) / fsecs))
}
// Just pretty print the byte sizes.
func byteSize(n int) string {
sizes := []string{"B", "K", "M", "G", "T"}
base := float64(1024)
if n < 10 {
return fmt.Sprintf("%d%s", n, sizes[0])
}
e := math.Floor(logn(float64(n), base))
suffix := sizes[int(e)]
val := math.Floor(float64(n)/math.Pow(base, e)*10+0.5) / 10
f := "%.0f%s"
if val < 10 {
f = "%.1f%s"
}
return fmt.Sprintf(f, val, suffix)
}
func logn(n, b float64) float64 {
return math.Log(n) / math.Log(b)
}
// Make time durations a bit prettier.
func fmtDur(t time.Duration) time.Duration {
// e.g 234us, 4.567ms, 1.234567s
return t.Truncate(time.Microsecond)
}
func getMedian(values []time.Duration) time.Duration {
l := len(values)
if l == 0 {
log.Fatalf("empty set")
}
if l%2 == 0 {
return (values[l/2-1] + values[l/2]) / 2
}
return values[l/2]
}
// writeRawFile creates a file with a list of recorded latency
// measurements, one per line.
func writeRawFile(filePath string, values []time.Duration) error {
f, err := os.Create(filePath)
if err != nil {
return err
}
defer f.Close()
for _, value := range values {
fmt.Fprintf(f, "%f\n", float64(value.Nanoseconds())/1000000.0)
}
return nil
}
// averageLatency calculates the average of a list of recorded latency
// measurements in msec
func averageLatency(values []time.Duration) float64 {
sum := 0.0
for _, value := range values {
sum += float64(value.Nanoseconds())/1000000.0
}
return sum / float64(len(values))
}