-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
181 lines (167 loc) · 4.62 KB
/
utils.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
173
174
175
176
177
178
179
180
181
package main
import (
"fmt"
"log"
"sync"
"sync/atomic"
"time"
//"constraints"
"golang.org/x/exp/constraints"
"golang.org/x/time/rate"
"github.com/fiorix/go-smpp/smpp"
"github.com/fiorix/go-smpp/smpp/pdu/pdufield"
"github.com/fiorix/go-smpp/smpp/pdu/pdutext"
)
func NewSmppConnection(port int, ip string, username string, password string, send_rate int) (*SmppConnection, error) {
lm := rate.NewLimiter(rate.Limit(send_rate), 1) // Max rate of 10/s.
session := &smpp.Transceiver{
Addr: fmt.Sprintf("%s:%d", ip, port),
User: username,
Passwd: password,
Handler: nil, // Handle incoming SM or delivery receipts.
RateLimiter: lm, // Optional rate limiter.
}
conn := session.Bind()
if conn == nil {
log.Fatal("could not connect to the socket.")
}
return &SmppConnection{
socket: conn,
session: session,
healthy: true,
}, nil
}
// SmppConnection : is struct that represent the smpp
type SmppConnection struct {
socket <-chan smpp.ConnStatus
session *smpp.Transceiver
healthy bool
}
func (smppc SmppConnection) sendProc(amount int, respTimeC chan<- []time.Duration, throughputC chan<- []uint32, wg *sync.WaitGroup, from string, to string) {
r := make([]time.Duration, 0)
ts := make([]uint32, 0)
var count uint32
count = 0
ticker := time.NewTicker(time.Second)
done := make(chan bool)
go func() {
for {
log.Println("Inside the sendproc throughput loop")
select {
case <-ticker.C:
log.Printf("The Count per seesion now is %d \n", count)
ts = append(ts, count)
atomic.StoreUint32(&count, 0)
case <-done:
log.Println("Sending througput data to aggregate Proc")
log.Println(ts)
throughputC <- ts
log.Println("returning from througput data sendProc")
wg.Done()
return
}
}
}()
for i := 0; i < amount; i++ {
if smppc.healthy {
t := time.Now()
sm, err := smppc.session.Submit(&smpp.ShortMessage{
Src: from,
Dst: to,
Text: pdutext.Raw("Hello, world!"),
Register: pdufield.FinalDeliveryReceipt,
})
dt := time.Now().Sub(t)
count++
r = append(r, dt)
fmt.Println("Submit_resp duration", dt)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(sm.RespID())
} else {
log.Println("Will not send session is not healthy")
smppc.socket = smppc.session.Bind()
log.Println("Trying to re-bind()")
}
}
log.Println("Sending responsetime data to aggregate Proc")
done <- true
log.Println(r)
respTimeC <- r
//log.Println("Before wg.Done()")
//time.Sleep(time.Second)
//log.Println("After wg.Done()")
}
func (smppc SmppConnection) healthProc() {
log.Println("Starting health check proc.")
for {
if status := <-smppc.socket; status.Error() != nil {
log.Println("Unable to connect, aborting:", status.Error())
smppc.healthy = false
} else {
log.Println("Session healthy and its status: ", status.Status())
smppc.healthy = true
}
time.Sleep(time.Second)
}
}
func startBind(N int, ip string, port int, username string, password string, perSecond int) {
for i := 0; i <= N-1; i++ {
log.Println("IDX: ", i)
s, err := NewSmppConnection(port, ip, username, password, perSecond)
if err != nil {
log.Fatalf("Failed to create TRX session", err)
}
sessions = append(sessions, s)
}
log.Println("Sessions: ", sessions)
log.Println(sessions[0].healthy)
if !sessions[0].healthy {
log.Fatal("Not able to bind")
}
}
func startSubmit(wg *sync.WaitGroup, sess int, count int, from string, to string, collectChan chan []time.Duration, throughputC chan []uint32) {
for i := 0; i <= sess-1; i++ {
log.Printf("Spawn new smpp session [%d]\n", i)
wg.Add(1)
// Sessoin health check
go sessions[i].healthProc()
if sessions[i].healthy {
go func(i int) {
startTime := time.Now()
sessions[i].sendProc(count, collectChan, throughputC, wg, from, to)
log.Println("*** Overall SyncSend time is: ", time.Now().Sub(startTime))
}(i)
} else {
log.Println("Seem session not healthy !")
}
}
}
func calcResults[T constraints.Ordered](data []T) (T, T, T) {
var sum, max, mini T
max = data[0]
mini = data[0]
for _, i := range data {
sum += i
if i > max {
max = i
} else if i < mini {
mini = i
}
}
return sum, max, mini
}
func aggregate[T any](input <-chan []T, output chan<- []T, N int) {
log.Println("*.*.*.*.*.*. Start stats aggregation proc. *.*.*.*.*.*. ")
AllResp := make([]T, N-1)
log.Println("Got new stas from a smpp worker")
for respArray := range input {
AllResp = append(AllResp, respArray...)
log.Println("xxxxxxx Aggregate append done")
}
log.Println("Aggregate proc AllResp", AllResp)
output <- AllResp
log.Println("*.*.*.*.*.*. Done ")
}