-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (87 loc) · 1.72 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
package main
import (
"context"
"github.com/gorilla/websocket"
"log"
"os"
"os/signal"
"strconv"
"sync/atomic"
"syscall"
"time"
)
var count int64
func main() {
args := os.Args[1:]
if len(args) != 2 {
log.Fatal("Mismatch arguments quantity: websocket-load-test [ws address] [threads num]")
}
threads, err := strconv.Atoi(args[1])
if err != nil {
log.Fatal("can't parse threads num", err)
}
log.Printf("connecting to the %s", args[0])
ctx, cancel := context.WithCancel(context.Background())
attempts := 0
go func() {
buf := make(chan struct{}, threads)
for {
buf <- struct{}{}
attempts++
go connection(ctx, args[0], buf)
}
}()
go func() {
tick := time.Tick(time.Second * 1)
prevAttempts := 0
for {
<-tick
if attempts != prevAttempts {
log.Printf("total connection attempts %d", attempts)
prevAttempts = attempts
}
}
}()
go func() {
tick := time.Tick(time.Second * 30)
for {
select {
case <-tick:
log.Printf("Handled %d messages", count)
count = 0
case <-ctx.Done():
return
}
}
}()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
log.Printf("terminated with sig %q", <-sigChan)
cancel()
}
func connection(ctx context.Context, host string, buf <-chan struct{}) {
defer func() {
<-buf
}()
c, _, err := websocket.DefaultDialer.Dial(host, nil)
if err != nil {
return
}
defer func() {
c.Close()
c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
}()
for {
select {
case <-ctx.Done():
return
default:
_, _, err := c.ReadMessage()
atomic.AddInt64(&count, 1)
if err != nil {
log.Println("read:", err)
return
}
}
}
}