forked from Terry-Mao/goim
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
136 lines (115 loc) · 2.63 KB
/
main.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
127
128
129
130
131
132
133
134
135
136
package main
// Start Command eg : ./push_rooms 0 20000 localhost:7172 40
// param 1 : the start of room number
// param 2 : the end of room number
// param 3 : comet server tcp address
// param 4 : push amount each goroutines per second
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"runtime"
"strconv"
"time"
)
var (
lg *log.Logger
httpClient *http.Client
)
const TestContent = "{\"test\":1}"
type pushBodyMsg struct {
Msg json.RawMessage `json:"m"`
UserId int64 `json:"u"`
}
func init() {
httpTransport := &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
deadline := time.Now().Add(30 * time.Second)
c, err := net.DialTimeout(netw, addr, 20*time.Second)
if err != nil {
return nil, err
}
c.SetDeadline(deadline)
return c, nil
},
DisableKeepAlives: false,
}
httpClient = &http.Client{
Transport: httpTransport,
}
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
infoLogfi, err := os.OpenFile("./push_rooms.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
panic(err)
}
lg = log.New(infoLogfi, "", log.LstdFlags|log.Lshortfile)
begin, err := strconv.Atoi(os.Args[1])
if err != nil {
panic(err)
}
length, err := strconv.Atoi(os.Args[2])
if err != nil {
panic(err)
}
num, err := strconv.Atoi(os.Args[4])
if err != nil {
panic(err)
}
delay := (1000 * time.Millisecond) / time.Duration(num)
routines := runtime.NumCPU() * 2
lg.Printf("start routine num:%d", routines)
l := length / routines
b, e := begin, begin+l
for i := 0; i < routines; i++ {
go startPush(b, e, delay)
b += l
e += l
}
if b < begin+length {
go startPush(b, begin+length, delay)
}
time.Sleep(9999 * time.Hour)
}
func stop() {
os.Exit(-1)
}
func startPush(b, e int, delay time.Duration) {
lg.Printf("start Push from %d to %d", b, e)
for {
for i := b; i < e; i++ {
resp, err := http.Post(fmt.Sprintf("http://%s/1/push/room?rid=%d", os.Args[3], i), "application/json", bytes.NewBufferString(TestContent))
if err != nil {
lg.Printf("post error (%v)", err)
continue
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
lg.Printf("post error (%v)", err)
return
}
resp.Body.Close()
lg.Printf("push room:%d response %s", i, string(body))
time.Sleep(delay)
}
}
}
func httpPost(url string, contentType string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}