-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (97 loc) · 3.07 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
type myStruct struct {
Username string `json:"username"`
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}
func main() {
/*http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
})*/
var messageCounter uint64
var tNow time.Time
var tCurrent time.Duration
var threadId [5]int
var threadIndex int
var threadMsgCount [5]uint64
var threadMsgTime [5]time.Duration
var threadMsgStartTime [5]time.Time
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var conn, _ = upgrader.Upgrade(w, r, nil)
go func(conn *websocket.Conn) {
var threadIndexForCounter = threadIndex
threadId[threadIndexForCounter] = threadIndex
threadIndex++
tNow = time.Now()
threadMsgStartTime[threadIndexForCounter] = time.Now()
for {
mType, msg, err := conn.ReadMessage()
if err != nil {
tCurrent = time.Since(tNow)
threadMsgTime[threadIndexForCounter] = time.Since(threadMsgStartTime[threadIndexForCounter])
fmt.Println("Before Closing connection Total number of requests received: ", messageCounter, "In time: ", tCurrent, mType)
fmt.Println("TotalThreads:", threadIndex)
conn.Close()
return
}
messageCounter++
threadMsgCount[threadIndexForCounter]++
//fmt.Println(mType, string(msg))
fmt.Println("MsgSize ", len(msg), "Message number ", messageCounter, "Time Elapsed: ", time.Since(tNow))
//fmt.Println("Total number of requests received: ", messageCounter, "In time: ", time.Since(tNow))
/*
err = conn.WriteMessage(mType, msg)
if err != nil {
tCurrent = time.Since(tNow)
fmt.Println("Total number of requests received: ", messageCounter, "In time: ", tCurrent)
}*/
}
tCurrent = time.Since(tNow)
//fmt.Println("Total number of requests received: ", messageCounter, "In time: ", tCurrent)
}(conn)
fmt.Println("Total number of requests received: ", messageCounter, "In time: ", tCurrent)
go func(conn *websocket.Conn) {
ch := time.Tick(5 * time.Second)
for range ch {
conn.WriteJSON(myStruct{
"UsErNaMe", "FiRsTnAmE", "LaStNaMe",
})
}
}(conn)
})
//handle server interrupts
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
<-c
log.Println("Stopping Server ...")
tCurrent = time.Since(tNow)
fmt.Println("Total number of requests received: ", messageCounter, "In time: ", tCurrent)
fmt.Println("Total Threads ", threadIndex)
for index := 0; index <= threadIndex; index++ {
threadMsgTime[index] = time.Since(threadMsgStartTime[index])
fmt.Println("Thread Index: ", index, " Message Count: ", threadMsgCount[index], "Time Elapsed: ", threadMsgTime[index])
}
os.Exit(1)
}()
log.Fatal(http.ListenAndServe(":9000", nil))
// http.ListenAndServe(":9000", nil)
}