forked from GoBelieveIO/im_service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_connection.go
126 lines (107 loc) · 2.38 KB
/
benchmark_connection.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import "net"
import "log"
import "runtime"
import "time"
import "flag"
import "math/rand"
var first int64
var last int64
var local_ip string
var host string
var port int
func init() {
flag.Int64Var(&first, "first", 0, "first uid")
flag.Int64Var(&last, "last", 0, "last uid")
flag.StringVar(&local_ip, "local_ip", "0.0.0.0", "local ip")
flag.StringVar(&host, "host", "127.0.0.1", "host")
flag.IntVar(&port, "port", 23000, "port")
}
func receive(uid int64) {
ip := net.ParseIP(host)
addr := net.TCPAddr{ip, port, ""}
lip := net.ParseIP(local_ip)
laddr := net.TCPAddr{lip, 0, ""}
conn, err := net.DialTCP("tcp4", &laddr, &addr)
if err != nil {
log.Println("connect error")
return
}
seq := 1
SendMessage(conn, &Message{MSG_AUTH, seq, DEFAULT_VERSION, &Authentication{uid: uid}})
ReceiveMessage(conn)
q := make(chan bool, 10)
wt := make(chan *Message, 10)
const HEARTBEAT_TIMEOUT = 3 * 60
go func() {
msgid := 0
ticker := time.NewTicker(HEARTBEAT_TIMEOUT * time.Second)
ticker2 := time.NewTicker(150 * time.Second)
for {
select {
case <-ticker2.C:
seq++
msgid++
receiver := first + rand.Int63()%(last-first)
im := &IMMessage{uid, receiver, 0, int32(msgid), "test"}
SendMessage(conn, &Message{MSG_IM, seq, DEFAULT_VERSION, im})
case <-ticker.C:
seq++
SendMessage(conn, &Message{MSG_PING, seq, DEFAULT_VERSION, nil})
case m := <-wt:
if m == nil {
q <- true
return
}
seq++
m.seq = seq
SendMessage(conn, m)
}
}
}()
go func() {
for {
msg := ReceiveMessage(conn)
if msg == nil {
wt <- nil
q <- true
return
}
if msg.cmd == MSG_IM || msg.cmd == MSG_GROUP_IM {
ack := &Message{cmd: MSG_ACK, body: &MessageACK{int32(msg.seq)}}
wt <- ack
}
}
}()
<-q
<-q
conn.Close()
}
func receive_loop(uid int64) {
for {
receive(uid)
n := rand.Int()
n = n % 20
time.Sleep(time.Duration(n) * time.Second)
}
}
func main() {
runtime.GOMAXPROCS(4)
rand.Seed(time.Now().Unix())
flag.Parse()
log.Printf("first:%d last:%d local ip:%s host:%s port:%d\n",
first, last, local_ip, host, port)
log.SetFlags(log.Lshortfile | log.LstdFlags)
c := make(chan bool, 100)
var i int64
var j int64
for i = first; i < last; i += 1000 {
for j = i; j < i+1000 && j < last; j++ {
go receive_loop(j)
}
time.Sleep(2 * time.Second)
}
for i = first; i < last; i++ {
<-c
}
}