/
bencher.go
246 lines (209 loc) · 5.87 KB
/
bencher.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
242
243
244
245
246
// Copyright 2017-2018 zhvala
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package main
import (
"bytes"
"context"
"fmt"
"io"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"sync"
"sync/atomic"
"time"
socks5 "github.com/zhvala/gosocks5"
"golang.org/x/net/http2"
)
// Status contains:
// reqCounter, num of requests
// sucCounter, success num of requests, status code 200
// failCounter, failed num of requests, status code not 200
// errCounter, err num of requests, other error
// costCounter, total time cost of all requests
// sendCounter, total input bytes
// recvCounter, total output bytes
type Status struct {
startTime time.Time
reqCounter int64
sucCounter int64
failCounter int64
costCounter int64
sendCounter int64
recvCounter int64
}
// StatusFmt convert Status to readable string
func StatusFmt(status Status) (str string) {
seconds := int64(time.Now().Sub(status.startTime).Seconds())
if seconds <= 0 {
seconds = 1
}
str += fmt.Sprintln("Total requests: ", status.reqCounter)
str += fmt.Sprintln("Success requests: ", status.sucCounter)
str += fmt.Sprintln("Failed requests: ", status.failCounter)
str += fmt.Sprintln("Requests per second: ", status.reqCounter/seconds)
str += fmt.Sprintln("Average response time: ", time.Duration(status.costCounter/status.reqCounter))
str += fmt.Sprintln("Total send: ", humanize(status.sendCounter))
str += fmt.Sprintln("Total recv: ", humanize(status.recvCounter))
str += fmt.Sprintln("Send per second: ", humanize(status.sendCounter/seconds))
str += fmt.Sprintln("Recv per second: ", humanize(status.recvCounter/seconds))
return str
}
func humanize(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
}
//Bencher contains a amount of clients, each client runs in a goroutine
type Bencher struct {
args *CmdArgs
httpTran *http.Transport
wg *sync.WaitGroup
cancel context.CancelFunc
status Status
}
func createTransport(args *CmdArgs) *http.Transport {
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 15 * time.Second,
KeepAlive: 15 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConnsPerHost: args.Thread,
IdleConnTimeout: args.Duration,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
if args.Proxy != "" {
transport.Proxy = func(*http.Request) (*url.URL, error) {
return url.Parse(args.Proxy)
}
} else if args.SOCKS5 != "" {
transport.Dial = (&socks5.Client{
Addr: args.SOCKS5,
}).Dial
}
if args.Version == HTTP2 {
http2.ConfigureTransport(transport)
}
return transport
}
//NewBencher create a client bencher, return its pointer
func NewBencher(args *CmdArgs) *Bencher {
if args == nil {
panic("empty args")
}
bencher := Bencher{
args: args,
httpTran: createTransport(args),
status: Status{startTime: time.Now()},
}
return &bencher
}
func (bencher *Bencher) newRequest() (req *http.Request, size int64) {
var body io.Reader
if bencher.args.Data != "" {
body = bytes.NewBuffer([]byte(bencher.args.Data))
}
req, err := http.NewRequest(bencher.args.Method, bencher.args.URL, body)
if err != nil {
return nil, 0
}
reqDump, err := httputil.DumpRequest(req, true)
if err != nil {
return nil, 0
}
size = int64(len(reqDump))
if body != nil {
req.Header.Add("Content-Type", "application/json;charset=utf-8")
}
if bencher.args.Reload {
req.Header.Add("Pragma", "no-cache")
}
return req, size
}
func (bencher *Bencher) process(ctx context.Context) {
defer bencher.wg.Done()
httpCli := &http.Client{
Timeout: bencher.args.Timeout,
Transport: bencher.httpTran,
}
for {
select {
case <-ctx.Done():
return
default:
atomic.AddInt64(&bencher.status.reqCounter, 1)
start := time.Now()
req, sendSize := bencher.newRequest()
rep, err := httpCli.Do(req)
if err != nil {
fmt.Fprintln(os.Stderr, err)
atomic.AddInt64(&bencher.status.failCounter, 1)
continue
}
defer rep.Body.Close()
atomic.AddInt64(&bencher.status.sendCounter, sendSize)
if rep.StatusCode == 200 {
atomic.AddInt64(&bencher.status.sucCounter, 1)
} else {
atomic.AddInt64(&bencher.status.failCounter, 1)
}
cost := time.Now().Sub(start)
repDump, err := httputil.DumpResponse(rep, true)
if err != nil {
fmt.Fprintln(os.Stderr, err)
atomic.AddInt64(&bencher.status.failCounter, 1)
continue
}
atomic.AddInt64(&bencher.status.recvCounter, int64(len(repDump)))
atomic.AddInt64(&bencher.status.costCounter, int64(cost))
}
}
}
// Run and stop after deadline
func (bencher *Bencher) Run() context.Context {
ctx, cancel := context.WithTimeout(context.Background(), bencher.args.Duration)
bencher.cancel = cancel
bencher.wg = &sync.WaitGroup{}
bencher.wg.Add(bencher.args.Thread)
for i := 0; i < bencher.args.Thread; i++ {
go bencher.process(ctx)
}
return ctx
}
// Close all routine and exec cancel func
func (bencher *Bencher) Close() {
bencher.wg.Wait()
bencher.cancel()
}
// Terminate exec cancel func before timeout
func (bencher *Bencher) Terminate() {
bencher.cancel()
bencher.wg.Wait()
}
// Status return bencher.status
func (bencher *Bencher) Status() Status {
return bencher.status
}