/
main_test.go
50 lines (38 loc) · 853 Bytes
/
main_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
package main
import (
"code.google.com/p/go.net/websocket"
"fmt"
"net"
"testing"
"time"
)
var startedMain bool = false
func BenchmarkWebSocketConnection(b *testing.B) {
b.StopTimer()
if !startedMain {
go main()
startedMain = true
time.Sleep(100)
}
srvAddr := "localhost:8080"
config, _ := wsConnSetup(srvAddr)
b.StartTimer()
for i := 0; i < b.N; i++ {
client, _ := net.Dial("tcp", srvAddr)
conn, _ := websocket.NewClient(config, client)
msg := []byte(`[{
"name": "user:new",
"args": {"id": -1}
}]`)
conn.Write(msg)
conn.Close()
}
}
func wsConnSetup(srvAddr string) (config *websocket.Config, err error) {
tcpAddr, err := net.ResolveTCPAddr("tcp", srvAddr)
if err != nil {
return nil, err
}
config, _ = websocket.NewConfig(fmt.Sprintf("ws://%s%s", tcpAddr, "/ws"), "http://localhost/ws")
return
}