Repository navigation
Expand file tree
/
Copy pathbench.go
More file actions
175 lines (150 loc) · 3.39 KB
/
Copy pathbench.go
File metadata and controls
175 lines (150 loc) · 3.39 KB
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
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net/http"
"net/url"
"sync"
"sync/atomic"
"time"
)
type bench struct {
requests uint
concurrency uint
timeout uint
host string
method string
params url.Values
data map[string]any
stats stats
client http.Client
}
type stats struct {
LaunchTime time.Time
Runtime time.Duration
RequestsPerSecond uint32
RequestsTotal uint32
RequestsSuccess uint32
RequestsFail uint32
RequestsTimeout uint32
DelayMin time.Duration
DelayAvg time.Duration
DelayMax time.Duration
}
type task struct {
url string
method string
data io.Reader
}
func NewBench() bench {
return bench{}
}
func (b *bench) ParseArgs() error {
numRequest := flag.Uint("n", 1000, "Number of requests")
concurrency := flag.Uint("c", 1, "Concurrency")
timeout := flag.Uint("t", 100, "Request timeout, ms")
host := flag.String("h", "", "Target URL address")
method := flag.String("m", "GET", "Request method")
params := flag.String("p", "", "Request params")
flag.Parse()
b.requests = *numRequest
b.concurrency = *concurrency
b.timeout = *timeout
switch *method {
case http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete:
b.method = *method
default:
return errors.New("unsupported HTTP method")
}
if u, err := url.ParseRequestURI(*host); err != nil {
return errors.New("invalid URL")
} else {
b.host = u.String()
}
if p, err := url.ParseQuery(*params); err == nil {
b.params = p
}
b.client = http.Client{
Timeout: time.Millisecond * time.Duration(b.timeout),
}
return nil
}
func (b *bench) Run() {
b.stats.LaunchTime = time.Now()
numRequests := b.requests / b.concurrency
var wg sync.WaitGroup
task := task{
url: fmt.Sprintf("%s?%s", b.host, b.params.Encode()),
}
if b.data != nil {
data, _ := json.Marshal(b.data)
task.data = bytes.NewBuffer(data)
}
for i := uint(0); i < b.concurrency; i++ {
wg.Add(1)
go func() {
b.LaunchTask(numRequests, task)
wg.Done()
}()
}
wg.Wait()
}
func (b *bench) LaunchTask(numRequest uint, t task) {
req, err := http.NewRequest(t.method, t.url, t.data)
if err != nil {
return
}
for i := uint(0); i < numRequest; i++ {
start := time.Now()
atomic.AddUint32(&b.stats.RequestsTotal, 1)
resp, err := b.client.Do(req)
if err != nil {
atomic.AddUint32(&b.stats.RequestsFail, 1)
if err == http.ErrHandlerTimeout {
atomic.AddUint32(&b.stats.RequestsTimeout, 1)
}
} else if resp.StatusCode == http.StatusOK {
atomic.AddUint32(&b.stats.RequestsSuccess, 1)
}
delay := time.Since(start)
if b.stats.DelayMin == 0 || delay < b.stats.DelayMin {
b.stats.DelayMin = delay
}
if delay > b.stats.DelayMax {
b.stats.DelayMax = delay
}
}
}
func (b *bench) PrintResult() {
b.stats.Runtime = time.Since(b.stats.LaunchTime)
rps := float64(b.stats.RequestsTotal) / b.stats.Runtime.Seconds()
b.stats.DelayAvg = (b.stats.DelayMax - b.stats.DelayMin) / 2
res := fmt.Sprintf(`
Runtime: %s
Concurrency: %d
Requests per second: %2.f
Total requests: %d
Success requests: %d
Fail requests: %d
Timeout requests: %d
Min delay: %s
Avg delay: %s
Max delay: %s
`,
b.stats.Runtime,
b.concurrency,
rps,
b.stats.RequestsTotal,
b.stats.RequestsSuccess,
b.stats.RequestsFail,
b.stats.RequestsTimeout,
b.stats.DelayMin,
b.stats.DelayAvg,
b.stats.DelayMax,
)
fmt.Println(res)
}