Debian Codegraph @

_examples/custom_broker_nats/natsbroker/broker_test.go

raw symbols SCIP
1 package natsbroker
2
3 import (
4 "strconv"
5 "testing"
6
7 "github.com/centrifugal/centrifuge"
8 )
9
10 func newTestNatsBroker() *NatsBroker {
11 return NewTestNatsBrokerWithPrefix("centrifuge-test")
12 }
13
14 func NewTestNatsBrokerWithPrefix(prefix string) *NatsBroker {
15 n, _ := centrifuge.New(centrifuge.Config{})
16 b, _ := New(n, Config{Prefix: prefix})
17 n.SetBroker(b)
18 err := n.Run()
19 if err != nil {
20 panic(err)
21 }
22 return b
23 }
24
25 func BenchmarkNatsBrokerPublish(b *testing.B) {
26 broker := newTestNatsBroker()
27 rawData := []byte(`{"bench": true}`)
28 b.ResetTimer()
29 for i := 0; i < b.N; i++ {
30 _, err := broker.Publish("channel", rawData, centrifuge.PublishOptions{})
31 if err != nil {
32 panic(err)
33 }
34 }
35 }
36
37 func BenchmarkNatsBrokerPublishParallel(b *testing.B) {
38 broker := newTestNatsBroker()
39 rawData := []byte(`{"bench": true}`)
40 b.SetParallelism(128)
41 b.ResetTimer()
42 b.RunParallel(func(pb *testing.PB) {
43 for pb.Next() {
44 _, err := broker.Publish("channel", rawData, centrifuge.PublishOptions{})
45 if err != nil {
46 panic(err)
47 }
48 }
49 })
50 }
51
52 func BenchmarkNatsBrokerSubscribe(b *testing.B) {
53 broker := newTestNatsBroker()
54 j := 0
55 b.ResetTimer()
56 for i := 0; i < b.N; i++ {
57 j++
58 err := broker.Subscribe("subscribe" + strconv.Itoa(j))
59 if err != nil {
60 panic(err)
61 }
62 }
63 }
64
65 func BenchmarkNatsBrokerSubscribeParallel(b *testing.B) {
66 broker := newTestNatsBroker()
67 i := 0
68 b.SetParallelism(128)
69 b.ResetTimer()
70 b.RunParallel(func(pb *testing.PB) {
71 for pb.Next() {
72 i++
73 err := broker.Subscribe("subscribe" + strconv.Itoa(i))
74 if err != nil {
75 panic(err)
76 }
77 }
78 })
79 }