Debian Codegraph @

test/bench_test.go

raw
1 // Copyright 2012-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 // Please note that these tests will stress a system and they need generous
15 // amounts of CPU, Memory and network sockets. Make sure the 'open files'
16 // setting for your platform is at least 8192. On linux and MacOSX you can
17 // do this via 'ulimit -n 8192'
18 //
19
20 package test
21
22 import (
23 "bufio"
24 "fmt"
25 "math/rand"
26 "net"
27 "net/url"
28 "testing"
29 "time"
30
31 "github.com/nats-io/nats-server/v2/server"
32 "github.com/nats-io/nats.go"
33 )
34
35 const PERF_PORT = 8422
36
37 // For Go routine based server.
38 func runBenchServer() *server.Server {
39 opts := DefaultTestOptions
40 opts.Port = PERF_PORT
41 opts.DisableShortFirstPing = true
42 return RunServer(&opts)
43 }
44
45 const defaultRecBufSize = 32768
46 const defaultSendBufSize = 32768
47
48 func flushConnection(b *testing.B, c net.Conn) {
49 buf := make([]byte, 32)
50 c.Write([]byte("PING\r\n"))
51 c.SetReadDeadline(time.Now().Add(5 * time.Second))
52 n, err := c.Read(buf)
53 c.SetReadDeadline(time.Time{})
54 if err != nil {
55 b.Fatalf("Failed read: %v\n", err)
56 }
57 if n != 6 && buf[0] != 'P' && buf[1] != 'O' {
58 b.Fatalf("Failed read of PONG: %s\n", buf)
59 }
60 }
61
62 func benchPub(b *testing.B, subject, payload string) {
63 b.StopTimer()
64 s := runBenchServer()
65 c := createClientConn(b, "127.0.0.1", PERF_PORT)
66 doDefaultConnect(b, c)
67 bw := bufio.NewWriterSize(c, defaultSendBufSize)
68 sendOp := []byte(fmt.Sprintf("PUB %s %d\r\n%s\r\n", subject, len(payload), payload))
69 b.SetBytes(int64(len(sendOp)))
70 b.StartTimer()
71 for i := 0; i < b.N; i++ {
72 bw.Write(sendOp)
73 }
74 bw.Flush()
75 flushConnection(b, c)
76 b.StopTimer()
77 c.Close()
78 s.Shutdown()
79 }
80
81 var ch = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@$#%^&*()")
82
83 func sizedBytes(sz int) []byte {
84 b := make([]byte, sz)
85 for i := range b {
86 b[i] = ch[rand.Intn(len(ch))]
87 }
88 return b
89 }
90
91 func sizedString(sz int) string {
92 return string(sizedBytes(sz))
93 }
94
95 // Publish subject for pub benchmarks.
96 var psub = "a"
97
98 func Benchmark______Pub0b_Payload(b *testing.B) {
99 benchPub(b, psub, "")
100 }
101
102 func Benchmark______Pub8b_Payload(b *testing.B) {
103 b.StopTimer()
104 s := sizedString(8)
105 benchPub(b, psub, s)
106 }
107
108 func Benchmark_____Pub32b_Payload(b *testing.B) {
109 b.StopTimer()
110 s := sizedString(32)
111 benchPub(b, psub, s)
112 }
113
114 func Benchmark____Pub128B_Payload(b *testing.B) {
115 b.StopTimer()
116 s := sizedString(128)
117 benchPub(b, psub, s)
118 }
119
120 func Benchmark____Pub256B_Payload(b *testing.B) {
121 b.StopTimer()
122 s := sizedString(256)
123 benchPub(b, psub, s)
124 }
125
126 func Benchmark______Pub1K_Payload(b *testing.B) {
127 b.StopTimer()
128 s := sizedString(1024)
129 benchPub(b, psub, s)
130 }
131
132 func Benchmark______Pub4K_Payload(b *testing.B) {
133 b.StopTimer()
134 s := sizedString(4 * 1024)
135 benchPub(b, psub, s)
136 }
137
138 func Benchmark______Pub8K_Payload(b *testing.B) {
139 b.StopTimer()
140 s := sizedString(8 * 1024)
141 benchPub(b, psub, s)
142 }
143
144 func Benchmark_____Pub32K_Payload(b *testing.B) {
145 b.StopTimer()
146 s := sizedString(32 * 1024)
147 benchPub(b, psub, s)
148 }
149
150 func drainConnection(b *testing.B, c net.Conn, ch chan bool, expected int) {
151 buf := make([]byte, defaultRecBufSize)
152 bytes := 0
153
154 for {
155 c.SetReadDeadline(time.Now().Add(30 * time.Second))
156 n, err := c.Read(buf)
157 if err != nil {
158 b.Errorf("Error on read: %v\n", err)
159 break
160 }
161 bytes += n
162 if bytes >= expected {
163 break
164 }
165 }
166 if bytes != expected {
167 b.Errorf("Did not receive all bytes: %d vs %d\n", bytes, expected)
168 }
169 ch <- true
170 }
171
172 // Benchmark the authorization code path.
173 func Benchmark__AuthPub0b_Payload(b *testing.B) {
174 b.StopTimer()
175
176 srv, opts := RunServerWithConfig("./configs/authorization.conf")
177 opts.DisableShortFirstPing = true
178 defer srv.Shutdown()
179
180 c := createClientConn(b, opts.Host, opts.Port)
181 defer c.Close()
182 expectAuthRequired(b, c)
183
184 cs := fmt.Sprintf("CONNECT {\"verbose\":false,\"user\":\"%s\",\"pass\":\"%s\"}\r\n", "bench", DefaultPass)
185 sendProto(b, c, cs)
186
187 bw := bufio.NewWriterSize(c, defaultSendBufSize)
188 sendOp := []byte("PUB a 0\r\n\r\n")
189 b.SetBytes(int64(len(sendOp)))
190 b.StartTimer()
191 for i := 0; i < b.N; i++ {
192 bw.Write(sendOp)
193 }
194 bw.Flush()
195 flushConnection(b, c)
196 b.StopTimer()
197 }
198
199 func Benchmark_____________PubSub(b *testing.B) {
200 b.StopTimer()
201 s := runBenchServer()
202 c := createClientConn(b, "127.0.0.1", PERF_PORT)
203 doDefaultConnect(b, c)
204 sendProto(b, c, "SUB foo 1\r\n")
205 bw := bufio.NewWriterSize(c, defaultSendBufSize)
206 sendOp := []byte("PUB foo 2\r\nok\r\n")
207 ch := make(chan bool)
208 expected := len("MSG foo 1 2\r\nok\r\n") * b.N
209 go drainConnection(b, c, ch, expected)
210 b.StartTimer()
211
212 for i := 0; i < b.N; i++ {
213 _, err := bw.Write(sendOp)
214 if err != nil {
215 b.Errorf("Received error on PUB write: %v\n", err)
216 }
217 }
218 err := bw.Flush()
219 if err != nil {
220 b.Errorf("Received error on FLUSH write: %v\n", err)
221 }
222
223 // Wait for connection to be drained
224 <-ch
225
226 b.StopTimer()
227 c.Close()
228 s.Shutdown()
229 }
230
231 func Benchmark_____PubSubTwoConns(b *testing.B) {
232 b.StopTimer()
233 s := runBenchServer()
234 c := createClientConn(b, "127.0.0.1", PERF_PORT)
235 doDefaultConnect(b, c)
236 bw := bufio.NewWriterSize(c, defaultSendBufSize)
237
238 c2 := createClientConn(b, "127.0.0.1", PERF_PORT)
239 doDefaultConnect(b, c2)
240 sendProto(b, c2, "SUB foo 1\r\n")
241 flushConnection(b, c2)
242
243 sendOp := []byte("PUB foo 2\r\nok\r\n")
244 ch := make(chan bool)
245
246 expected := len("MSG foo 1 2\r\nok\r\n") * b.N
247 go drainConnection(b, c2, ch, expected)
248
249 b.StartTimer()
250 for i := 0; i < b.N; i++ {
251 bw.Write(sendOp)
252 }
253 err := bw.Flush()
254 if err != nil {
255 b.Errorf("Received error on FLUSH write: %v\n", err)
256 }
257
258 // Wait for connection to be drained
259 <-ch
260
261 b.StopTimer()
262 c.Close()
263 c2.Close()
264 s.Shutdown()
265 }
266
267 func benchDefaultOptionsForAccounts() *server.Options {
268 o := DefaultTestOptions
269 o.Host = "127.0.0.1"
270 o.Port = -1
271 o.Cluster.Host = o.Host
272 o.Cluster.Port = -1
273 o.DisableShortFirstPing = true
274 fooAcc := server.NewAccount("$foo")
275 fooAcc.AddStreamExport("foo", nil)
276 barAcc := server.NewAccount("$bar")
277 barAcc.AddStreamImport(fooAcc, "foo", "")
278 o.Accounts = []*server.Account{fooAcc, barAcc}
279
280 return &o
281 }
282
283 func createClientWithAccount(b *testing.B, account, host string, port int) net.Conn {
284 c := createClientConn(b, host, port)
285 checkInfoMsg(b, c)
286 cs := fmt.Sprintf("CONNECT {\"verbose\":%v,\"pedantic\":%v,\"tls_required\":%v,\"account\":%q}\r\n", false, false, false, account)
287 sendProto(b, c, cs)
288 return c
289 }
290
291 func benchOptionsForServiceImports() *server.Options {
292 o := DefaultTestOptions
293 o.Host = "127.0.0.1"
294 o.Port = -1
295 o.DisableShortFirstPing = true
296 foo := server.NewAccount("$foo")
297 bar := server.NewAccount("$bar")
298 o.Accounts = []*server.Account{foo, bar}
299
300 return &o
301 }
302
303 func addServiceImports(b *testing.B, s *server.Server) {
304 // Add a bunch of service exports with wildcards, similar to JS.
305 var exports = []string{
306 server.JSApiAccountInfo,
307 server.JSApiStreamCreate,
308 server.JSApiStreamUpdate,
309 server.JSApiStreams,
310 server.JSApiStreamInfo,
311 server.JSApiStreamDelete,
312 server.JSApiStreamPurge,
313 server.JSApiMsgDelete,
314 server.JSApiConsumerCreate,
315 server.JSApiConsumers,
316 server.JSApiConsumerInfo,
317 server.JSApiConsumerDelete,
318 }
319 foo, _ := s.LookupAccount("$foo")
320 bar, _ := s.LookupAccount("$bar")
321
322 for _, export := range exports {
323 if err := bar.AddServiceExport(export, nil); err != nil {
324 b.Fatalf("Could not add service export: %v", err)
325 }
326 if err := foo.AddServiceImport(bar, export, ""); err != nil {
327 b.Fatalf("Could not add service import: %v", err)
328 }
329 }
330 }
331
332 func Benchmark__PubServiceImports(b *testing.B) {
333 o := benchOptionsForServiceImports()
334 s := RunServer(o)
335 defer s.Shutdown()
336
337 addServiceImports(b, s)
338
339 c := createClientWithAccount(b, "$foo", o.Host, o.Port)
340 defer c.Close()
341
342 bw := bufio.NewWriterSize(c, defaultSendBufSize)
343 sendOp := []byte("PUB foo 2\r\nok\r\n")
344
345 b.ResetTimer()
346 for i := 0; i < b.N; i++ {
347 bw.Write(sendOp)
348 }
349 err := bw.Flush()
350 if err != nil {
351 b.Errorf("Received error on FLUSH write: %v\n", err)
352 }
353 b.StopTimer()
354 }
355
356 func Benchmark___PubSubAccsImport(b *testing.B) {
357 o := benchDefaultOptionsForAccounts()
358 s := RunServer(o)
359 defer s.Shutdown()
360
361 pub := createClientWithAccount(b, "$foo", o.Host, o.Port)
362 defer pub.Close()
363
364 sub := createClientWithAccount(b, "$bar", o.Host, o.Port)
365 defer sub.Close()
366
367 sendProto(b, sub, "SUB foo 1\r\n")
368 flushConnection(b, sub)
369 ch := make(chan bool)
370 expected := len("MSG foo 1 2\r\nok\r\n") * b.N
371 go drainConnection(b, sub, ch, expected)
372
373 bw := bufio.NewWriterSize(pub, defaultSendBufSize)
374 sendOp := []byte("PUB foo 2\r\nok\r\n")
375
376 b.ResetTimer()
377 for i := 0; i < b.N; i++ {
378 bw.Write(sendOp)
379 }
380 err := bw.Flush()
381 if err != nil {
382 b.Errorf("Received error on FLUSH write: %v\n", err)
383 }
384
385 // Wait for connection to be drained
386 <-ch
387 b.StopTimer()
388 }
389
390 func Benchmark_____PubTwoQueueSub(b *testing.B) {
391 b.StopTimer()
392 s := runBenchServer()
393 c := createClientConn(b, "127.0.0.1", PERF_PORT)
394 doDefaultConnect(b, c)
395 sendProto(b, c, "SUB foo group1 1\r\n")
396 sendProto(b, c, "SUB foo group1 2\r\n")
397 bw := bufio.NewWriterSize(c, defaultSendBufSize)
398 sendOp := []byte("PUB foo 2\r\nok\r\n")
399 ch := make(chan bool)
400 expected := len("MSG foo 1 2\r\nok\r\n") * b.N
401 go drainConnection(b, c, ch, expected)
402 b.StartTimer()
403
404 for i := 0; i < b.N; i++ {
405 _, err := bw.Write(sendOp)
406 if err != nil {
407 b.Fatalf("Received error on PUB write: %v\n", err)
408 }
409 }
410 err := bw.Flush()
411 if err != nil {
412 b.Fatalf("Received error on FLUSH write: %v\n", err)
413 }
414
415 // Wait for connection to be drained
416 <-ch
417
418 b.StopTimer()
419 c.Close()
420 s.Shutdown()
421 }
422
423 func Benchmark____PubFourQueueSub(b *testing.B) {
424 b.StopTimer()
425 s := runBenchServer()
426 c := createClientConn(b, "127.0.0.1", PERF_PORT)
427 doDefaultConnect(b, c)
428 sendProto(b, c, "SUB foo group1 1\r\n")
429 sendProto(b, c, "SUB foo group1 2\r\n")
430 sendProto(b, c, "SUB foo group1 3\r\n")
431 sendProto(b, c, "SUB foo group1 4\r\n")
432 bw := bufio.NewWriterSize(c, defaultSendBufSize)
433 sendOp := []byte("PUB foo 2\r\nok\r\n")
434 ch := make(chan bool)
435 expected := len("MSG foo 1 2\r\nok\r\n") * b.N
436 go drainConnection(b, c, ch, expected)
437 b.StartTimer()
438
439 for i := 0; i < b.N; i++ {
440 _, err := bw.Write(sendOp)
441 if err != nil {
442 b.Fatalf("Received error on PUB write: %v\n", err)
443 }
444 }
445 err := bw.Flush()
446 if err != nil {
447 b.Fatalf("Received error on FLUSH write: %v\n", err)
448 }
449
450 // Wait for connection to be drained
451 <-ch
452
453 b.StopTimer()
454 c.Close()
455 s.Shutdown()
456 }
457
458 func Benchmark___PubEightQueueSub(b *testing.B) {
459 b.StopTimer()
460 s := runBenchServer()
461 c := createClientConn(b, "127.0.0.1", PERF_PORT)
462 doDefaultConnect(b, c)
463 sendProto(b, c, "SUB foo group1 1\r\n")
464 sendProto(b, c, "SUB foo group1 2\r\n")
465 sendProto(b, c, "SUB foo group1 3\r\n")
466 sendProto(b, c, "SUB foo group1 4\r\n")
467 sendProto(b, c, "SUB foo group1 5\r\n")
468 sendProto(b, c, "SUB foo group1 6\r\n")
469 sendProto(b, c, "SUB foo group1 7\r\n")
470 sendProto(b, c, "SUB foo group1 8\r\n")
471 bw := bufio.NewWriterSize(c, defaultSendBufSize)
472 sendOp := []byte("PUB foo 2\r\nok\r\n")
473 ch := make(chan bool)
474 expected := len("MSG foo 1 2\r\nok\r\n") * b.N
475 go drainConnection(b, c, ch, expected)
476 b.StartTimer()
477
478 for i := 0; i < b.N; i++ {
479 _, err := bw.Write(sendOp)
480 if err != nil {
481 b.Fatalf("Received error on PUB write: %v\n", err)
482 }
483 }
484 err := bw.Flush()
485 if err != nil {
486 b.Fatalf("Received error on FLUSH write: %v\n", err)
487 }
488
489 // Wait for connection to be drained
490 <-ch
491
492 b.StopTimer()
493 c.Close()
494 s.Shutdown()
495 }
496
497 func Benchmark_PubSub512kTwoConns(b *testing.B) {
498 b.StopTimer()
499 s := runBenchServer()
500 c := createClientConn(b, "127.0.0.1", PERF_PORT)
501 doDefaultConnect(b, c)
502 bw := bufio.NewWriterSize(c, defaultSendBufSize)
503
504 c2 := createClientConn(b, "127.0.0.1", PERF_PORT)
505 doDefaultConnect(b, c2)
506 sendProto(b, c2, "SUB foo 1\r\n")
507 flushConnection(b, c2)
508
509 sz := 1024 * 512
510 payload := sizedString(sz)
511
512 sendOp := []byte(fmt.Sprintf("PUB foo %d\r\n%s\r\n", sz, payload))
513 ch := make(chan bool)
514
515 expected := len(fmt.Sprintf("MSG foo 1 %d\r\n%s\r\n", sz, payload)) * b.N
516 go drainConnection(b, c2, ch, expected)
517
518 b.StartTimer()
519 for i := 0; i < b.N; i++ {
520 bw.Write(sendOp)
521 }
522 err := bw.Flush()
523 if err != nil {
524 b.Errorf("Received error on FLUSH write: %v\n", err)
525 }
526
527 // Wait for connection to be drained
528 <-ch
529
530 b.StopTimer()
531 c.Close()
532 c2.Close()
533 s.Shutdown()
534 }
535
536 func Benchmark__DenyMsgNoWCPubSub(b *testing.B) {
537 s, opts := RunServerWithConfig("./configs/authorization.conf")
538 opts.DisableShortFirstPing = true
539 defer s.Shutdown()
540
541 c := createClientConn(b, opts.Host, opts.Port)
542 defer c.Close()
543
544 expectAuthRequired(b, c)
545 cs := fmt.Sprintf("CONNECT {\"verbose\":false,\"pedantic\":false,\"user\":\"%s\",\"pass\":\"%s\"}\r\n", "bench-deny", DefaultPass)
546 sendProto(b, c, cs)
547
548 sendProto(b, c, "SUB foo 1\r\n")
549 bw := bufio.NewWriterSize(c, defaultSendBufSize)
550 sendOp := []byte("PUB foo 2\r\nok\r\n")
551 ch := make(chan bool)
552 expected := len("MSG foo 1 2\r\nok\r\n") * b.N
553 go drainConnection(b, c, ch, expected)
554 b.ResetTimer()
555
556 for i := 0; i < b.N; i++ {
557 _, err := bw.Write(sendOp)
558 if err != nil {
559 b.Errorf("Received error on PUB write: %v\n", err)
560 }
561 }
562 err := bw.Flush()
563 if err != nil {
564 b.Errorf("Received error on FLUSH write: %v\n", err)
565 }
566
567 // Wait for connection to be drained
568 <-ch
569
570 // To not count defer cleanup of client and server.
571 b.StopTimer()
572 }
573
574 func Benchmark_DenyMsgYesWCPubSub(b *testing.B) {
575 s, opts := RunServerWithConfig("./configs/authorization.conf")
576 opts.DisableShortFirstPing = true
577 defer s.Shutdown()
578
579 c := createClientConn(b, opts.Host, opts.Port)
580 defer c.Close()
581
582 expectAuthRequired(b, c)
583 cs := fmt.Sprintf("CONNECT {\"verbose\":false,\"pedantic\":false,\"user\":\"%s\",\"pass\":\"%s\"}\r\n", "bench-deny", DefaultPass)
584 sendProto(b, c, cs)
585
586 sendProto(b, c, "SUB * 1\r\n")
587 bw := bufio.NewWriterSize(c, defaultSendBufSize)
588 sendOp := []byte("PUB foo 2\r\nok\r\n")
589 ch := make(chan bool)
590 expected := len("MSG foo 1 2\r\nok\r\n") * b.N
591 go drainConnection(b, c, ch, expected)
592 b.ResetTimer()
593
594 for i := 0; i < b.N; i++ {
595 _, err := bw.Write(sendOp)
596 if err != nil {
597 b.Errorf("Received error on PUB write: %v\n", err)
598 }
599 }
600 err := bw.Flush()
601 if err != nil {
602 b.Errorf("Received error on FLUSH write: %v\n", err)
603 }
604
605 // Wait for connection to be drained
606 <-ch
607
608 // To not count defer cleanup of client and server.
609 b.StopTimer()
610 }
611
612 func routePubSub(b *testing.B, size int) {
613 b.StopTimer()
614
615 s1, o1 := RunServerWithConfig("./configs/srv_a.conf")
616 o1.DisableShortFirstPing = true
617 defer s1.Shutdown()
618 s2, o2 := RunServerWithConfig("./configs/srv_b.conf")
619 o2.DisableShortFirstPing = true
620 defer s2.Shutdown()
621
622 sub := createClientConn(b, o1.Host, o1.Port)
623 doDefaultConnect(b, sub)
624 sendProto(b, sub, "SUB foo 1\r\n")
625 flushConnection(b, sub)
626
627 payload := sizedString(size)
628
629 pub := createClientConn(b, o2.Host, o2.Port)
630 doDefaultConnect(b, pub)
631 bw := bufio.NewWriterSize(pub, defaultSendBufSize)
632
633 ch := make(chan bool)
634 sendOp := []byte(fmt.Sprintf("PUB foo %d\r\n%s\r\n", len(payload), payload))
635 expected := len(fmt.Sprintf("MSG foo 1 %d\r\n%s\r\n", len(payload), payload)) * b.N
636 go drainConnection(b, sub, ch, expected)
637 b.StartTimer()
638
639 for i := 0; i < b.N; i++ {
640 _, err := bw.Write(sendOp)
641 if err != nil {
642 b.Fatalf("Received error on PUB write: %v\n", err)
643 }
644
645 }
646 err := bw.Flush()
647 if err != nil {
648 b.Errorf("Received error on FLUSH write: %v\n", err)
649 }
650
651 // Wait for connection to be drained
652 <-ch
653
654 b.StopTimer()
655 pub.Close()
656 sub.Close()
657 }
658
659 func Benchmark____RoutedPubSub_0b(b *testing.B) {
660 routePubSub(b, 2)
661 }
662
663 func Benchmark____RoutedPubSub_1K(b *testing.B) {
664 routePubSub(b, 1024)
665 }
666
667 func Benchmark__RoutedPubSub_100K(b *testing.B) {
668 routePubSub(b, 100*1024)
669 }
670
671 func routeQueue(b *testing.B, numQueueSubs, size int) {
672 s1, o1 := RunServerWithConfig("./configs/srv_a.conf")
673 o1.DisableShortFirstPing = true
674 defer s1.Shutdown()
675 s2, o2 := RunServerWithConfig("./configs/srv_b.conf")
676 o2.DisableShortFirstPing = true
677 defer s2.Shutdown()
678
679 sub := createClientConn(b, o1.Host, o1.Port)
680 defer sub.Close()
681 doDefaultConnect(b, sub)
682 for i := 0; i < numQueueSubs; i++ {
683 sendProto(b, sub, fmt.Sprintf("SUB foo bar %d\r\n", 100+i))
684 }
685 flushConnection(b, sub)
686
687 payload := sizedString(size)
688
689 pub := createClientConn(b, o2.Host, o2.Port)
690 defer pub.Close()
691 doDefaultConnect(b, pub)
692 bw := bufio.NewWriterSize(pub, defaultSendBufSize)
693
694 ch := make(chan bool)
695 sendOp := []byte(fmt.Sprintf("PUB foo %d\r\n%s\r\n", len(payload), payload))
696 expected := len(fmt.Sprintf("MSG foo 100 %d\r\n%s\r\n", len(payload), payload)) * b.N
697 go drainConnection(b, sub, ch, expected)
698
699 b.ResetTimer()
700
701 for i := 0; i < b.N; i++ {
702 _, err := bw.Write(sendOp)
703 if err != nil {
704 b.Fatalf("Received error on PUB write: %v\n", err)
705 }
706
707 }
708 err := bw.Flush()
709 if err != nil {
710 b.Errorf("Received error on FLUSH write: %v\n", err)
711 }
712
713 // Wait for connection to be drained
714 <-ch
715
716 b.StopTimer()
717 }
718
719 func Benchmark____Routed2QueueSub(b *testing.B) {
720 routeQueue(b, 2, 2)
721 }
722
723 func Benchmark____Routed4QueueSub(b *testing.B) {
724 routeQueue(b, 4, 2)
725 }
726
727 func Benchmark____Routed8QueueSub(b *testing.B) {
728 routeQueue(b, 8, 2)
729 }
730
731 func Benchmark___Routed16QueueSub(b *testing.B) {
732 routeQueue(b, 16, 2)
733 }
734
735 func doS2CompressBench(b *testing.B, compress string) {
736 b.StopTimer()
737 conf1 := createConfFile(b, []byte(fmt.Sprintf(`
738 port: -1
739 cluster {
740 name: "local"
741 port: -1
742 pool_size: -1
743 compression: %s
744 }
745 `, compress)))
746 s1, o1 := RunServerWithConfig(conf1)
747 defer s1.Shutdown()
748
749 conf2 := createConfFile(b, []byte(fmt.Sprintf(`
750 port: -1
751 cluster {
752 name: "local"
753 port: -1
754 pool_size: -1
755 compression: %s
756 routes: ["nats://127.0.0.1:%d"]
757 }
758 `, compress, o1.Cluster.Port)))
759 s2, _ := RunServerWithConfig(conf2)
760 defer s2.Shutdown()
761
762 checkClusterFormed(b, s1, s2)
763
764 nc2, err := nats.Connect(s2.ClientURL())
765 if err != nil {
766 b.Fatalf("Error on connect: %v", err)
767 }
768 defer nc2.Close()
769
770 ch := make(chan struct{}, 1)
771 var count int
772 nc2.Subscribe("foo", func(_ *nats.Msg) {
773 if count++; count == b.N {
774 select {
775 case ch <- struct{}{}:
776 default:
777 }
778 }
779 })
780
781 checkSubInterest(b, s1, "$G", "foo", time.Second)
782
783 nc1, err := nats.Connect(s1.ClientURL())
784 if err != nil {
785 b.Fatalf("Error on connect: %v", err)
786 }
787 defer nc1.Close()
788
789 // This one is easily compressible.
790 payload1 := make([]byte, 128)
791 // Make it random so that compression code has more to do.
792 payload2 := make([]byte, 256)
793 for i := 0; i < len(payload); i++ {
794 payload2[i] = byte(rand.Intn(26) + 'A')
795 }
796 b.StartTimer()
797
798 for i := 0; i < b.N; i++ {
799 if i%2 == 0 {
800 nc1.Publish("foo", payload1)
801 } else {
802 nc1.Publish("foo", payload2)
803 }
804 }
805
806 select {
807 case <-ch:
808 return
809 case <-time.After(10 * time.Second):
810 b.Fatal("Timeout waiting to receive all messages")
811 }
812 }
813
814 func Benchmark____________RouteCompressOff(b *testing.B) {
815 doS2CompressBench(b, server.CompressionOff)
816 }
817
818 func Benchmark_RouteCompressS2Uncompressed(b *testing.B) {
819 doS2CompressBench(b, server.CompressionS2Uncompressed)
820 }
821
822 func Benchmark_________RouteCompressS2Fast(b *testing.B) {
823 doS2CompressBench(b, server.CompressionS2Fast)
824 }
825
826 func Benchmark_______RouteCompressS2Better(b *testing.B) {
827 doS2CompressBench(b, server.CompressionS2Better)
828 }
829
830 func Benchmark_________RouteCompressS2Best(b *testing.B) {
831 doS2CompressBench(b, server.CompressionS2Best)
832 }
833
834 func doFanout(b *testing.B, numServers, numConnections, subsPerConnection int, subject, payload string) {
835 var s1, s2 *server.Server
836 var o1, o2 *server.Options
837
838 switch numServers {
839 case 1:
840 s1, o1 = RunServerWithConfig("./configs/srv_a.conf")
841 o1.DisableShortFirstPing = true
842 defer s1.Shutdown()
843 s2, o2 = s1, o1
844 case 2:
845 s1, o1 = RunServerWithConfig("./configs/srv_a.conf")
846 o1.DisableShortFirstPing = true
847 defer s1.Shutdown()
848 s2, o2 = RunServerWithConfig("./configs/srv_b.conf")
849 o2.DisableShortFirstPing = true
850 defer s2.Shutdown()
851 default:
852 b.Fatalf("%d servers not supported for this test\n", numServers)
853 }
854
855 // To get a consistent length sid in MSG sent to us for drainConnection.
856 var sidFloor int
857 switch {
858 case subsPerConnection <= 100:
859 sidFloor = 100
860 case subsPerConnection <= 1000:
861 sidFloor = 1000
862 case subsPerConnection <= 10000:
863 sidFloor = 10000
864 default:
865 b.Fatalf("Unsupported SubsPerConnection argument of %d\n", subsPerConnection)
866 }
867
868 msgOp := fmt.Sprintf("MSG %s %d %d\r\n%s\r\n", subject, sidFloor, len(payload), payload)
869 expected := len(msgOp) * subsPerConnection * b.N
870
871 // Client connections and subscriptions.
872 clients := make([]chan bool, 0, numConnections)
873 for i := 0; i < numConnections; i++ {
874 c := createClientConn(b, o2.Host, o2.Port)
875 doDefaultConnect(b, c)
876 defer c.Close()
877
878 ch := make(chan bool)
879 clients = append(clients, ch)
880
881 for s := 0; s < subsPerConnection; s++ {
882 subOp := fmt.Sprintf("SUB %s %d\r\n", subject, sidFloor+s)
883 sendProto(b, c, subOp)
884 }
885 flushConnection(b, c)
886 go drainConnection(b, c, ch, expected)
887 }
888 // Publish Connection
889 c := createClientConn(b, o1.Host, o1.Port)
890 doDefaultConnect(b, c)
891 flushConnection(b, c)
892
893 bw := bufio.NewWriterSize(c, defaultSendBufSize)
894 sendOp := []byte(fmt.Sprintf("PUB %s %d\r\n%s\r\n", subject, len(payload), payload))
895
896 b.SetBytes(int64(len(sendOp) + (len(msgOp) * numConnections * subsPerConnection)))
897 b.ResetTimer()
898
899 for i := 0; i < b.N; i++ {
900 _, err := bw.Write(sendOp)
901 if err != nil {
902 b.Fatalf("Received error on PUB write: %v\n", err)
903 }
904 }
905 err := bw.Flush()
906 if err != nil {
907 b.Fatalf("Received error on FLUSH write: %v\n", err)
908 }
909
910 // Wait for connections to be drained
911 for i := 0; i < numConnections; i++ {
912 <-clients[i]
913 }
914 b.StopTimer()
915 }
916
917 var sub = "x"
918 var payload = "12345678"
919
920 func Benchmark______FanOut_8x1x10(b *testing.B) {
921 doFanout(b, 1, 1, 10, sub, payload)
922 }
923
924 func Benchmark_____FanOut_8x1x100(b *testing.B) {
925 doFanout(b, 1, 1, 100, sub, payload)
926 }
927
928 func Benchmark____FanOut_8x10x100(b *testing.B) {
929 doFanout(b, 1, 10, 100, sub, payload)
930 }
931
932 func Benchmark___FanOut_8x10x1000(b *testing.B) {
933 doFanout(b, 1, 10, 1000, sub, payload)
934 }
935
936 func Benchmark___FanOut_8x100x100(b *testing.B) {
937 doFanout(b, 1, 100, 100, sub, payload)
938 }
939
940 func Benchmark__FanOut_8x100x1000(b *testing.B) {
941 doFanout(b, 1, 100, 1000, sub, payload)
942 }
943
944 func Benchmark__FanOut_8x10x10000(b *testing.B) {
945 doFanout(b, 1, 10, 10000, sub, payload)
946 }
947
948 func Benchmark___FanOut_8x500x100(b *testing.B) {
949 doFanout(b, 1, 500, 100, sub, payload)
950 }
951
952 func Benchmark___FanOut_128x1x100(b *testing.B) {
953 doFanout(b, 1, 1, 100, sub, sizedString(128))
954 }
955
956 func Benchmark__FanOut_128x10x100(b *testing.B) {
957 doFanout(b, 1, 10, 100, sub, sizedString(128))
958 }
959
960 func Benchmark_FanOut_128x10x1000(b *testing.B) {
961 doFanout(b, 1, 10, 1000, sub, sizedString(128))
962 }
963
964 func Benchmark_FanOut_128x100x100(b *testing.B) {
965 doFanout(b, 1, 100, 100, sub, sizedString(128))
966 }
967
968 func BenchmarkFanOut_128x100x1000(b *testing.B) {
969 doFanout(b, 1, 100, 1000, sub, sizedString(128))
970 }
971
972 func BenchmarkFanOut_128x10x10000(b *testing.B) {
973 doFanout(b, 1, 10, 10000, sub, sizedString(128))
974 }
975
976 func BenchmarkFanOut__128x500x100(b *testing.B) {
977 doFanout(b, 1, 500, 100, sub, sizedString(128))
978 }
979
980 func Benchmark_FanOut_512x100x100(b *testing.B) {
981 doFanout(b, 1, 100, 100, sub, sizedString(512))
982 }
983
984 func Benchmark__FanOut_512x100x1k(b *testing.B) {
985 doFanout(b, 1, 100, 1000, sub, sizedString(512))
986 }
987
988 func Benchmark____FanOut_1kx10x1k(b *testing.B) {
989 doFanout(b, 1, 10, 1000, sub, sizedString(1024))
990 }
991
992 func Benchmark__FanOut_1kx100x100(b *testing.B) {
993 doFanout(b, 1, 100, 100, sub, sizedString(1024))
994 }
995
996 func Benchmark_____RFanOut_8x1x10(b *testing.B) {
997 doFanout(b, 2, 1, 10, sub, payload)
998 }
999
1000 func Benchmark____RFanOut_8x1x100(b *testing.B) {
1001 doFanout(b, 2, 1, 100, sub, payload)
1002 }
1003
1004 func Benchmark___RFanOut_8x10x100(b *testing.B) {
1005 doFanout(b, 2, 10, 100, sub, payload)
1006 }
1007
1008 func Benchmark__RFanOut_8x10x1000(b *testing.B) {
1009 doFanout(b, 2, 10, 1000, sub, payload)
1010 }
1011
1012 func Benchmark__RFanOut_8x100x100(b *testing.B) {
1013 doFanout(b, 2, 100, 100, sub, payload)
1014 }
1015
1016 func Benchmark_RFanOut_8x100x1000(b *testing.B) {
1017 doFanout(b, 2, 100, 1000, sub, payload)
1018 }
1019
1020 func Benchmark_RFanOut_8x10x10000(b *testing.B) {
1021 doFanout(b, 2, 10, 10000, sub, payload)
1022 }
1023
1024 func Benchmark_RFanOut_1kx10x1000(b *testing.B) {
1025 doFanout(b, 2, 10, 1000, sub, sizedString(1024))
1026 }
1027
1028 func doFanIn(b *testing.B, numServers, numPublishers, numSubscribers int, subject, payload string) {
1029 b.Helper()
1030 if b.N < numPublishers {
1031 return
1032 }
1033 // Don't check for number of subscribers being lower than the number of publishers.
1034 // We also use this bench to show the performance impact of increased number of publishers,
1035 // and for those tests, the number of publishers will start at 1 and increase to 10,
1036 // while the number of subscribers will always be 3.
1037 if numSubscribers > 10 {
1038 b.Fatalf("numSubscribers should be <= 10")
1039 }
1040
1041 var s1, s2 *server.Server
1042 var o1, o2 *server.Options
1043
1044 switch numServers {
1045 case 1:
1046 s1, o1 = RunServerWithConfig("./configs/srv_a.conf")
1047 o1.DisableShortFirstPing = true
1048 defer s1.Shutdown()
1049 s2, o2 = s1, o1
1050 case 2:
1051 s1, o1 = RunServerWithConfig("./configs/srv_a.conf")
1052 o1.DisableShortFirstPing = true
1053 defer s1.Shutdown()
1054 s2, o2 = RunServerWithConfig("./configs/srv_b.conf")
1055 o2.DisableShortFirstPing = true
1056 defer s2.Shutdown()
1057 default:
1058 b.Fatalf("%d servers not supported for this test\n", numServers)
1059 }
1060
1061 msgOp := fmt.Sprintf("MSG %s %d %d\r\n%s\r\n", subject, 9, len(payload), payload)
1062 l := b.N / numPublishers
1063 expected := len(msgOp) * l * numPublishers
1064
1065 // Client connections and subscriptions. For fan in these are smaller then numPublishers.
1066 clients := make([]chan bool, 0, numSubscribers)
1067 for i := 0; i < numSubscribers; i++ {
1068 c := createClientConn(b, o2.Host, o2.Port)
1069 doDefaultConnect(b, c)
1070 defer c.Close()
1071
1072 ch := make(chan bool)
1073 clients = append(clients, ch)
1074
1075 subOp := fmt.Sprintf("SUB %s %d\r\n", subject, i)
1076 sendProto(b, c, subOp)
1077 flushConnection(b, c)
1078 go drainConnection(b, c, ch, expected)
1079 }
1080
1081 sendOp := []byte(fmt.Sprintf("PUB %s %d\r\n%s\r\n", subject, len(payload), payload))
1082 startCh := make(chan bool)
1083
1084 pubLoop := func(c net.Conn, ch chan bool) {
1085 bw := bufio.NewWriterSize(c, defaultSendBufSize)
1086
1087 // Signal we are ready
1088 close(ch)
1089
1090 // Wait to start up actual sends.
1091 <-startCh
1092
1093 for i := 0; i < l; i++ {
1094 _, err := bw.Write(sendOp)
1095 if err != nil {
1096 b.Errorf("Received error on PUB write: %v\n", err)
1097 return
1098 }
1099 }
1100 err := bw.Flush()
1101 if err != nil {
1102 b.Errorf("Received error on FLUSH write: %v\n", err)
1103 return
1104 }
1105 }
1106
1107 // Publish Connections SPINUP
1108 for i := 0; i < numPublishers; i++ {
1109 c := createClientConn(b, o1.Host, o1.Port)
1110 doDefaultConnect(b, c)
1111 flushConnection(b, c)
1112 ch := make(chan bool)
1113
1114 go pubLoop(c, ch)
1115 <-ch
1116 }
1117
1118 b.SetBytes(int64(len(sendOp) + len(msgOp)))
1119 b.ResetTimer()
1120
1121 // Closing this will start all publishers at once (roughly)
1122 close(startCh)
1123
1124 // Wait for connections to be drained
1125 for i := 0; i < len(clients); i++ {
1126 <-clients[i]
1127 }
1128 b.StopTimer()
1129 }
1130
1131 func Benchmark_____FanIn_1kx100x1(b *testing.B) {
1132 doFanIn(b, 1, 100, 1, sub, sizedString(1024))
1133 }
1134
1135 func Benchmark_____FanIn_4kx100x1(b *testing.B) {
1136 doFanIn(b, 1, 100, 1, sub, sizedString(4096))
1137 }
1138
1139 func Benchmark_____FanIn_8kx100x1(b *testing.B) {
1140 doFanIn(b, 1, 100, 1, sub, sizedString(8192))
1141 }
1142
1143 func Benchmark____FanIn_16kx100x1(b *testing.B) {
1144 doFanIn(b, 1, 100, 1, sub, sizedString(16384))
1145 }
1146
1147 func Benchmark____FanIn_64kx100x1(b *testing.B) {
1148 doFanIn(b, 1, 100, 1, sub, sizedString(65536))
1149 }
1150
1151 func Benchmark___FanIn_128kx100x1(b *testing.B) {
1152 doFanIn(b, 1, 100, 1, sub, sizedString(65536*2))
1153 }
1154
1155 func Benchmark___BumpPubCount_1x3(b *testing.B) {
1156 doFanIn(b, 1, 1, 3, sub, sizedString(128))
1157 }
1158
1159 func Benchmark___BumpPubCount_2x3(b *testing.B) {
1160 doFanIn(b, 1, 2, 3, sub, sizedString(128))
1161 }
1162
1163 func Benchmark___BumpPubCount_5x3(b *testing.B) {
1164 doFanIn(b, 1, 5, 3, sub, sizedString(128))
1165 }
1166
1167 func Benchmark__BumpPubCount_10x3(b *testing.B) {
1168 doFanIn(b, 1, 10, 3, sub, sizedString(128))
1169 }
1170
1171 func testDefaultBenchOptionsForGateway(name string) *server.Options {
1172 opts := testDefaultOptionsForGateway(name)
1173 opts.DisableShortFirstPing = true
1174 return opts
1175 }
1176
1177 func gatewaysBench(b *testing.B, optimisticMode bool, payload string, numPublishers int, subInterest bool) {
1178 b.Helper()
1179 if b.N < numPublishers {
1180 return
1181 }
1182
1183 ob := testDefaultBenchOptionsForGateway("B")
1184 sb := RunServer(ob)
1185 defer sb.Shutdown()
1186
1187 server.SetGatewaysSolicitDelay(10 * time.Millisecond)
1188 defer server.ResetGatewaysSolicitDelay()
1189
1190 gwbURL, err := url.Parse(fmt.Sprintf("nats://%s:%d", ob.Gateway.Host, ob.Gateway.Port))
1191 if err != nil {
1192 b.Fatalf("Error parsing url: %v", err)
1193 }
1194 oa := testDefaultBenchOptionsForGateway("A")
1195 oa.Gateway.Gateways = []*server.RemoteGatewayOpts{
1196 {
1197 Name: "B",
1198 URLs: []*url.URL{gwbURL},
1199 },
1200 }
1201 sa := RunServer(oa)
1202 defer sa.Shutdown()
1203
1204 sub := createClientConn(b, ob.Host, ob.Port)
1205 defer sub.Close()
1206 doDefaultConnect(b, sub)
1207 sendProto(b, sub, "SUB end.test 1\r\n")
1208 if subInterest {
1209 sendProto(b, sub, "SUB foo 2\r\n")
1210 }
1211 flushConnection(b, sub)
1212
1213 // If not optimisticMode, make B switch GW connection
1214 // to interest mode only
1215 if !optimisticMode {
1216 pub := createClientConn(b, oa.Host, oa.Port)
1217 doDefaultConnect(b, pub)
1218 // has to be more that defaultGatewayMaxRUnsubBeforeSwitch
1219 for i := 0; i < 2000; i++ {
1220 sendProto(b, pub, fmt.Sprintf("PUB reject.me.%d 2\r\nok\r\n", i+1))
1221 }
1222 flushConnection(b, pub)
1223 pub.Close()
1224 }
1225
1226 ch := make(chan bool)
1227 var msgOp string
1228 var expected int
1229 l := b.N / numPublishers
1230 if subInterest {
1231 msgOp = fmt.Sprintf("MSG foo 2 %d\r\n%s\r\n", len(payload), payload)
1232 expected = len(msgOp) * l * numPublishers
1233 }
1234 // Last message sent to end.test
1235 lastMsg := "MSG end.test 1 2\r\nok\r\n"
1236 expected += len(lastMsg) * numPublishers
1237 go drainConnection(b, sub, ch, expected)
1238
1239 sendOp := []byte(fmt.Sprintf("PUB foo %d\r\n%s\r\n", len(payload), payload))
1240 startCh := make(chan bool)
1241
1242 lastMsgSendOp := []byte("PUB end.test 2\r\nok\r\n")
1243
1244 pubLoop := func(c net.Conn, ch chan bool) {
1245 bw := bufio.NewWriterSize(c, defaultSendBufSize)
1246
1247 // Signal we are ready
1248 close(ch)
1249
1250 // Wait to start up actual sends.
1251 <-startCh
1252
1253 for i := 0; i < l; i++ {
1254 if _, err := bw.Write(sendOp); err != nil {
1255 b.Errorf("Received error on PUB write: %v\n", err)
1256 return
1257 }
1258 }
1259 if _, err := bw.Write(lastMsgSendOp); err != nil {
1260 b.Errorf("Received error on PUB write: %v\n", err)
1261 return
1262 }
1263 if err := bw.Flush(); err != nil {
1264 b.Errorf("Received error on FLUSH write: %v\n", err)
1265 return
1266 }
1267 flushConnection(b, c)
1268 }
1269
1270 // Publish Connections SPINUP
1271 for i := 0; i < numPublishers; i++ {
1272 c := createClientConn(b, oa.Host, oa.Port)
1273 defer c.Close()
1274 doDefaultConnect(b, c)
1275 flushConnection(b, c)
1276 ch := make(chan bool)
1277
1278 go pubLoop(c, ch)
1279 <-ch
1280 }
1281
1282 // To report the number of bytes:
1283 // from publisher to server on cluster A:
1284 numBytes := len(sendOp)
1285 if subInterest {
1286 // from server in cluster A to server on cluster B:
1287 // RMSG $G foo <payload size> <payload>\r\n
1288 numBytes += len("RMSG $G foo xxxx ") + len(payload) + 2
1289
1290 // From server in cluster B to sub:
1291 numBytes += len(msgOp)
1292 }
1293 b.SetBytes(int64(numBytes))
1294 b.ResetTimer()
1295
1296 // Closing this will start all publishers at once (roughly)
1297 close(startCh)
1298
1299 // Wait for end of test
1300 <-ch
1301
1302 b.StopTimer()
1303 }
1304
1305 func Benchmark____GWs_Opt_1kx01x0(b *testing.B) {
1306 gatewaysBench(b, true, sizedString(1024), 1, false)
1307 }
1308
1309 func Benchmark____GWs_Opt_2kx01x0(b *testing.B) {
1310 gatewaysBench(b, true, sizedString(2048), 1, false)
1311 }
1312
1313 func Benchmark____GWs_Opt_4kx01x0(b *testing.B) {
1314 gatewaysBench(b, true, sizedString(4096), 1, false)
1315 }
1316
1317 func Benchmark____GWs_Opt_1kx10x0(b *testing.B) {
1318 gatewaysBench(b, true, sizedString(1024), 10, false)
1319 }
1320
1321 func Benchmark____GWs_Opt_2kx10x0(b *testing.B) {
1322 gatewaysBench(b, true, sizedString(2048), 10, false)
1323 }
1324
1325 func Benchmark____GWs_Opt_4kx10x0(b *testing.B) {
1326 gatewaysBench(b, true, sizedString(4096), 10, false)
1327 }
1328
1329 func Benchmark____GWs_Opt_1kx01x1(b *testing.B) {
1330 gatewaysBench(b, true, sizedString(1024), 1, true)
1331 }
1332
1333 func Benchmark____GWs_Opt_2kx01x1(b *testing.B) {
1334 gatewaysBench(b, true, sizedString(2048), 1, true)
1335 }
1336
1337 func Benchmark____GWs_Opt_4kx01x1(b *testing.B) {
1338 gatewaysBench(b, true, sizedString(4096), 1, true)
1339 }
1340
1341 func Benchmark____GWs_Opt_1kx10x1(b *testing.B) {
1342 gatewaysBench(b, true, sizedString(1024), 10, true)
1343 }
1344
1345 func Benchmark____GWs_Opt_2kx10x1(b *testing.B) {
1346 gatewaysBench(b, true, sizedString(2048), 10, true)
1347 }
1348
1349 func Benchmark____GWs_Opt_4kx10x1(b *testing.B) {
1350 gatewaysBench(b, true, sizedString(4096), 10, true)
1351 }
1352
1353 func Benchmark____GWs_Int_1kx01x0(b *testing.B) {
1354 gatewaysBench(b, false, sizedString(1024), 1, false)
1355 }
1356
1357 func Benchmark____GWs_Int_2kx01x0(b *testing.B) {
1358 gatewaysBench(b, false, sizedString(2048), 1, false)
1359 }
1360
1361 func Benchmark____GWs_Int_4kx01x0(b *testing.B) {
1362 gatewaysBench(b, false, sizedString(4096), 1, false)
1363 }
1364
1365 func Benchmark____GWs_Int_1kx10x0(b *testing.B) {
1366 gatewaysBench(b, false, sizedString(1024), 10, false)
1367 }
1368
1369 func Benchmark____GWs_Int_2kx10x0(b *testing.B) {
1370 gatewaysBench(b, false, sizedString(2048), 10, false)
1371 }
1372
1373 func Benchmark____GWs_Int_4kx10x0(b *testing.B) {
1374 gatewaysBench(b, false, sizedString(4096), 10, false)
1375 }
1376
1377 func Benchmark____GWs_Int_1kx01x1(b *testing.B) {
1378 gatewaysBench(b, false, sizedString(1024), 1, true)
1379 }
1380
1381 func Benchmark____GWs_Int_2kx01x1(b *testing.B) {
1382 gatewaysBench(b, false, sizedString(2048), 1, true)
1383 }
1384
1385 func Benchmark____GWs_Int_4kx01x1(b *testing.B) {
1386 gatewaysBench(b, false, sizedString(4096), 1, true)
1387 }
1388
1389 func Benchmark____GWs_Int_1kx10x1(b *testing.B) {
1390 gatewaysBench(b, false, sizedString(1024), 10, true)
1391 }
1392
1393 func Benchmark____GWs_Int_2kx10x1(b *testing.B) {
1394 gatewaysBench(b, false, sizedString(2048), 10, true)
1395 }
1396
1397 func Benchmark____GWs_Int_4kx10x1(b *testing.B) {
1398 gatewaysBench(b, false, sizedString(4096), 10, true)
1399 }
1400
1401 // This bench only sends the requests to verify impact
1402 // of reply mapping in GW code.
1403 func gatewaySendRequestsBench(b *testing.B, singleReplySub bool) {
1404 server.SetGatewaysSolicitDelay(10 * time.Millisecond)
1405 defer server.ResetGatewaysSolicitDelay()
1406
1407 ob := testDefaultBenchOptionsForGateway("B")
1408 sb := RunServer(ob)
1409 defer sb.Shutdown()
1410
1411 gwbURL, err := url.Parse(fmt.Sprintf("nats://%s:%d", ob.Gateway.Host, ob.Gateway.Port))
1412 if err != nil {
1413 b.Fatalf("Error parsing url: %v", err)
1414 }
1415 oa := testDefaultBenchOptionsForGateway("A")
1416 oa.Gateway.Gateways = []*server.RemoteGatewayOpts{
1417 {
1418 Name: "B",
1419 URLs: []*url.URL{gwbURL},
1420 },
1421 }
1422 sa := RunServer(oa)
1423 defer sa.Shutdown()
1424
1425 sub := createClientConn(b, ob.Host, ob.Port)
1426 defer sub.Close()
1427 doDefaultConnect(b, sub)
1428 sendProto(b, sub, "SUB foo 1\r\n")
1429 flushConnection(b, sub)
1430
1431 lenMsg := len("MSG foo reply.xxxxxxxxxx 1 2\r\nok\r\n")
1432 expected := b.N * lenMsg
1433 ch := make(chan bool, 1)
1434 go drainConnection(b, sub, ch, expected)
1435
1436 c := createClientConn(b, oa.Host, oa.Port)
1437 defer c.Close()
1438 doDefaultConnect(b, c)
1439 flushConnection(b, c)
1440
1441 // From pub to server in cluster A:
1442 numBytes := len("PUB foo reply.0123456789 2\r\nok\r\n")
1443 if !singleReplySub {
1444 // Add the preceding SUB
1445 numBytes += len("SUB reply.0123456789 0123456789\r\n")
1446 // And UNSUB...
1447 numBytes += len("UNSUB 0123456789\r\n")
1448 }
1449 // From server in cluster A to cluster B
1450 numBytes += len("RMSG $G foo reply.0123456789 2\r\nok\r\n")
1451 // If mapping of reply...
1452 if !singleReplySub {
1453 // the mapping uses about 24 more bytes. So add them
1454 // for RMSG from server to server.
1455 numBytes += 24
1456 }
1457 // From server in cluster B to sub
1458 numBytes += lenMsg
1459 b.SetBytes(int64(numBytes))
1460
1461 bw := bufio.NewWriterSize(c, defaultSendBufSize)
1462 var subStr string
1463
1464 b.ResetTimer()
1465
1466 for i := 0; i < b.N; i++ {
1467 if !singleReplySub {
1468 subStr = fmt.Sprintf("SUB reply.%010d %010d\r\n", i+1, i+1)
1469 }
1470 bw.Write([]byte(fmt.Sprintf("%sPUB foo reply.%010d 2\r\nok\r\n", subStr, i+1)))
1471 // Simulate that we are doing actual request/reply and therefore
1472 // unsub'ing the subs on the reply subject.
1473 if !singleReplySub && i > 1000 {
1474 bw.Write([]byte(fmt.Sprintf("UNSUB %010d\r\n", (i - 1000))))
1475 }
1476 }
1477 bw.Flush()
1478 flushConnection(b, c)
1479
1480 <-ch
1481 }
1482
1483 func Benchmark__GWs_Reqs_1_SubAll(b *testing.B) {
1484 gatewaySendRequestsBench(b, true)
1485 }
1486
1487 func Benchmark__GWs_Reqs_1SubEach(b *testing.B) {
1488 gatewaySendRequestsBench(b, false)
1489 }