-
Notifications
You must be signed in to change notification settings - Fork 9
/
core.go
73 lines (62 loc) · 1.64 KB
/
core.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
package carrot
import (
"log"
"sync"
"time"
"github.com/gorilla/websocket"
)
func receiveMsg(wsconn *websocket.Conn, done chan *Routine, rout *Routine) {
for {
_, message, err := wsconn.ReadMessage()
rout.ReceiveTime = time.Now()
rout.Diff = rout.ReceiveTime.Sub(rout.SendTime)
rout.ReceivedMsg = string(message)
if err != nil {
log.Println("read:", err)
return
}
done <- rout
}
}
func writeMsg(wsconn *websocket.Conn, base *Base, rout *Routine) {
time.Sleep(time.Second * time.Duration(base.Delay))
rout.SendTime = time.Now()
wsconn.WriteMessage(websocket.TextMessage, base.Msg)
}
func singleTest(counter *Counter, queue chan *Routine, base *Base, rout *Routine) {
doneCh := make(chan *Routine)
conn, err := CreateSocket(base.URL, base.Proto, base.Path, counter)
if err != nil {
return
}
go writeMsg(conn, base, rout)
go receiveMsg(conn, doneCh, rout)
queue <- <-doneCh
}
func LoadTest(base *Base, latencyCh chan []float64, timeCh chan []time.Time) {
queue := make(chan *Routine, 1)
globalCounter := &Counter{0, sync.Mutex{}, 0, 0}
localCounter := 0
var latency []float64
var timeSeries []time.Time
for range time.Tick(time.Millisecond * time.Duration(base.TickDelay)) {
routine := &Routine{time.Now(), time.Now(), 0, ""}
go singleTest(globalCounter, queue, base, routine)
localCounter++
if localCounter == base.Count {
break
}
}
go func() {
bufferLimit := 0
for req := range queue {
latency = append(latency, req.Diff.Seconds()*1000)
timeSeries = append(timeSeries, req.SendTime)
bufferLimit++
if bufferLimit == base.Count {
latencyCh <- latency
timeCh <- timeSeries
}
}
}()
}