-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (115 loc) · 2.47 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
package main
// Start Command eg : ./push 0 20000 localhost:7172 60
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"runtime"
"strconv"
"time"
)
var (
httpClient *http.Client
t int
)
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())
begin, err := strconv.Atoi(os.Args[1])
if err != nil {
panic(err)
}
length, err := strconv.Atoi(os.Args[2])
if err != nil {
panic(err)
}
t, err = strconv.Atoi(os.Args[4])
if err != nil {
panic(err)
}
num := runtime.NumCPU() * 2
log.Printf("start routine num:%d", num)
l := length / num
b, e := begin, begin+l
time.AfterFunc(time.Duration(t)*time.Second, stop)
for i := 0; i < num; i++ {
go startPush(b, e)
b += l
e += l
}
if b < begin+length {
go startPush(b, begin+length)
}
time.Sleep(9999 * time.Hour)
}
func stop() {
os.Exit(-1)
}
func startPush(b, e int) {
log.Printf("start Push from %d to %d", b, e)
bodys := make([][]byte, e-b)
for i := 0; i < e-b; i++ {
msg := &pushBodyMsg{Msg: json.RawMessage(testContent), UserID: int64(b)}
body, err := json.Marshal(msg)
if err != nil {
panic(err)
}
bodys[i] = body
}
for {
for i := 0; i < len(bodys); i++ {
resp, err := httpPost(fmt.Sprintf("http://%s/goim/push/mids?operation=1000&mids=%d", os.Args[3], b), "application/x-www-form-urlencoded", bytes.NewBuffer(bodys[i]))
if err != nil {
log.Printf("post error (%v)", err)
continue
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("post error (%v)", err)
return
}
resp.Body.Close()
log.Printf("response %s", string(body))
//time.Sleep(50 * time.Millisecond)
}
}
}
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
}