-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
241 lines (201 loc) · 6.02 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"flag"
"fmt"
"github.com/ptttcode/gosst/internal/app"
"github.com/ptttcode/gosst/internal/pkg/ilog"
"github.com/ptttcode/gosst/internal/req"
"github.com/ptttcode/gosst/internal/socks"
"github.com/valyala/fasthttp"
"sort"
"strings"
"sync"
"time"
)
type Count struct {
count int
m sync.Mutex
}
var ct = &Count{
count: 0,
m: sync.Mutex{},
}
type sliceString []string
func (s *sliceString) String() string {
return strings.Join(*s, "\n")
}
func (s *sliceString) Set(str string) error {
*s = append(*s, str)
return nil
}
func getSum(s []int64) int64 {
var sum int64
for _, i := range s {
sum += i
}
return sum
}
func getRespSize(fv *app.FlagVar) (respSize int) {
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)
err := fv.Fc.Do(fv.FastReq, resp)
if err != nil {
ilog.GetLogger().Error("请求失败:", string(fv.FastReq.Host()), err.Error())
} else {
respSize = len(resp.Body()) + len(resp.Header.Header())
}
return
}
func main() {
var concurrency int
var totalRequests int64
var proxyAddr, dstAddr, username, passwd string
var headers sliceString
var contentType, body, method string
var clientCrtPath, clientKeyPath, caCrtPath string
flag.Var(&headers, "H", "Input the Headers you need. exp: -H 'appid:fdg231^%#a1' -H 'auth:xxxxxxxx' ")
flag.IntVar(&concurrency, "c", 10, "The number of simulated concurrent users")
flag.Int64Var(&totalRequests, "n", 1000, "The total number of requests")
flag.StringVar(&proxyAddr, "proxy", "", "Input the address of proxy server.")
flag.StringVar(&dstAddr, "dst", "", "Input the address of destination sever.")
flag.StringVar(&username, "u", "", "Input the username of auth in proxy server.")
flag.StringVar(&passwd, "p", "", "Input the password of auth in proxy server.")
flag.StringVar(&body, "b", "", "input body as string during request if you need.")
flag.StringVar(&contentType, "T", "text/plain", "Input the content type you need.")
flag.StringVar(&clientCrtPath, "crt", "", "Input the path of 'client.crt' file.")
flag.StringVar(&clientKeyPath, "key", "", "Input the path of 'client.key' file.")
flag.StringVar(&caCrtPath, "ca", "", "Input the path of 'ca.crt' file.")
flag.StringVar(&method, "m", "GET", "Input the request method. exp: GET, POST, PUT, DELETE...")
flag.Parse()
dstAddr = strings.Replace(dstAddr, "localhost", "http://127.0.0.1", 1)
b := []byte(body)
ilog.InitLogger("Gosst")
var reqTimeMap = sync.Map{}
var requestTimeSlice = []int64{}
ch := make(chan int, totalRequests/2)
wg := &sync.WaitGroup{}
headersMap := make(map[string]string)
for _, s := range headers {
v := strings.Split(s, ":")
if len(v) != 2 {
ilog.GetLogger().Warning("The given header value is not valid: ", s)
} else {
headersMap[v[0]] = v[1]
}
}
sa := &socks.SocksAuth{
Username: username,
Password: passwd,
}
fv := &app.FlagVar{
ConcurrentUsers: concurrency,
TotalRequests: totalRequests,
ProxyAddr: proxyAddr,
DstAddr: dstAddr,
Sa: sa,
Body: b,
Headers: headersMap,
ContentType: contentType,
Method: strings.ToUpper(method),
TlsConfig: app.GetTLSConfig(caCrtPath, clientCrtPath, clientKeyPath),
}
fv.RequestPrepare()
fv.FasthttpPrepare()
defer fv.FasthttpRelease()
var rh = req.NewRequestHandle(fv)
rh.FastRequest()
ccyPerRequest := totalRequests / int64(concurrency)
s := time.Now()
go func() {
//wg.Add(1)
//defer wg.Done()
for {
if n, ok := <-ch; ok == true {
ct.count += n
} else if len(ch) == 0 {
return
}
}
//for rep := range ch {
// ct.count += rep
//}
}()
for i := 0; i < concurrency; i++ {
wg.Add(1)
if i == concurrency-1 {
ccyPerRequest = totalRequests - (ccyPerRequest * (int64(concurrency) - 1))
}
go func(cc int, max int64) {
n := 0
var tmpSlice []int64
for j := int64(0); j < max; j++ {
s := time.Now()
err := rh.FastRequest()
// err = rh.NetDial()
if err != nil {
n++
}
consume := time.Now().Sub(s).Milliseconds()
tmpSlice = append(tmpSlice, consume)
}
reqTimeMap.Store(cc, tmpSlice)
ch <- n
wg.Done()
}(i, ccyPerRequest)
}
wg.Wait()
close(ch)
t := time.Now().Sub(s)
reqTimeMap.Range(func(key, value interface{}) bool {
v := value.([]int64)
requestTimeSlice = append(requestTimeSlice, v...)
return true
})
//fmt.Println(len(requestTimeSlice), t.Seconds(), concurrency, float64(totalRequests)/t.Seconds(), ct.count)
sort.Slice(requestTimeSlice, func(i, j int) bool {
return requestTimeSlice[i] < requestTimeSlice[j]
})
distribExp := []float32{0.50, 0.65, 0.75, 0.85, 0.95, 0.98, 0.99, 1.00}
length := len(requestTimeSlice)
fmt.Println(fmt.Sprintf("\n\nBenchmark %d times to %s by %d concurrency:\n", totalRequests, dstAddr, concurrency))
if proxyAddr != "" {
fmt.Println(fmt.Sprintf("Proxy Server Addr:\t%s", proxyAddr))
}
// 请求服务具体信息
serverAddrSplit := strings.Split(strings.Replace(dstAddr, "http://", "", 1), "/")
if len(serverAddrSplit) == 1 {
serverAddrSplit = append(serverAddrSplit, "/")
}
fmt.Println(fmt.Sprintf(
"Server Address:\t\t%s\nApi Path:\t\t%s",
serverAddrSplit[0], serverAddrSplit[1]),
)
// Requests详情
fmt.Println(fmt.Sprintf(
"Total Concurrency: %d\nTotal Requests: %d\nFailed Requests: %d\n",
concurrency, totalRequests, ct.count),
)
fmt.Println(fmt.Sprintf(
"Request per second: %.2f [req/sec]\nTime per request: %.2f ms",
float64(totalRequests)/t.Seconds(),
float64(getSum(requestTimeSlice)/int64(len(requestTimeSlice)))),
)
// Time详情
fmt.Println(fmt.Sprintf(
"Time taken for benchmark: %f s\n", t.Seconds()))
fmt.Println(
fmt.Sprintf(
"Total Sent: %d bytes\nTotal read: %d bytes\n",
int64(len(fv.FastReq.Header.Header())+len(fv.FastReq.Body()))*totalRequests,
int64(getRespSize(fv))*totalRequests,
),
)
fmt.Println("Percentage of the requests served within a certain time (ms)")
for _, v := range distribExp {
idx := int(float32(length)*v) - 1
if idx < 0 {
idx = 0
}
fmt.Println(fmt.Sprintf(" %d%% \t%dms", int(v*100), int(requestTimeSlice[idx])))
}
}