-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (102 loc) · 2.83 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
package main
import (
"fmt"
"github.com/gorilla/websocket"
"log"
"strconv"
"sync"
"time"
)
var n_conn = 100
type LatencyData struct {
sync.Mutex
latencies []time.Duration
clientCount int
}
func connectAndListen(i int, dialer *websocket.Dialer) {
url := "ws://localhost:3000"
conn, _, err := dialer.Dial(url, nil)
if err != nil {
log.Fatal("Error connecting to WebSocket server:", err)
}
defer conn.Close()
fmt.Println("connection i=" + strconv.Itoa(i) + " connected")
for {
_, _, err := conn.ReadMessage()
if err != nil {
conn.Close()
fmt.Println("hata ", strconv.Itoa(i))
break
}
}
}
func latency_test(i int, dialer *websocket.Dialer, latencyData *LatencyData, shutdown chan int) {
url := "ws://localhost:3000"
conn, _, err := dialer.Dial(url, nil)
if err != nil {
log.Fatal("Error connecting to WebSocket server:", err)
}
fmt.Println("connection i=" + strconv.Itoa(i) + " connected")
message := make([]byte, 1024)
latencies := make([]time.Duration, 0)
for m := range 10 {
startTime := time.Now()
err2 := conn.WriteMessage(websocket.TextMessage, message)
if err2 != nil {
log.Println("error while sending message on client ", i, " try: ", m, " err: ", err2)
}
_, _, err2 = conn.ReadMessage()
if err2 != nil {
log.Println("error while reading message on client ", i, " try: ", m, " err: ", err2)
}
endTime := time.Now()
latencies = append(latencies, endTime.Sub(startTime))
}
terminate := false
latencyData.Lock()
latencyData.latencies = append(latencyData.latencies, latencies...)
latencyData.clientCount++
if latencyData.clientCount == n_conn {
terminate = true
}
latencyData.Unlock()
closeErr := conn.Close()
if closeErr != nil {
log.Println("error while closing connection ", i, " err: ", closeErr)
}
if terminate {
close(shutdown)
}
}
func calculateLatencyStats(latencyData *LatencyData) {
sumLatencies := time.Duration(0)
minLatency := latencyData.latencies[0]
maxLatency := latencyData.latencies[0]
for _, latency := range latencyData.latencies {
sumLatencies += latency
if latency < minLatency {
minLatency = latency
}
if latency > maxLatency {
maxLatency = latency
}
}
averageLatency := float64(sumLatencies.Microseconds()) / float64(len(latencyData.latencies))
fmt.Printf("average latency: %f ms \n", averageLatency/float64(1000))
fmt.Printf("min latency: %f ms \n", float64(minLatency.Microseconds())/float64(1000))
fmt.Printf("max latency: %f ms \n", float64(maxLatency.Microseconds())/float64(1000))
}
func main() {
c := make(chan int)
dialer := websocket.DefaultDialer
//latencyData := LatencyData{}
//shutdown := make(chan int)
for i := range n_conn {
time.Sleep(100 * time.Microsecond)
go connectAndListen(i, dialer)
//go latency_test(i, dialer, &latencyData, shutdown)
}
//<-shutdown
//calculateLatencyStats(&latencyData)
<-c
}