forked from ikruglov/slapper
/
ui.go
240 lines (204 loc) · 6.26 KB
/
ui.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
package slapperx
import (
"bytes"
"fmt"
term "github.com/nsf/termbox-go"
terminal "golang.org/x/term"
"log"
"strings"
"time"
)
const (
statsLines = 4
reservedWidthSpace = 40
reservedHeightSpace = 3
)
// colors for histogram bars
var colors = []string{
"\033[38;5;46m", "\033[38;5;47m", "\033[38;5;48m", "\033[38;5;49m", // green
"\033[38;5;149m", "\033[38;5;148m", "\033[38;5;179m", "\033[38;5;176m", // yellow
"\033[38;5;169m", "\033[38;5;168m", "\033[38;5;197m", "\033[38;5;196m", // red
}
// var partChar = []string{" ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█"}
type UI struct {
start time.Time
terminalWidth int
terminalHeight int
// plotting vars
plotWidth int
plotHeight int
lbc *logBucketCalculator
}
// InitTerminal initializes the terminal and sets the UI dimensions
func InitTerminal(minY time.Duration, maxY time.Duration) *UI {
if !terminal.IsTerminal(0) {
panic("Not a terminal")
}
ui := UI{
start: time.Now(),
}
ui.setWindowSize()
ui.lbc = newLogBucketCalculator(minY, maxY, ui.plotHeight)
return &ui
}
func (ui *UI) close() {
//term.Close()
}
func (ui *UI) setWindowSize() {
var err error
ui.terminalWidth, ui.terminalHeight, err = terminal.GetSize(0)
if err != nil {
panic(err)
}
ui.plotWidth = ui.terminalWidth
ui.plotHeight = ui.terminalHeight - statsLines
if ui.plotWidth < reservedWidthSpace {
log.Fatal("not enough screen width, min 40 characters required")
}
if ui.plotHeight <= reservedHeightSpace {
log.Fatal("not enough screen height, min 3 lines required")
}
}
func (ui *UI) listParameters() {
//fmt.Println("connection timeout:", trgt.client.Timeout)
/*
fmt.Printf("Requests: %d\n", stats.requestsSent.Load())
fmt.Printf("Errors: %d\n", stats.errors.Load())
fmt.Printf("Min response time: %s\n", stats.minResponseTime.Load())
fmt.Printf("Max response time: %s\n", stats.maxResponseTime.Load())
fmt.Printf("Average response time: %s\n", stats.avgResponseTime.Load())
*/
}
// keyPressListener listens for key presses and sends rate changes to rateChanger channel
func (ui *UI) keyPressListener(rateChanger chan<- int64) {
// start keyPress listener
err := term.Init()
term.HideCursor()
if err != nil {
log.Fatal(err)
}
defer term.Close()
for {
switch ev := term.PollEvent(); ev.Type {
case term.EventKey:
if ui.handleKeyPress(ev, rateChanger) {
return
}
case term.EventError:
log.Fatal(ev.Err)
}
}
}
// handleKeyPress processes key press events and updates the rateChanger channel
func (ui *UI) handleKeyPress(ev term.Event, rateChanger chan<- int64) bool {
switch ev.Key {
case term.KeyCtrlC:
return true
default:
switch ev.Ch {
case 'q':
return true
case 'r':
resetStats()
case 'k': // up
rateChanger <- rateIncreaseStep
case 'j':
rateChanger <- rateDecreaseStep
}
}
return false
}
// printHistogramHeader prints the header of the histogram with sent, in-flight, and responses information
func (ui *UI) printHistogramHeader(sb *strings.Builder, currentRate counter, currentSetRate counter) {
sent := stats.requestsSent.Load()
recv := stats.responsesReceived.Load()
_, _ = fmt.Fprintf(sb, "time: %4ds ", int(time.Since(ui.start).Seconds()))
_, _ = fmt.Fprintf(sb, "sent: %-5d ", sent)
//_, _ = fmt.Fprintf(sb, "connections: %-5d ", trgt.client.CurrentConnections)
_, _ = fmt.Fprintf(sb, "in-flight: %-4d ", sent-recv)
_, _ = fmt.Fprintf(sb, "\033[96mrate: %4d/%d RPS\033[0m ", currentRate.Load(), currentSetRate.Load())
_, _ = fmt.Fprint(sb, "\r\nresponses: ")
if stats.responses.ErrorNoSuchHost > 0 {
_, _ = fmt.Fprintf(sb, "\033[31m[No such host]: %-6d\033[0m ", stats.responses.ErrorNoSuchHost)
}
if stats.responses.ErrorConnRefused > 0 {
_, _ = fmt.Fprintf(sb, "\033[31m[Conn refused]: %-6d\033[0m ", stats.responses.ErrorConnRefused)
}
if stats.responses.ErrorEof > 0 {
_, _ = fmt.Fprintf(sb, "\033[31m[EOF]: %-6d\033[0m ", stats.responses.ErrorEof)
}
if stats.responses.ErrorTimeout > 0 {
_, _ = fmt.Fprintf(sb, "\033[31m[Timeout]: %-6d\033[0m ", stats.responses.ErrorTimeout)
}
for status, counter := range stats.responses.status {
if c := counter.Load(); c > 0 {
if status >= 200 && status < 300 {
_, _ = fmt.Fprintf(sb, "\033[32m[%d]: %-6d\033[0m ", status, c)
} else {
_, _ = fmt.Fprintf(sb, "\033[31m[%d]: %-6d\033[0m ", status, c)
}
}
}
}
// drawHistogram draws the histogram of response times
func (ui *UI) drawHistogram(currentRate counter, currentSetRate counter) {
var sb strings.Builder
sb.Grow(int(ui.terminalWidth*ui.terminalHeight*2 + ui.terminalHeight*(5*5+12*2))) // just a guess
colorMultiplier := float64(len(colors)) / float64(ui.lbc.buckets)
barWidth := int(ui.plotWidth) - reservedWidthSpace // reserve some space on right and left
tOk, tBad, max := stats.timings.prepareHistogramData()
_, _ = fmt.Fprint(&sb, "\033[H") // clean screen
ui.printHistogramHeader(&sb, currentRate, currentSetRate)
_, _ = fmt.Fprint(&sb, "\r\n\r\n")
width := float64(barWidth) / float64(max)
for bkt := 0; bkt < ui.lbc.buckets; bkt++ {
label := ui.lbc.createLabel(bkt)
widthOk := int(float64(tOk[bkt]) * width)
widthBad := int(float64(tBad[bkt]) * width)
widthLeft := barWidth - widthOk - widthBad
_, _ = fmt.Fprintf(&sb, "%11s ms: [%s%6d%s/%s%6d%s] %s%s%s%s%s \r\n",
label,
"\033[32m",
tOk[bkt],
"\033[0m",
"\033[31m",
tBad[bkt],
"\033[0m",
colors[int(float64(bkt)*colorMultiplier)],
bytes.Repeat([]byte("E"), widthBad),
bytes.Repeat([]byte("█"), widthOk),
bytes.Repeat([]byte(" "), widthLeft),
"\033[0m")
} // end for
_, _ = fmt.Print(sb.String())
}
// clearScreen clears the terminal screen
func (ui *UI) clearScreen() {
//_, _ = fmt.Print("\033[H\033[2J")
fmt.Print("\033[H")
for i := 0; i < ui.terminalHeight; i++ {
fmt.Println(string(bytes.Repeat([]byte(" "), int(ui.terminalWidth)-1)))
}
}
// reporter periodically updates and redraws the histogram
func (ui *UI) reporter(quit <-chan struct{}) {
ui.clearScreen()
var currentRate counter
go func() {
var lastSent int64
for range time.Tick(time.Second) {
curr := stats.requestsSent.Load()
currentRate.Store(curr - lastSent)
lastSent = curr
}
}()
ticker := time.Tick(screenRefreshInterval)
for {
select {
case <-ticker:
ui.drawHistogram(currentRate, stats.currentSetRate)
case <-quit:
return
}
}
}