@@ -3,7 +3,6 @@ package main
3
3
import (
4
4
"context"
5
5
"encoding/json"
6
- "errors"
7
6
"flag"
8
7
"fmt"
9
8
radix "github.com/mediocregopher/radix/v4"
@@ -12,6 +11,7 @@ import (
12
11
"log"
13
12
"os"
14
13
"os/signal"
14
+ "runtime/pprof"
15
15
"strings"
16
16
"sync"
17
17
"sync/atomic"
@@ -40,14 +40,14 @@ type testResult struct {
40
40
func subscriberRoutine (addr string , mode , subscriberName string , channel string , printMessages bool , ctx context.Context , wg * sync.WaitGroup , opts radix.Dialer , protocolVersion int ) {
41
41
// tell the caller we've stopped
42
42
defer wg .Done ()
43
+ client := redis .NewClient (& redis.Options {
44
+ Addr : addr ,
45
+ Password : opts .AuthPass ,
46
+ ClientName : subscriberName ,
47
+ ProtocolVersion : protocolVersion ,
48
+ })
43
49
switch mode {
44
50
case "ssubscribe" :
45
- client := redis .NewClient (& redis.Options {
46
- Addr : addr ,
47
- Password : opts .AuthPass ,
48
- ClientName : subscriberName ,
49
- ProtocolVersion : protocolVersion ,
50
- })
51
51
spubsub := client .SSubscribe (ctx , channel )
52
52
defer spubsub .Close ()
53
53
for {
@@ -64,53 +64,26 @@ func subscriberRoutine(addr string, mode, subscriberName string, channel string,
64
64
case "subscribe" :
65
65
fallthrough
66
66
default :
67
- _ , _ , ps , _ := bootstrapPubSub ( addr , subscriberName , channel , opts )
68
- defer ps .Close ()
67
+ pubsub := client . Subscribe ( ctx , channel )
68
+ defer pubsub .Close ()
69
69
for {
70
- msg , err := ps .Next (ctx )
71
- if errors .Is (err , context .Canceled ) {
72
- break
73
- } else if err != nil {
70
+ msg , err := pubsub .ReceiveMessage (ctx )
71
+ if err != nil {
74
72
panic (err )
75
73
}
76
74
if printMessages {
77
- fmt .Println (fmt .Sprintf ("received message in channel %s. Message: %s" , msg .Channel , msg .Message ))
75
+ fmt .Println (fmt .Sprintf ("received message in channel %s. Message: %s" , msg .Channel , msg .Payload ))
78
76
}
79
77
atomic .AddUint64 (& totalMessages , 1 )
80
78
}
81
-
82
- }
83
-
84
- }
85
-
86
- func bootstrapPubSub (addr string , subscriberName string , channel string , opts radix.Dialer ) (radix.Conn , error , radix.PubSubConn , * time.Ticker ) {
87
- // Create a normal redis connection
88
- ctx := context .Background ()
89
- conn , err := opts .Dial (ctx , "tcp" , addr )
90
-
91
- if err != nil {
92
- log .Fatal (err )
93
- }
94
-
95
- err = conn .Do (ctx , radix .FlatCmd (nil , "CLIENT" , "SETNAME" , subscriberName ))
96
- if err != nil {
97
- log .Fatal (err )
98
- }
99
-
100
- // Pass that connection into PubSub, conn should never get used after this
101
- ps := radix.PubSubConfig {}.New (conn )
102
-
103
- err = ps .Subscribe (ctx , channel )
104
- if err != nil {
105
- log .Fatal (err )
106
79
}
107
80
108
- return conn , err , ps , nil
109
81
}
110
82
111
83
func main () {
112
84
host := flag .String ("host" , "127.0.0.1" , "redis host." )
113
85
port := flag .String ("port" , "6379" , "redis port." )
86
+ cpuprofile := flag .String ("cpuprofile" , "" , "write cpu profile to file" )
114
87
password := flag .String ("a" , "" , "Password for Redis Auth." )
115
88
mode := flag .String ("mode" , "subscribe" , "Subscribe mode. Either 'subscribe' or 'ssubscribe'." )
116
89
username := flag .String ("user" , "" , "Used to send ACL style 'AUTH username pass'. Needs -a." )
@@ -186,6 +159,13 @@ func main() {
186
159
total_subscriptions := total_channels * * subscribers_per_channel
187
160
total_messages := int64 (total_subscriptions ) * * messages_per_channel_subscriber
188
161
fmt .Println (fmt .Sprintf ("Total subcriptions: %d. Subscriptions per node %d. Total messages: %d" , total_subscriptions , subscriptions_per_node , total_messages ))
162
+ if * cpuprofile != "" {
163
+ f , err := os .Create (* cpuprofile )
164
+ if err != nil {
165
+ log .Fatal (err )
166
+ }
167
+ pprof .StartCPUProfile (f )
168
+ }
189
169
190
170
if strings .Compare (* subscribers_placement , "dense" ) == 0 {
191
171
for channel_id := * channel_minimum ; channel_id <= * channel_maximum ; channel_id ++ {
@@ -210,6 +190,10 @@ func main() {
210
190
closed , start_time , duration , totalMessages , messageRateTs := updateCLI (tick , c , total_messages , w , * test_time )
211
191
messageRate := float64 (totalMessages ) / float64 (duration .Seconds ())
212
192
193
+ if * cpuprofile != "" {
194
+ pprof .StopCPUProfile ()
195
+ }
196
+
213
197
fmt .Fprint (w , fmt .Sprintf ("#################################################\n Total Duration %f Seconds\n Message Rate %f\n #################################################\n " , duration .Seconds (), messageRate ))
214
198
fmt .Fprint (w , "\r \n " )
215
199
w .Flush ()
0 commit comments