forked from jeffallen/mqtt
/
main.go
244 lines (198 loc) · 4.59 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
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
package main
import (
"bytes"
"flag"
"fmt"
"log"
"net"
"os"
"sync"
"time"
"github.com/eclesh/welford"
proto "github.com/huin/mqtt"
"github.com/jeffallen/mqtt"
)
var pairs = flag.Int("pairs", 100, "how many ping/reply pairs")
var wsubs = flag.Int("wsubs", 20, "how many wildcard subscribers")
var messages = flag.Int("messages", 1000, "how many messages")
var host = flag.String("host", "localhost:1883", "hostname of broker")
var dump = flag.Bool("dump", false, "dump messages?")
var id = flag.String("id", "", "client id (default: use a random one)")
var user = flag.String("user", "", "username")
var pass = flag.String("pass", "", "password")
var cwg sync.WaitGroup
type sd struct {
mu sync.Mutex
w welford.Stats
}
func (s *sd) add(sample time.Duration) {
s.mu.Lock()
defer s.mu.Unlock()
s.w.Add(float64(sample))
}
func (s *sd) calculate() (min, max, mean, stddev time.Duration) {
s.mu.Lock()
defer s.mu.Unlock()
min = time.Duration(s.w.Min())
max = time.Duration(s.w.Max())
mean = time.Duration(s.w.Mean())
stddev = time.Duration(s.w.Stddev())
return
}
var stddev sd
func main() {
log.SetFlags(log.Lmicroseconds)
flag.Parse()
timeStart := time.Now()
timeConnStart := time.Now()
// a system to check how long connection establishment takes
cwg.Add(2**pairs + *wsubs)
go func() {
cwg.Wait()
log.Print("all connections made")
log.Print("Time to establish connections: ", time.Now().Sub(timeConnStart))
}()
// start the wildcard subscribers
wsubExit.Add(*wsubs)
wsubReady.Add(*wsubs)
for i := 0; i < *wsubs; i++ {
go wsub()
}
var wg sync.WaitGroup
for i := 0; i < *pairs; i++ {
wg.Add(1)
go func(i int) {
ping(i)
wg.Done()
}(i)
}
log.Print("all started")
wg.Wait()
log.Print("all finished")
timeEnd := time.Now()
log.Print("checking wildcard subscribers:")
wsubExit.Wait()
log.Print("ok")
elapsed := timeEnd.Sub(timeStart)
totmsg := float64(*messages * 2 * *pairs)
msgpersec := totmsg / (float64(elapsed) / float64(time.Second))
log.Print("elapsed time: ", elapsed)
log.Print("messages : ", totmsg)
log.Print("messages/sec: ", msgpersec)
log.Print("round-trip latency")
min, max, mean, stddev := stddev.calculate()
log.Print("min : ", min)
log.Print("max : ", max)
log.Print("mean : ", mean)
log.Print("stddev: ", stddev)
}
func ping(i int) {
wsubReady.Wait()
topic := fmt.Sprintf("pingtest/%v/request", i)
topic2 := fmt.Sprintf("pingtest/%v/reply", i)
start := make(chan struct{})
stop := make(chan struct{})
// the goroutine to reply to pings for this pair
go func() {
payload := []byte("ok")
cc := connect()
ack := cc.Subscribe([]proto.TopicQos{
{topic, proto.QosAtLeastOnce},
})
if *dump {
fmt.Printf("suback: %#v\n", ack)
}
close(start)
for {
select {
// for each incoming message, send it back unchanged
case in := <-cc.Incoming:
if *dump {
fmt.Printf("request: %#v\n", in)
}
in.TopicName = topic2
in.Payload = proto.BytesPayload(payload)
cc.Publish(in)
case _ = <-stop:
cc.Disconnect()
return
}
}
}()
_ = <-start
cc := connect()
ack := cc.Subscribe([]proto.TopicQos{
{topic2, proto.QosAtLeastOnce},
})
if *dump {
fmt.Printf("suback: %#v\n", ack)
}
for i := 0; i < *messages; i++ {
payload := []byte(fmt.Sprintf("ping %v", i))
timeStart := time.Now()
cc.Publish(&proto.Publish{
Header: proto.Header{QosLevel: proto.QosAtMostOnce},
TopicName: topic,
Payload: proto.BytesPayload(payload),
})
in := <-cc.Incoming
if in == nil {
break
}
if *dump {
fmt.Printf("reply: %#v\n", in)
}
elapsed := time.Now().Sub(timeStart)
stddev.add(elapsed)
buf := &bytes.Buffer{}
err := in.Payload.WritePayload(buf)
if err != nil {
log.Fatalln("payload data:", err)
}
if !bytes.Equal(buf.Bytes(), []byte("ok")) {
log.Println("unexpected reply: ", string(buf.Bytes()))
break
}
}
cc.Disconnect()
close(stop)
}
func connect() *mqtt.ClientConn {
conn, err := net.Dial("tcp", *host)
if err != nil {
fmt.Fprintf(os.Stderr, "dial: %v\n", err)
os.Exit(2)
}
cc := mqtt.NewClientConn(conn)
cc.Dump = *dump
cc.ClientId = *id
err = cc.Connect(*user, *pass)
if err != nil {
fmt.Println(err)
os.Exit(3)
}
cwg.Done()
return cc
}
var wsubExit sync.WaitGroup
var wsubReady sync.WaitGroup
func wsub() {
topic := "pingtest/0/#"
cc := connect()
defer cc.Disconnect()
ack := cc.Subscribe([]proto.TopicQos{
{topic, proto.QosAtLeastOnce},
})
if *dump {
fmt.Printf("suback: %#v\n", ack)
}
wsubReady.Done()
count := 0
for range cc.Incoming {
count++
if count == (2 * *messages) {
wsubExit.Done()
return
}
}
}