forked from kubemq-io/kubemq-go
/
main.go
172 lines (157 loc) · 4.67 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
package main
import (
"context"
"fmt"
"github.com/kubemq-io/kubemq-go"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"go.uber.org/atomic"
"log"
"math/rand"
"sync"
"time"
)
type Config struct {
Address string
Queue string
Send int
Threads int
Rounds int
AckDelay int
Receivers int
}
var (
_ = pflag.String("address", "localhost", "kubemq-address")
_ = pflag.String("queue", "d", "queue destination")
_ = pflag.Int("send", 100, "total send messages")
_ = pflag.Int("threads", 10, "total threads")
_ = pflag.Int("rounds", 10, "total rounds")
_ = pflag.Int("ack_delay", 0, "ack delay in seconds")
_ = pflag.Int("receivers", 1, "num of receivers for each threads")
)
func LoadConfig() (*Config, error) {
pflag.Parse()
cfg := &Config{}
_ = viper.BindEnv("Address", "ADDRESS")
_ = viper.BindEnv("Send", "SEND")
_ = viper.BindEnv("Threads", "THREADS")
_ = viper.BindEnv("Queue", "QUEUE")
_ = viper.BindEnv("Rounds", "ROUNDS")
_ = viper.BindEnv("AckDelay", "ACK_DELAY")
_ = viper.BindEnv("Receivers", "RECEIVERS")
_ = viper.BindPFlag("Address", pflag.CommandLine.Lookup("address"))
_ = viper.BindPFlag("Queue", pflag.CommandLine.Lookup("queue"))
_ = viper.BindPFlag("Send", pflag.CommandLine.Lookup("send"))
_ = viper.BindPFlag("Threads", pflag.CommandLine.Lookup("threads"))
_ = viper.BindPFlag("Rounds", pflag.CommandLine.Lookup("rounds"))
_ = viper.BindPFlag("AckDelay", pflag.CommandLine.Lookup("ack_delay"))
_ = viper.BindPFlag("Receivers", pflag.CommandLine.Lookup("receivers"))
err := viper.Unmarshal(cfg)
if err != nil {
return nil, err
}
return cfg, nil
}
func getFloatAvg(list []float64) float64 {
cnt := 0.0
for _, val := range list {
cnt += val
}
return cnt / float64(len(list))
}
func main() {
cfg, err := LoadConfig()
rand.Seed(time.Now().UnixNano())
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sender, err := kubemq.NewQueuesStreamClient(ctx,
kubemq.WithAddress(cfg.Address, 50000),
kubemq.WithClientId("stream-queue-sender"),
kubemq.WithTransportType(kubemq.TransportTypeGRPC))
if err != nil {
log.Fatal(err)
}
defer sender.Close()
var roundList []float64
cnt := 0
for {
cnt++
if cfg.Rounds > 0 && cnt > cfg.Rounds {
break
}
for i := 0; i < cfg.Threads; i++ {
channel := fmt.Sprintf("%s%d", cfg.Queue, i)
var batch []*kubemq.QueueMessage
for i := 0; i < cfg.Send; i++ {
batch = append(batch, kubemq.NewQueueMessage().
SetChannel(channel).SetBody([]byte("some-stream-simple-queue-message")))
}
_, err = sender.Batch(ctx, batch)
if err != nil {
log.Fatal(err)
}
log.Printf(fmt.Sprintf("%d messages sent to queue %s", cfg.Send, channel))
}
startAll := time.Now().UnixNano()
wg := sync.WaitGroup{}
wg.Add(cfg.Threads)
for i := 0; i < cfg.Threads; i++ {
go func(index int) {
defer wg.Done()
cnt := atomic.NewInt32(0)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
receiver, err := kubemq.NewQueuesStreamClient(ctx,
kubemq.WithAddress(cfg.Address, 50000),
kubemq.WithClientId(fmt.Sprintf("stream-queue-receiver-%d", index)),
kubemq.WithAutoReconnect(true),
kubemq.WithTransportType(kubemq.TransportTypeGRPC))
if err != nil {
log.Fatal(err)
}
defer receiver.Close()
for i := 0; i < cfg.Receivers; i++ {
go func(ctx context.Context, index int) {
channel := fmt.Sprintf("%s%d", cfg.Queue, index)
done, err := receiver.TransactionStream(ctx, kubemq.NewQueueTransactionMessageRequest().SetChannel(channel).SetVisibilitySeconds(10).SetWaitTimeSeconds(15), func(response *kubemq.QueueTransactionMessageResponse, err error) {
if err != nil {
} else {
cnt.Inc()
//log.Println (fmt.Sprintf("queue: %s, message seq %d received",response.Message.Channel,response.Message.Attributes.Sequence))
//t:=rand.Intn(1200-200+1) + 200
//time.Sleep(time.Duration(t) *time.Millisecond)
time.Sleep(time.Duration(cfg.AckDelay) * time.Millisecond)
err = response.Ack()
if err != nil {
//log.Fatal(err)
}
}
})
if err != nil {
return
}
<-ctx.Done()
done <- struct{}{}
}(ctx, index)
}
for {
select {
case <-time.After(1 * time.Millisecond):
if cnt.Load() >= int32(cfg.Send) {
return
}
}
}
}(i)
}
wg.Wait()
avgTime := float64(time.Now().UnixNano()-startAll) / 1e6
roundList = append(roundList, avgTime)
fmt.Println(fmt.Sprintf("Round %d, %d Messages received, overall avg time: %f ms", cnt, cfg.Send*cfg.Threads, getFloatAvg(roundList)))
time.Sleep(time.Second)
}
<-ctx.Done()
}