-
Notifications
You must be signed in to change notification settings - Fork 0
/
goroutine-per-conn-cli.go
62 lines (43 loc) · 1.08 KB
/
goroutine-per-conn-cli.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
package main
import (
"net"
"fmt"
"flag"
"time"
)
var (
ip = flag.String("ip", "127.0.0.1", "Server IP")
connections = flag.Int("conn", 1, "number of tcp connections")
)
func main() {
//分析参数
flag.Parse()
addr := *ip + ":12345"
fmt.Println("连接到:%s", addr)
var conns []net.Conn
for i := 0; i < *connections; i++ {
c, err := net.DialTimeout("tcp", addr, 10 *time.Second)
if err != nil {
fmt.Println("failed to connect", i, err)
}
conns = append(conns, c)
time.Sleep(time.Millisecond)
}
defer func() {
for _, c := range conns {
c.Close()
}
}()
fmt.Println("完成初始化 %d 连接", len(conns))
tts := time.Second
if *connections > 100 {
tts = time.Millisecond * 5
}
for {
for i := 0; i < len(conns); i++ {
time.Sleep(tts)
conn := conns[i]
conn.Write([]byte("hello world\r\n"))
}
}
}