golang websocket压测示例
package main
import (
"flag"
"fmt"
"log"
"net/url"
"time"
"github.com/gorilla/websocket"
)
//根据自身情况修改服务器ip地址
var addr = flag.String("addr", "localhost:12345", "http service address")
func main() {
succCount := 0
failCount := 0
//建立1000个websocket连接请求某数据
for i := 1; i < 1000; i++ {
u := url.URL{Scheme: "wss", Host: *addr, Path: "/ws"}
log.Println(u.String())
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Println(err)
failCount++
fmt.Println("Number of dialing failures:", failCount)
fmt.Println("Number of dialing fail:", err)
time.Sleep(time.Second)
continue
}
defer conn.Close()
succCount++
fmt.Println("Number of successful connections:", succCount)
go Client(conn)
}
//Stop here to prevent the program from exiting
tick :=