-
Notifications
You must be signed in to change notification settings - Fork 8
/
pit.go
195 lines (162 loc) · 3.38 KB
/
pit.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
package pit
import (
"errors"
"strings"
"sync"
"sync/atomic"
"time"
tea "github.com/charmbracelet/bubbletea"
)
// Version of current httpit
const Version = "0.4.0"
const (
defaultConnections = 128
defaultDuration = time.Second * 10
defaultTimeout = time.Second * 3
defaultMaxRedirects = 30
)
// Pit denotes httpit application
type Pit struct {
c *Config
client
limiter
wg sync.WaitGroup
mut sync.Mutex
startTime time.Time
roundReqs int64
done bool
doneChan chan struct{}
*tui
}
// New create a Pit instance with specific Config
func New(c Config) *Pit {
p := &Pit{
c: &c,
limiter: nopeLimiter(false),
doneChan: make(chan struct{}),
}
if p.c.Connections <= 0 {
p.c.Connections = defaultConnections
}
if p.c.Duration <= 0 {
p.c.Duration = defaultDuration
}
if p.c.Timeout <= 0 {
p.c.Timeout = defaultTimeout
}
if p.c.MaxRedirects <= 0 {
p.c.MaxRedirects = defaultMaxRedirects
}
p.tui = newTui()
p.tui.count = p.c.Count
p.tui.duration = p.c.Duration
p.tui.connections = p.c.Connections
p.tui.throughput = &p.c.throughput
p.initCmd = p.run
return p
}
// Run starts benchmarking
func (p *Pit) Run() (err error) {
if err = p.init(); err != nil {
return
}
if p.c.Debug {
return p.doOnce()
}
return p.tui.start()
}
func (p *Pit) init() (err error) {
if p.c.Url == "" {
return errors.New("missing url")
}
// :3000 => http://127.0.0.1
// example.com => http://example.com
p.c.Url = addMissingSchemaAndHost(p.c.Url)
p.tui.url = p.c.Url
if p.c.Qps > 0 {
p.limiter = newTokenLimiter(p.c.Qps)
}
if p.client == nil {
p.client, err = newFasthttpClient(p.c)
}
return
}
func addMissingSchemaAndHost(url string) string {
if !strings.HasPrefix(url, "://") && strings.HasPrefix(url, ":") {
// :3000 => http://localhost:3000
return "http://localhost" + url
}
if strings.Index(url, "://") == -1 && len(url) >= 2 {
if url[0] == '/' && url[1] != '/' {
// /foo => http://localhost/foo
return "http://localhost" + url
}
if url[0] != '/' && url[1] != '/' {
// example.com => http://example.com
return "http://" + url
}
}
return url
}
func (p *Pit) run() tea.Msg {
p.startTime = time.Now()
n := p.c.Connections
p.wg.Add(n)
for i := 0; i < n; i++ {
go p.worker()
}
// wait for all workers stop
p.wg.Wait()
return done
}
func (p *Pit) worker() {
for {
select {
case <-p.doneChan:
p.wg.Done()
return
default:
if p.limiter.allow() {
p.statistic(p.do())
}
}
}
}
const interval = time.Millisecond * 10
func (p *Pit) statistic(code int, latency time.Duration, err error) {
p.mut.Lock()
defer p.mut.Unlock()
if p.done {
return
}
if err != nil {
p.appendError(err)
} else {
p.roundReqs++
atomic.AddInt64(&p.reqs, 1)
p.appendCode(code)
p.appendLatency(latency)
}
elapsed := time.Since(p.startTime)
// reached count
if p.c.Count > 0 && atomic.LoadInt64(&p.reqs) == int64(p.c.Count) {
p.appendRps(float64(p.roundReqs) / elapsed.Seconds())
p.done = true
// notify workers to stop
close(p.doneChan)
return
}
// one round is over
if elapsed >= interval {
p.appendRps(float64(p.roundReqs) / elapsed.Seconds())
atomic.AddInt64(&p.elapsed, int64(elapsed))
p.startTime = time.Now()
p.roundReqs = 0
}
// exceed duration
if p.c.Count <= 0 && atomic.LoadInt64(&p.elapsed) >= int64(p.c.Duration) {
p.done = true
// notify workers to stop
close(p.doneChan)
}
}