Debian Codegraph @

server/mqtt_ex_bench_test.go

raw
1 // Copyright 2024-2025 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 //go:build !skip_mqtt_tests
15
16 package server
17
18 import (
19 "fmt"
20 "strconv"
21 "testing"
22 "time"
23 )
24
25 const (
26 KB = 1024
27 )
28
29 type mqttBenchMatrix struct {
30 QOS []int
31 MessageSize []int
32 Topics []int
33 Publishers []int
34 Subscribers []int
35 }
36
37 type mqttBenchContext struct {
38 QOS int
39 MessageSize int
40 Topics int
41 Publishers int
42 Subscribers int
43
44 Host string
45 Port int
46 }
47
48 var mqttBenchDefaultMatrix = mqttBenchMatrix{
49 QOS: []int{0, 1, 2},
50 MessageSize: []int{100, 1 * KB, 10 * KB},
51 Topics: []int{100},
52 Publishers: []int{1},
53 Subscribers: []int{1},
54 }
55
56 type MQTTBenchmarkResult struct {
57 Ops int `json:"ops"`
58 NS map[string]time.Duration `json:"ns"`
59 Bytes int64 `json:"bytes"`
60 }
61
62 func BenchmarkXMQTT(b *testing.B) {
63 if mqttTestCommandPath == "" {
64 b.Skip(`"mqtt-test" command is not found in $PATH.`)
65 }
66
67 bc := mqttBenchContext{}
68 b.Run("Server", func(b *testing.B) {
69 b.Cleanup(bc.startServer(b, false))
70 bc.runAll(b)
71 })
72
73 b.Run("Cluster", func(b *testing.B) {
74 b.Cleanup(bc.startCluster(b, false))
75 bc.runAll(b)
76 })
77
78 b.Run("Server-no-RMSCache", func(b *testing.B) {
79 b.Cleanup(bc.startServer(b, true))
80
81 bc.benchmarkSubRet(b)
82 })
83
84 b.Run("Cluster-no-RMSCache", func(b *testing.B) {
85 b.Cleanup(bc.startCluster(b, true))
86
87 bc.benchmarkSubRet(b)
88 })
89 }
90
91 func (bc mqttBenchContext) runAll(b *testing.B) {
92 bc.benchmarkPub(b)
93 bc.benchmarkPubRetained(b)
94 bc.benchmarkPubSub(b)
95 bc.benchmarkSubRet(b)
96 }
97
98 // makes a copy of bc
99 func (bc mqttBenchContext) benchmarkPub(b *testing.B) {
100 m := mqttBenchDefaultMatrix.
101 NoSubscribers().
102 NoTopics()
103
104 b.Run("PUB", func(b *testing.B) {
105 m.runMatrix(b, bc, func(b *testing.B, bc *mqttBenchContext) {
106 bc.runAndReport(b, "pub",
107 "--qos", strconv.Itoa(bc.QOS),
108 "--messages", strconv.Itoa(b.N),
109 "--size", strconv.Itoa(bc.MessageSize),
110 "--publishers", strconv.Itoa(bc.Publishers),
111 )
112 })
113 })
114 }
115
116 // makes a copy of bc
117 func (bc mqttBenchContext) benchmarkPubRetained(b *testing.B) {
118 // This bench is meaningless for QOS0 since the client considers the message
119 // sent as soon as it's written out. It is also useless for QOS2 since the
120 // flow takes a lot longer, and the difference of publishing as retained or
121 // not is lost in the noise.
122 m := mqttBenchDefaultMatrix.
123 NoSubscribers().
124 NoTopics().
125 QOS1Only()
126
127 b.Run("PUBRET", func(b *testing.B) {
128 m.runMatrix(b, bc, func(b *testing.B, bc *mqttBenchContext) {
129 bc.runAndReport(b, "pub", "--retain",
130 "--qos", strconv.Itoa(bc.QOS),
131 "--messages", strconv.Itoa(b.N),
132 "--size", strconv.Itoa(bc.MessageSize),
133 "--publishers", strconv.Itoa(bc.Publishers),
134 )
135 })
136 })
137 }
138
139 // makes a copy of bc
140 func (bc mqttBenchContext) benchmarkPubSub(b *testing.B) {
141 // This test uses a single built-in topic, and a built-in publisher, so no
142 // reason to run it for topics and publishers.
143 m := mqttBenchDefaultMatrix.
144 NoTopics().
145 NoPublishers()
146
147 b.Run("PUBSUB", func(b *testing.B) {
148 m.runMatrix(b, bc, func(b *testing.B, bc *mqttBenchContext) {
149 bc.runAndReport(b, "pubsub",
150 "--qos", strconv.Itoa(bc.QOS),
151 "--messages", strconv.Itoa(b.N),
152 "--size", strconv.Itoa(bc.MessageSize),
153 "--subscribers", strconv.Itoa(bc.Subscribers),
154 )
155 })
156 })
157 }
158
159 // makes a copy of bc
160 func (bc mqttBenchContext) benchmarkSubRet(b *testing.B) {
161 // This test uses a built-in publisher, and it makes most sense to measure
162 // the retained message delivery "overhead" on a QoS0 subscription; without
163 // the extra time involved in actually subscribing.
164 m := mqttBenchDefaultMatrix.
165 NoPublishers().
166 QOS0Only()
167
168 b.Run("SUBRET", func(b *testing.B) {
169 m.runMatrix(b, bc, func(b *testing.B, bc *mqttBenchContext) {
170 bc.runAndReport(b, "subret",
171 "--qos", strconv.Itoa(bc.QOS),
172 "--topics", strconv.Itoa(bc.Topics), // number of retained messages
173 "--subscribers", strconv.Itoa(bc.Subscribers),
174 "--size", strconv.Itoa(bc.MessageSize),
175 "--repeat", strconv.Itoa(b.N), // number of subscribe requests
176 )
177 })
178 })
179 }
180
181 func (bc mqttBenchContext) runAndReport(b *testing.B, name string, extraArgs ...string) {
182 b.Helper()
183 r := mqttRunExCommandTest(b, name, mqttNewDial("", "", bc.Host, bc.Port, ""), extraArgs...)
184 r.report(b)
185 }
186
187 func (bc *mqttBenchContext) startServer(b *testing.B, disableRMSCache bool) func() {
188 b.Helper()
189 b.StopTimer()
190 o := testMQTTDefaultOptions()
191 s := testMQTTRunServer(b, o)
192
193 o = s.getOpts()
194 bc.Host = o.MQTT.Host
195 bc.Port = o.MQTT.Port
196 mqttInitTestServer(b, mqttNewDial("", "", bc.Host, bc.Port, ""))
197 return func() {
198 testMQTTShutdownServer(s)
199 }
200 }
201
202 func (bc *mqttBenchContext) startCluster(b *testing.B, disableRMSCache bool) func() {
203 b.Helper()
204 b.StopTimer()
205 conf := `
206 listen: 127.0.0.1:-1
207 server_name: %s
208 jetstream: {max_mem_store: 256MB, max_file_store: 2GB, store_dir: '%s'}
209
210 cluster {
211 name: %s
212 listen: 127.0.0.1:%d
213 routes = [%s]
214 }
215
216 mqtt {
217 listen: 127.0.0.1:-1
218 stream_replicas: 3
219 }
220
221 # For access to system account.
222 accounts { $SYS { users = [ { user: "admin", pass: "s3cr3t!" } ] } }
223 `
224
225 cl := createJetStreamClusterWithTemplate(b, conf, "MQTT", 3)
226 o := cl.randomNonLeader().getOpts()
227 bc.Host = o.MQTT.Host
228 bc.Port = o.MQTT.Port
229 mqttInitTestServer(b, mqttNewDial("", "", bc.Host, bc.Port, ""))
230 return func() {
231 cl.shutdown()
232 }
233 }
234
235 func mqttBenchWrapForMatrixField(
236 vFieldPtr *int,
237 arr []int,
238 f func(b *testing.B, bc *mqttBenchContext),
239 namef func(int) string,
240 ) func(b *testing.B, bc *mqttBenchContext) {
241 if len(arr) == 0 {
242 return f
243 }
244 return func(b *testing.B, bc *mqttBenchContext) {
245 for _, value := range arr {
246 *vFieldPtr = value
247 b.Run(namef(value), func(b *testing.B) {
248 f(b, bc)
249 })
250 }
251 }
252 }
253
254 func (m mqttBenchMatrix) runMatrix(b *testing.B, bc mqttBenchContext, f func(*testing.B, *mqttBenchContext)) {
255 b.Helper()
256 f = mqttBenchWrapForMatrixField(&bc.MessageSize, m.MessageSize, f, func(size int) string {
257 return sizeKB(size)
258 })
259 f = mqttBenchWrapForMatrixField(&bc.Topics, m.Topics, f, func(n int) string {
260 return fmt.Sprintf("%dtopics", n)
261 })
262 f = mqttBenchWrapForMatrixField(&bc.Publishers, m.Publishers, f, func(n int) string {
263 return fmt.Sprintf("%dpubc", n)
264 })
265 f = mqttBenchWrapForMatrixField(&bc.Subscribers, m.Subscribers, f, func(n int) string {
266 return fmt.Sprintf("%dsubc", n)
267 })
268 f = mqttBenchWrapForMatrixField(&bc.QOS, m.QOS, f, func(qos int) string {
269 return fmt.Sprintf("QOS%d", qos)
270 })
271 b.ResetTimer()
272 b.StartTimer()
273 f(b, &bc)
274 }
275
276 func (m mqttBenchMatrix) NoSubscribers() mqttBenchMatrix {
277 m.Subscribers = nil
278 return m
279 }
280
281 func (m mqttBenchMatrix) NoTopics() mqttBenchMatrix {
282 m.Topics = nil
283 return m
284 }
285
286 func (m mqttBenchMatrix) NoPublishers() mqttBenchMatrix {
287 m.Publishers = nil
288 return m
289 }
290
291 func (m mqttBenchMatrix) QOS0Only() mqttBenchMatrix {
292 m.QOS = []int{0}
293 return m
294 }
295
296 func (m mqttBenchMatrix) QOS1Only() mqttBenchMatrix {
297 m.QOS = []int{1}
298 return m
299 }
300
301 func sizeKB(size int) string {
302 unit := "B"
303 N := size
304 if size >= KB {
305 unit = "KB"
306 N = (N + KB/2) / KB
307 }
308 return fmt.Sprintf("%d%s", N, unit)
309 }
310
311 func (r MQTTBenchmarkResult) report(b *testing.B) {
312 // Disable the default ns metric in favor of custom X_ms/op.
313 b.ReportMetric(0, "ns/op")
314
315 // Disable MB/s since the github benchmarking action misinterprets the sign
316 // of the result (treats it as less is better).
317 b.SetBytes(0)
318 // b.SetBytes(r.Bytes)
319
320 for unit, ns := range r.NS {
321 nsOp := float64(ns) / float64(r.Ops)
322 b.ReportMetric(nsOp/1000000, unit+"_ms/op")
323 }
324 // Diable ReportAllocs() since it confuses the github benchmarking action
325 // with the noise.
326 // b.ReportAllocs()
327 }