-
Notifications
You must be signed in to change notification settings - Fork 3
/
benchmarks_test.go
56 lines (47 loc) · 1 KB
/
benchmarks_test.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
package lightcable
import (
"context"
"math/rand"
"testing"
"github.com/gorilla/websocket"
)
func BenchmarkBroadcast(b *testing.B) {
server := New(DefaultConfig)
conns := makeConns(b, server, "/test", "/test")
ws, ws2 := conns[0], conns[1]
ctx, cancel := context.WithCancel(context.Background())
sign := make(chan bool)
server.OnServClose(func() {
sign <- true
})
join := make(chan string)
server.OnConnReady(func(c *Client) {
join <- c.Name
})
go server.Run(ctx)
// Need wait for connection ready
<-join
<-join
data := make([]byte, 4096)
n, err := rand.Read(data)
if err != nil {
b.Error(err)
}
for i := 0; i < b.N; i++ {
if err := ws.WriteMessage(websocket.TextMessage, data[:n]); err != nil {
b.Error(err)
}
if code, recv, err := ws2.ReadMessage(); err == nil {
if code != websocket.TextMessage {
b.Error("Type should TextMessage")
}
if string(recv) != string(data[:n]) {
b.Error("Data should Equal")
}
} else {
b.Error(err)
}
}
cancel()
<-sign
}