This repository has been archived by the owner on Nov 25, 2021. It is now read-only.
/
gofigure.go
230 lines (189 loc) · 4.59 KB
/
gofigure.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
// (c) 2011 Alexander Solovyov
// under terms of ISC license
package main
import (
"bufio"
"errors"
"fmt"
goopt "github.com/droundy/goopt"
"net"
"net/http"
"net/url"
"runtime"
"sort"
"strings"
"time"
)
var Author = "Alexander Solovyov"
var Version = "0.2"
var Summary = "gofigure [OPTS] URL\n"
var reqs = goopt.Int([]string{"-n", "--requests"}, 1,
"number of requests to make")
var concurrency = goopt.Int([]string{"-c", "--concurrency"}, 1,
"concurrency level")
var timeout = goopt.Int([]string{"-t", "--timeout"}, 1000,
"timeout of each request in milliseconds")
var cpus = goopt.Int([]string{"-p", "--cpus"}, 0,
"how many processes to run (0 - default)")
type someError struct {
what string
str string
}
func (e *someError) Error() string {
return fmt.Sprintf(e.what, e.str)
}
type result struct {
time time.Duration
err error
}
type DurationArray []time.Duration
func (p DurationArray) Len() int {
return len(p)
}
func (p DurationArray) Less(i, j int) bool {
return p[i].Nanoseconds() < p[j].Nanoseconds()
}
func (p DurationArray) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func main() {
goopt.Author = Author
goopt.Version = Version
goopt.Summary = Summary
goopt.Parse(nil)
if len(goopt.Args) == 0 {
println(goopt.Usage())
return
}
if *concurrency > *reqs {
fmt.Printf("You can't have concurrency higher than number of requests\n")
return
}
url, ip, err := getURL(goopt.Args[0])
if err != nil {
fmt.Printf("url is invalid: %s\n", err)
return
}
runtime.GOMAXPROCS(*cpus)
fmt.Printf("Statistics for requests to %s\n", goopt.Args[0])
results, total := start(url, ip, *reqs, *concurrency)
printStats(results, total)
}
func start(url *url.URL, addr string, requests int, concurrency int) ([]result, time.Duration) {
results := make([]result, requests)
queue := make(chan int, requests)
out := make(chan result, concurrency)
const fmtCompleted = "\rCompleted %d from %d requests"
for i := 0; i < requests; i++ {
queue <- i
}
now := time.Now()
for i := 0; i < concurrency; i++ {
go sender(url, addr, queue, out)
}
for i := 0; i < requests; i++ {
results[i] = <-out
if i > 0 && i%100 == 0 {
fmt.Printf(fmtCompleted, i, requests)
}
}
// erase 'Completed ...' line
fmt.Printf("\r%*s", len(fmtCompleted)+10, " ")
return results, time.Now().Sub(now)
}
func printStats(results []result, workTime time.Duration) {
times := make(DurationArray, 0)
total := time.Duration(0)
for _, r := range results {
if r.err == nil {
times = append(times, r.time)
total += r.time
}
}
sort.Sort(times)
average := time.Duration(0)
median := time.Duration(0)
if len(times) > 0 {
average = time.Duration(total.Nanoseconds() / int64(len(times)))
median = times[len(times) / 2]
}
fmt.Printf(`
Total requests performed: %d
Total failures: %d
Time taken for tests: %s
Average request takes: %s
Median request time: %s
Average time between responses: %s
Average requests per second: %.3f
`,
*reqs,
*reqs-len(times),
workTime,
average,
median,
time.Duration(workTime.Nanoseconds() / int64(*reqs)),
(float64(*reqs) / workTime.Seconds()))
}
func hasPort(s string) bool {
return strings.LastIndex(s, ":") > strings.LastIndex(s, "]")
}
func getURL(rawurl string) (*url.URL, string, error) {
URL, err := url.Parse(rawurl)
if err != nil {
return nil, "", err
}
if URL.Scheme != "http" && URL.Scheme != "https" {
return nil, "", &someError{"unsupported protocol scheme: %s", URL.Scheme}
}
bits := strings.Split(URL.Host, ":")
host := bits[0]
var port string
if len(bits) > 1 {
port = bits[1]
} else {
port = "80"
}
ip, err := net.ResolveIPAddr("ip", host)
if err != nil {
return nil, "", err
}
return URL, ip.String() + ":" + port, nil
}
func sender(url *url.URL, addr string, queue chan int, out chan result) {
for _ = range queue {
out <- send(url, addr)
}
}
type respErr struct {
resp *http.Response
err error
}
func send(url *url.URL, addr string) result {
var req http.Request
req.URL = url
now := time.Now()
conn, err := net.Dial("tcp", addr)
if err != nil {
return result{0, err}
}
defer conn.Close()
err = req.Write(conn)
if err != nil {
return result{0, err}
}
ch := make(chan respErr, 1)
go func() {
reader := bufio.NewReader(conn)
response, err := http.ReadResponse(reader, &req)
ch <- respErr{response, err}
}()
var res result
select {
case <-time.After(time.Duration(*timeout * 1e6)):
res = result{time.Now().Sub(now), errors.New("Timeout!")}
case rerr := <-ch:
res = result{time.Now().Sub(now), rerr.err}
rerr.resp.Body.Close()
}
return res
}