This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
/
wsbench.go
108 lines (92 loc) · 2.43 KB
/
wsbench.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
package main
import (
"io"
"log"
"math/rand"
"net/http"
"time"
"github.com/fasthttp/websocket"
"github.com/samber/lo"
"github.com/tidwall/gjson"
"google.golang.org/protobuf/proto"
"exusiai.dev/gommon/model/pb"
)
func penguinV3GetRequest(path string) []byte {
log.Println("Fetching", path)
r := lo.Must(http.NewRequest("GET", "https://penguin-stats.io/api/v3alpha"+path, nil))
r.Header.Set("Accept", "application/vnd.penguin.v3+json")
resp := lo.Must(http.DefaultClient.Do(r))
defer resp.Body.Close()
return lo.Must(io.ReadAll(resp.Body))
}
func penguinItemIds() []uint32 {
resp := penguinV3GetRequest("/items")
gj := gjson.ParseBytes(resp).Array()
ids := make([]uint32, len(gj))
for i, v := range gj {
ids[i] = uint32(v.Get("penguinItemId").Uint())
}
return ids
}
func penguinStageIds() []uint32 {
resp := penguinV3GetRequest("/stages")
gj := gjson.ParseBytes(resp).Array()
ids := make([]uint32, len(gj))
for i, v := range gj {
ids[i] = uint32(v.Get("penguinStageId").Uint())
}
return ids
}
func randomInSlice(slice []uint32) uint32 {
return slice[rand.Intn(len(slice))]
}
var (
Servers = []pb.Server{pb.Server_CN, pb.Server_US, pb.Server_JP, pb.Server_KR}
Items = penguinItemIds()
Stages = penguinStageIds()
)
func getRandomSubscriptionReq() []byte {
if rand.Intn(2) == 0 {
return lo.Must(proto.Marshal(&pb.MatrixUpdateSubscribeReq{
Id: &pb.MatrixUpdateSubscribeReq_StageId{
StageId: randomInSlice(Stages),
},
Server: Servers[rand.Intn(len(Servers))],
}))
} else {
return lo.Must(proto.Marshal(&pb.MatrixUpdateSubscribeReq{
Id: &pb.MatrixUpdateSubscribeReq_ItemId{
ItemId: randomInSlice(Items),
},
Server: Servers[rand.Intn(len(Servers))],
}))
}
}
func main() {
for i := 0; i < 5000; i++ {
conn, resp, err := websocket.DefaultDialer.Dial("ws://localhost:9020/live", http.Header{
"Sec-WebSocket-Protocol": []string{"v3.penguin-stats.live+proto"},
})
if err != nil {
log.Println("dial error:", err, resp)
panic(err)
}
go func(i int) {
err := conn.WriteMessage(websocket.BinaryMessage, getRandomSubscriptionReq())
if err != nil {
log.Println("write error:", err)
return
}
log.Println("client", i, "is ready")
for {
_, _, err = conn.ReadMessage()
if err != nil {
log.Println("read error:", err)
}
// log.Println("client", i, "received", mt, ":", hex.EncodeToString(b))
}
}(i)
time.Sleep(time.Millisecond * 1)
}
select {}
}