forked from rakyll/boom
/
boomer.go
191 lines (161 loc) · 4.42 KB
/
boomer.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
// Copyright 2014 Google Inc. All Rights Reserved.
//
// 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 boomer provides commands to run load tests and display results.
package boomer
import (
"crypto/tls"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"sync"
"time"
"golang.org/x/net/http2"
"net"
)
type result struct {
err error
statusCode int
duration time.Duration
contentLength int64
}
type Boomer struct {
// Request is the request to be made.
Request *http.Request
RequestBody string
// N is the total number of requests to make.
N int
// C is the concurrency level, the number of concurrent workers to run.
C int
// H2 is an option to make HTTP/2 requests
H2 bool
// Timeout in seconds.
Timeout int
// Qps is the rate limit.
Qps int
// DisableCompression is an option to disable compression in response
DisableCompression bool
// DisableKeepAlives is an option to prevents re-use of TCP connections between different HTTP requests
DisableKeepAlives bool
// Output represents the output type. If "csv" is provided, the
// output will be dumped as a csv stream.
Output string
// ProxyAddr is the address of HTTP proxy server in the format on "host:port".
// Optional.
ProxyAddr *url.URL
UDS string
results chan *result
}
// Run makes all the requests, prints the summary. It blocks until
// all work is done.
func (b *Boomer) Run() {
b.results = make(chan *result, b.N)
start := time.Now()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
// TODO(jbd): Progress bar should not be finalized.
newReport(b.N, b.results, b.Output, time.Now().Sub(start)).finalize()
os.Exit(1)
}()
b.runWorkers()
newReport(b.N, b.results, b.Output, time.Now().Sub(start)).finalize()
close(b.results)
}
func (b *Boomer) makeRequest(c *http.Client) {
s := time.Now()
var size int64
var code int
resp, err := c.Do(cloneRequest(b.Request, b.RequestBody))
if err == nil {
size = resp.ContentLength
code = resp.StatusCode
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}
b.results <- &result{
statusCode: code,
duration: time.Now().Sub(s),
err: err,
contentLength: size,
}
}
func dialUnix(proto, addr string) func(network, address string) (conn net.Conn, err error) {
return func(network, address string) (conn net.Conn, err error) {
return net.DialUnix(proto, nil, &net.UnixAddr{addr, proto})
}
}
func (b *Boomer) runWorker(n int) {
var throttle <-chan time.Time
if b.Qps > 0 {
throttle = time.Tick(time.Duration(1e6/(b.Qps)) * time.Microsecond)
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
DisableCompression: b.DisableCompression,
DisableKeepAlives: b.DisableKeepAlives,
// TODO(jbd): Add dial timeout.
TLSHandshakeTimeout: time.Duration(b.Timeout) * time.Millisecond,
Proxy: http.ProxyURL(b.ProxyAddr),
}
if b.UDS != "" {
tr = &http.Transport{
Dial: dialUnix("unix", b.UDS),
}
}
if b.H2 {
http2.ConfigureTransport(tr)
} else {
tr.TLSNextProto = make(map[string]func(string, *tls.Conn) http.RoundTripper)
}
client := &http.Client{Transport: tr}
for i := 0; i < n; i++ {
if b.Qps > 0 {
<-throttle
}
b.makeRequest(client)
}
}
func (b *Boomer) runWorkers() {
var wg sync.WaitGroup
wg.Add(b.C)
// Ignore the case where b.N % b.C != 0.
for i := 0; i < b.C; i++ {
go func() {
b.runWorker(b.N / b.C)
wg.Done()
}()
}
wg.Wait()
}
// cloneRequest returns a clone of the provided *http.Request.
// The clone is a shallow copy of the struct and its Header map.
func cloneRequest(r *http.Request, body string) *http.Request {
// shallow copy of the struct
r2 := new(http.Request)
*r2 = *r
// deep copy of the Header
r2.Header = make(http.Header, len(r.Header))
for k, s := range r.Header {
r2.Header[k] = append([]string(nil), s...)
}
r2.Body = ioutil.NopCloser(strings.NewReader(body))
return r2
}