-
Notifications
You must be signed in to change notification settings - Fork 0
/
thrash.go
309 lines (256 loc) · 7.14 KB
/
thrash.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof"
"net/url"
"os"
"strings"
"time"
"github.com/cheggaaa/pb"
"golang.org/x/text/message"
)
const DEFAULT_NUM_REQUESTS = 100
const DEFAULT_CONCURRENCY = 1
const DEFAULT_TIMEOUT = "60s"
type Configuration struct {
Concurrency int
NumRequests int
Timeout time.Duration
Histogram bool
PrintErrors bool
Profile bool
Url string
Headers map[string]string
Username string
Password string
}
type Response struct {
OK bool
Error error
StartTime time.Time
EndTime time.Time
Status string
StatusCode int
ContentLength int64
}
type ResponseSummary struct {
NumResponses int
NumOK int
BytesTransferred int64
SumResponseTimes time.Duration
MaxResponseTime time.Duration
MinResponseTime time.Duration
ResponseTimes []time.Duration
StatusCounts map[int]int
Errors []error
}
func (s *ResponseSummary) addResponse(r *Response) {
s.NumResponses++
if r.OK == false {
s.Errors = append(s.Errors, r.Error)
return
}
s.NumOK++
if s.StatusCounts == nil {
s.StatusCounts = map[int]int{}
}
s.StatusCounts[r.StatusCode]++
if r.ContentLength != -1 {
s.BytesTransferred += r.ContentLength
}
responseTime := r.EndTime.Sub(r.StartTime)
s.ResponseTimes = append(s.ResponseTimes, responseTime)
s.SumResponseTimes += responseTime
if s.MaxResponseTime == 0 {
s.MaxResponseTime = responseTime
}
if s.MinResponseTime == 0 {
s.MinResponseTime = responseTime
}
if responseTime > s.MaxResponseTime {
s.MaxResponseTime = responseTime
}
if responseTime < s.MinResponseTime {
s.MinResponseTime = responseTime
}
}
func (s *ResponseSummary) print() {
statusCountsString, _ := json.Marshal(s.StatusCounts)
pctOK := int((float64(s.NumOK) / float64(s.NumResponses)) * 100)
avgResponseTime := time.Duration(0)
if s.NumOK != 0 {
avgResponseTime = time.Duration(float64(s.SumResponseTimes) / float64(s.NumOK))
}
p := message.NewPrinter(message.MatchLanguage("en"))
p.Printf("Responses OK: %d%% (%d/%d), Errors: %d\n", pctOK, s.NumOK, s.NumResponses, len(s.Errors))
p.Printf("Status Codes: %s\n", statusCountsString)
p.Printf("Bytes Transferred: %d\n", s.BytesTransferred)
p.Printf("Avg Response Time: %v\n", avgResponseTime)
p.Printf("Min Response Time %v\n", s.MinResponseTime)
p.Printf("Max Response Time %v\n", s.MaxResponseTime)
}
func (s *ResponseSummary) printErrors() {
for _, err := range s.Errors {
fmt.Println(err)
}
}
func (s *ResponseSummary) printHistogram() {
scalingFactor := float64(100) / float64(len(s.ResponseTimes))
var buckets [5]int64
bucketLength := float64(s.MaxResponseTime-s.MinResponseTime) / 4
for _, responseTime := range s.ResponseTimes {
bucketTime := time.Duration(responseTime) - s.MinResponseTime
bucket := int(float64(bucketTime) / bucketLength)
buckets[bucket]++
}
for index, bucket := range buckets {
bucketStart := s.MinResponseTime + (time.Duration(bucketLength) * time.Duration(index))
bucketEnd := s.MinResponseTime + (time.Duration(bucketLength) * time.Duration(index+1))
fmt.Printf("(%3d%%) ", int(float64(bucket)*scalingFactor))
bricks := int(float64(bucket)*scalingFactor) / 2
for i := 0; i < bricks; i++ {
fmt.Print("∎")
if i == bricks-1 {
fmt.Print(" ")
}
}
fmt.Printf("[%v - %v]", bucketStart, bucketEnd)
fmt.Println()
}
}
func fetchURL(ack chan<- *Response, config Configuration, client *http.Client) {
req, err := http.NewRequest("GET", config.Url, nil)
if err != nil {
fmt.Println("Error creating new request")
}
if config.Username != "" && config.Password != "" {
req.SetBasicAuth(config.Username, config.Password)
}
for key, value := range config.Headers {
req.Header.Add(key, value)
}
response := &Response{OK: true, StartTime: time.Now()}
resp, err := client.Do(req)
response.EndTime = time.Now()
if err != nil {
response.OK = false
response.Error = err
ack <- response
return
}
response.Status = resp.Status
response.StatusCode = resp.StatusCode
response.ContentLength = resp.ContentLength
defer resp.Body.Close()
_, err = io.Copy(ioutil.Discard, resp.Body)
if err != nil {
response.OK = false
response.Error = err
fmt.Println("Error reading response body", err)
}
ack <- response
}
func startProfiler() {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
}
func configure() *Configuration {
config := Configuration{}
defaultTimeoutDuration, _ := time.ParseDuration(DEFAULT_TIMEOUT)
flag.IntVar(&config.Concurrency, "c", DEFAULT_CONCURRENCY, "how much concurrency")
flag.IntVar(&config.NumRequests, "n", DEFAULT_NUM_REQUESTS, "how many requests")
flag.DurationVar(&config.Timeout, "t", defaultTimeoutDuration, "request timeout in MS")
flag.BoolVar(&config.Histogram, "d", false, "print response time histogram")
flag.BoolVar(&config.PrintErrors, "e", false, "print errors")
//flag.BoolVar(&config.Profile, "p", false, "start the profile server on port 6060")
flag.StringVar(&config.Username, "u", "", "username for basic auth")
flag.StringVar(&config.Password, "p", "", "password for basic auth")
headerStr := flag.String("h", "", "request headers key:value")
flag.Parse()
flag.Usage = func() {
printUsage()
}
if len(os.Args) < 2 || os.Args[1] == "-help" {
printUsage()
os.Exit(1)
}
urlArg := os.Args[len(os.Args)-1]
_, err := url.ParseRequestURI(urlArg)
if err != nil {
fmt.Printf("Error: \"%s\" does not look like a valid url!\n", urlArg)
printUsage()
os.Exit(2)
} else {
config.Url = urlArg
}
if *headerStr != "" {
config.Headers = make(map[string]string)
headerSlice := strings.Fields(*headerStr)
for _, header := range headerSlice {
parts := strings.Split(header, ":")
if len(parts) != 2 {
return nil
}
key := parts[0]
value := parts[1]
config.Headers[key] = value
}
}
return &config
}
func printUsage() {
fmt.Fprintf(os.Stderr, "Usage: %s [flags] url\n", os.Args[0])
flag.PrintDefaults()
}
func main() {
configPtr := configure()
if configPtr == nil {
fmt.Println("Error in configuration! Exiting!")
os.Exit(1)
}
config := *configPtr
fmt.Println("Thrashing", config.Url)
p := message.NewPrinter(message.MatchLanguage("en"))
p.Println("Concurrency", config.Concurrency, "Num Requests", config.NumRequests)
if config.Profile {
startProfiler()
}
sem := make(chan bool, config.Concurrency)
ack := make(chan *Response, config.NumRequests)
tr := &http.Transport{
MaxIdleConns: config.Concurrency,
MaxIdleConnsPerHost: config.Concurrency,
}
client := http.Client{Transport: tr, Timeout: config.Timeout}
bar := pb.StartNew(config.NumRequests)
// Queue up the requests
for i := 0; i < config.NumRequests; i++ {
sem <- true
go func() {
defer func() { <-sem }()
fetchURL(ack, config, &client)
bar.Increment()
}()
}
summary := ResponseSummary{}
// Collect the responses
for i := 0; i < config.NumRequests; i++ {
response := <-ack
summary.addResponse(response)
if response.OK != true && config.PrintErrors {
fmt.Println(response.Error)
}
}
bar.Finish()
summary.print()
if config.Histogram {
summary.printHistogram()
}
}