This repository has been archived by the owner on Jun 9, 2019. It is now read-only.
/
main.go
172 lines (139 loc) · 4.15 KB
/
main.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
// Copyright 2015-2018 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"sync"
"time"
"github.com/nats-io/go-nats"
"github.com/nats-io/go-nats/bench"
)
// Some sane defaults
const (
DefaultNumMsgs = 100000
DefaultNumPubs = 1
DefaultNumSubs = 0
DefaultMessageSize = 128
)
func usage() {
log.Fatalf("Usage: nats-bench [-s server (%s)] [--tls] [-np NUM_PUBLISHERS] [-ns NUM_SUBSCRIBERS] [-n NUM_MSGS] [-ms MESSAGE_SIZE] [-csv csvfile] <subject>\n", nats.DefaultURL)
}
var benchmark *bench.Benchmark
func main() {
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
var tls = flag.Bool("tls", false, "Use TLS Secure Connection")
var numPubs = flag.Int("np", DefaultNumPubs, "Number of Concurrent Publishers")
var numSubs = flag.Int("ns", DefaultNumSubs, "Number of Concurrent Subscribers")
var numMsgs = flag.Int("n", DefaultNumMsgs, "Number of Messages to Publish")
var msgSize = flag.Int("ms", DefaultMessageSize, "Size of the message.")
var csvFile = flag.String("csv", "", "Save bench data to csv file")
var userCreds = flag.String("creds", "", "User Credentials File")
log.SetFlags(0)
flag.Usage = usage
flag.Parse()
args := flag.Args()
if len(args) != 1 {
usage()
}
if *numMsgs <= 0 {
log.Fatal("Number of messages should be greater than zero.")
}
// Connect Options.
opts := []nats.Option{nats.Name("NATS Benchmark")}
// Use UserCredentials
if *userCreds != "" {
opts = append(opts, nats.UserCredentials(*userCreds))
}
// Use TLS specfied
if *tls {
opts = append(opts, nats.Secure(nil))
}
benchmark = bench.NewBenchmark("NATS", *numSubs, *numPubs)
var startwg sync.WaitGroup
var donewg sync.WaitGroup
donewg.Add(*numPubs + *numSubs)
// Run Subscribers first
startwg.Add(*numSubs)
for i := 0; i < *numSubs; i++ {
nc, err := nats.Connect(*urls, opts...)
if err != nil {
log.Fatalf("Can't connect: %v\n", err)
}
defer nc.Close()
go runSubscriber(nc, &startwg, &donewg, *numMsgs, *msgSize)
}
startwg.Wait()
// Now Publishers
startwg.Add(*numPubs)
pubCounts := bench.MsgsPerClient(*numMsgs, *numPubs)
for i := 0; i < *numPubs; i++ {
nc, err := nats.Connect(*urls, opts...)
if err != nil {
log.Fatalf("Can't connect: %v\n", err)
}
defer nc.Close()
go runPublisher(nc, &startwg, &donewg, pubCounts[i], *msgSize)
}
log.Printf("Starting benchmark [msgs=%d, msgsize=%d, pubs=%d, subs=%d]\n", *numMsgs, *msgSize, *numPubs, *numSubs)
startwg.Wait()
donewg.Wait()
benchmark.Close()
fmt.Print(benchmark.Report())
if len(*csvFile) > 0 {
csv := benchmark.CSV()
ioutil.WriteFile(*csvFile, []byte(csv), 0644)
fmt.Printf("Saved metric data in csv file %s\n", *csvFile)
}
}
func runPublisher(nc *nats.Conn, startwg, donewg *sync.WaitGroup, numMsgs int, msgSize int) {
startwg.Done()
args := flag.Args()
subj := args[0]
var msg []byte
if msgSize > 0 {
msg = make([]byte, msgSize)
}
start := time.Now()
for i := 0; i < numMsgs; i++ {
nc.Publish(subj, msg)
}
nc.Flush()
benchmark.AddPubSample(bench.NewSample(numMsgs, msgSize, start, time.Now(), nc))
donewg.Done()
}
func runSubscriber(nc *nats.Conn, startwg, donewg *sync.WaitGroup, numMsgs int, msgSize int) {
args := flag.Args()
subj := args[0]
received := 0
ch := make(chan time.Time, 2)
sub, _ := nc.Subscribe(subj, func(msg *nats.Msg) {
received++
if received == 1 {
ch <- time.Now()
}
if received >= numMsgs {
ch <- time.Now()
}
})
sub.SetPendingLimits(-1, -1)
nc.Flush()
startwg.Done()
start := <-ch
end := <-ch
benchmark.AddSubSample(bench.NewSample(numMsgs, msgSize, start, end, nc))
nc.Close()
donewg.Done()
}