This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
loadtester.go
141 lines (130 loc) · 2.95 KB
/
loadtester.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
package main
import (
"context"
"fmt"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"github.com/ronaksoft/rony"
"github.com/ronaksoft/rony/internal/log"
"github.com/ronaksoft/rony/internal/testEnv/pb/service"
"github.com/ronaksoft/rony/tools"
"go.uber.org/zap"
"net"
"os"
"sync"
"sync/atomic"
"time"
)
/*
Creation Time: 2019 - Jun - 05
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
var (
cntConnected int32
cntReceivedPacket int32
cntWritePacket int32
latency int64
)
func main() {
fmt.Println("RUN...")
log.Init(log.DefaultConfig)
log.SetLevel(log.InfoLevel)
var n, m, port int64
switch len(os.Args) {
case 4:
n = tools.StrToInt64(os.Args[1])
m = tools.StrToInt64(os.Args[2])
port = tools.StrToInt64(os.Args[3])
default:
fmt.Println("needs 3 args, n m port")
return
}
go func() {
for {
log.Info("Stats",
zap.Int32("Received", cntReceivedPacket),
zap.Int32("Sent", cntWritePacket),
zap.Int32("Connected", cntConnected),
)
time.Sleep(time.Second)
}
}()
runClients(int(n), int(m), int(port))
log.Info("Final Stats",
zap.Int32("Received", cntReceivedPacket),
zap.Int32("Sent", cntWritePacket),
zap.Int32("Connected", cntConnected),
zap.Duration("Avg Latency", time.Duration(latency/int64(cntReceivedPacket))),
)
}
func runClients(n, m, port int) {
waitGroup := sync.WaitGroup{}
for i := 0; i < n; i++ {
waitGroup.Add(1)
go func(i int) {
runClient(&waitGroup, m, port)
waitGroup.Done()
}(i)
time.Sleep(time.Millisecond)
}
waitGroup.Wait()
log.Debug("Running Clients Finished")
}
func runClient(wg *sync.WaitGroup, m int, port int) {
var (
c net.Conn
err error
)
for {
c, _, _, err = ws.Dialer{}.Dial(context.Background(), fmt.Sprintf("ws://localhost:%d", port))
if err == nil {
break
}
log.Warn("error on connect: ", zap.Error(err))
time.Sleep(time.Millisecond)
}
atomic.AddInt32(&cntConnected, 1)
wg.Add(1)
go func() {
time.Sleep(time.Second)
defer wg.Done()
var m []wsutil.Message
for {
m = m[:0]
_ = c.SetReadDeadline(time.Now().Add(time.Second * 3))
startTime := tools.CPUTicks()
m, err = wsutil.ReadServerMessage(c, m)
d := time.Duration(tools.CPUTicks() - startTime)
if err != nil {
err = c.Close()
if err != nil {
log.Warn("Error on Close", zap.Error(err))
}
return
}
atomic.AddInt64(&latency, int64(d))
atomic.AddInt32(&cntReceivedPacket, int32(len(m)))
}
}()
echoRequest := service.EchoRequest{
Int: 21313,
Timestamp: 42342342342,
}
req := rony.PoolMessageEnvelope.Get()
req.Fill(tools.RandomUint64(0), service.C_SampleEcho, &echoRequest)
reqBytes, _ := req.Marshal()
for i := 0; i < m; i++ {
err := wsutil.WriteClientMessage(c, ws.OpBinary, reqBytes)
if err != nil {
log.Error(err.Error())
_ = c.Close()
return
} else {
atomic.AddInt32(&cntWritePacket, 1)
}
}
}