forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.go
370 lines (334 loc) · 10.3 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2019 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package cmd
import (
"bytes"
"context"
"fmt"
"io"
"os"
"os/signal"
"strings"
"sync"
"time"
"unicode/utf8"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/terminal"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/output"
"github.com/loadimpact/k6/ui"
"github.com/loadimpact/k6/ui/pb"
)
const (
// Max length of left-side progress bar text before trimming is forced
maxLeftLength = 30
// Amount of padding in chars between rendered progress
// bar text and right-side terminal window edge.
termPadding = 1
defaultTermWidth = 80
)
// A writer that syncs writes with a mutex and, if the output is a TTY, clears before newlines.
type consoleWriter struct {
Writer io.Writer
IsTTY bool
Mutex *sync.Mutex
// Used for flicker-free persistent objects like the progressbars
PersistentText func()
}
func (w *consoleWriter) Write(p []byte) (n int, err error) {
origLen := len(p)
if w.IsTTY {
// Add a TTY code to erase till the end of line with each new line
// TODO: check how cross-platform this is...
p = bytes.Replace(p, []byte{'\n'}, []byte{'\x1b', '[', '0', 'K', '\n'}, -1)
}
w.Mutex.Lock()
n, err = w.Writer.Write(p)
if w.PersistentText != nil {
w.PersistentText()
}
w.Mutex.Unlock()
if err != nil && n < origLen {
return n, err
}
return origLen, err
}
func printBar(bar *pb.ProgressBar) {
if quiet {
return
}
end := "\n"
// TODO: refactor widthDelta away? make the progressbar rendering a bit more
// stateless... basically first render the left and right parts, so we know
// how long the longest line is, and how much space we have for the progress
widthDelta := -defaultTermWidth
if stdout.IsTTY {
// If we're in a TTY, instead of printing the bar and going to the next
// line, erase everything till the end of the line and return to the
// start, so that the next print will overwrite the same line.
//
// TODO: check for cross platform support
end = "\x1b[0K\r"
widthDelta = 0
}
rendered := bar.Render(0, widthDelta)
// Only output the left and middle part of the progress bar
fprintf(stdout, "%s%s", rendered.String(), end)
}
func modifyAndPrintBar(bar *pb.ProgressBar, options ...pb.ProgressBarOption) {
bar.Modify(options...)
printBar(bar)
}
// Print execution description for both cloud and local execution.
// TODO: Clean this up as part of #1499 or #1427
func printExecutionDescription(
execution, filename, outputOverride string, conf Config, et *lib.ExecutionTuple,
execPlan []lib.ExecutionStep, outputs []output.Output,
) {
fprintf(stdout, " execution: %s\n", ui.ValueColor.Sprint(execution))
fprintf(stdout, " script: %s\n", ui.ValueColor.Sprint(filename))
var outputDescriptions []string
switch {
case outputOverride != "":
outputDescriptions = []string{outputOverride}
case len(outputs) == 0:
outputDescriptions = []string{"-"}
default:
for _, out := range outputs {
outputDescriptions = append(outputDescriptions, out.Description())
}
}
fprintf(stdout, " output: %s\n", ui.ValueColor.Sprint(strings.Join(outputDescriptions, ", ")))
fprintf(stdout, "\n")
maxDuration, _ := lib.GetEndOffset(execPlan)
executorConfigs := conf.Scenarios.GetSortedConfigs()
scenarioDesc := "1 scenario"
if len(executorConfigs) > 1 {
scenarioDesc = fmt.Sprintf("%d scenarios", len(executorConfigs))
}
fprintf(stdout, " scenarios: %s\n", ui.ValueColor.Sprintf(
"(%.2f%%) %s, %d max VUs, %s max duration (incl. graceful stop):",
conf.ExecutionSegment.FloatLength()*100, scenarioDesc,
lib.GetMaxPossibleVUs(execPlan), maxDuration.Round(100*time.Millisecond)),
)
for _, ec := range executorConfigs {
fprintf(stdout, " * %s: %s\n",
ec.GetName(), ec.GetDescription(et))
}
fprintf(stdout, "\n")
}
//nolint: funlen
func renderMultipleBars(
isTTY, goBack bool, maxLeft, termWidth, widthDelta int, pbs []*pb.ProgressBar,
) (string, int) {
lineEnd := "\n"
if isTTY {
//TODO: check for cross platform support
lineEnd = "\x1b[K\n" // erase till end of line
}
var (
// Amount of times line lengths exceed termWidth.
// Needed to factor into the amount of lines to jump
// back with [A and avoid scrollback issues.
lineBreaks int
longestLine int
// Maximum length of each right side column except last,
// used to calculate the padding between columns.
maxRColumnLen = make([]int, 2)
pbsCount = len(pbs)
rendered = make([]pb.ProgressBarRender, pbsCount)
result = make([]string, pbsCount+2)
)
result[0] = lineEnd // start with an empty line
// First pass to render all progressbars and get the maximum
// lengths of right-side columns.
for i, pb := range pbs {
rend := pb.Render(maxLeft, widthDelta)
for i := range rend.Right {
// Skip last column, since there's nothing to align after it (yet?).
if i == len(rend.Right)-1 {
break
}
if len(rend.Right[i]) > maxRColumnLen[i] {
maxRColumnLen[i] = len(rend.Right[i])
}
}
rendered[i] = rend
}
// Second pass to render final output, applying padding where needed
for i := range rendered {
rend := rendered[i]
if rend.Hijack != "" {
result[i+1] = rend.Hijack + lineEnd
runeCount := utf8.RuneCountInString(rend.Hijack)
lineBreaks += (runeCount - termPadding) / termWidth
continue
}
var leftText, rightText string
leftPadFmt := fmt.Sprintf("%%-%ds", maxLeft)
leftText = fmt.Sprintf(leftPadFmt, rend.Left)
for i := range rend.Right {
rpad := 0
if len(maxRColumnLen) > i {
rpad = maxRColumnLen[i]
}
rightPadFmt := fmt.Sprintf(" %%-%ds", rpad+1)
rightText += fmt.Sprintf(rightPadFmt, rend.Right[i])
}
// Get visible line length, without ANSI escape sequences (color)
status := fmt.Sprintf(" %s ", rend.Status())
line := leftText + status + rend.Progress() + rightText
lineRuneCount := utf8.RuneCountInString(line)
if lineRuneCount > longestLine {
longestLine = lineRuneCount
}
lineBreaks += (lineRuneCount - termPadding) / termWidth
if !noColor {
rend.Color = true
status = fmt.Sprintf(" %s ", rend.Status())
line = fmt.Sprintf(leftPadFmt+"%s%s%s",
rend.Left, status, rend.Progress(), rightText)
}
result[i+1] = line + lineEnd
}
if isTTY && goBack {
// Clear screen and go back to the beginning
//TODO: check for cross platform support
result[pbsCount+1] = fmt.Sprintf("\r\x1b[J\x1b[%dA", pbsCount+lineBreaks+1)
} else {
result[pbsCount+1] = ""
}
return strings.Join(result, ""), longestLine
}
//TODO: show other information here?
//TODO: add a no-progress option that will disable these
//TODO: don't use global variables...
// nolint:funlen
func showProgress(
ctx context.Context, conf Config,
pbs []*pb.ProgressBar, logger *logrus.Logger,
) {
if quiet {
return
}
var errTermGetSize bool
termWidth := defaultTermWidth
if stdoutTTY {
tw, _, err := terminal.GetSize(int(os.Stdout.Fd()))
if !(tw > 0) || err != nil {
errTermGetSize = true
logger.WithError(err).Warn("error getting terminal size")
} else {
termWidth = tw
}
}
// Get the longest left side string length, to align progress bars
// horizontally and trim excess text.
var leftLen int64
for _, pb := range pbs {
l := pb.Left()
leftLen = lib.Max(int64(len(l)), leftLen)
}
// Limit to maximum left text length
maxLeft := int(lib.Min(leftLen, maxLeftLength))
var progressBarsLastRenderLock sync.Mutex
var progressBarsLastRender []byte
printProgressBars := func() {
progressBarsLastRenderLock.Lock()
_, _ = stdout.Writer.Write(progressBarsLastRender)
progressBarsLastRenderLock.Unlock()
}
var widthDelta int
// Default to responsive progress bars when in an interactive terminal
renderProgressBars := func(goBack bool) {
barText, longestLine := renderMultipleBars(stdoutTTY, goBack, maxLeft, termWidth, widthDelta, pbs)
widthDelta = termWidth - longestLine - termPadding
progressBarsLastRenderLock.Lock()
progressBarsLastRender = []byte(barText)
progressBarsLastRenderLock.Unlock()
}
// Otherwise fallback to fixed compact progress bars
if !stdoutTTY {
widthDelta = -pb.DefaultWidth
renderProgressBars = func(goBack bool) {
barText, _ := renderMultipleBars(stdoutTTY, goBack, maxLeft, termWidth, widthDelta, pbs)
progressBarsLastRenderLock.Lock()
progressBarsLastRender = []byte(barText)
progressBarsLastRenderLock.Unlock()
}
}
//TODO: make configurable?
updateFreq := 1 * time.Second
//TODO: remove !noColor after we fix how we handle colors (see the related
//description in the TODO message in cmd/root.go)
if stdoutTTY && !noColor {
updateFreq = 100 * time.Millisecond
outMutex.Lock()
stdout.PersistentText = printProgressBars
stderr.PersistentText = printProgressBars
outMutex.Unlock()
defer func() {
outMutex.Lock()
stdout.PersistentText = nil
stderr.PersistentText = nil
outMutex.Unlock()
}()
}
var (
fd = int(os.Stdout.Fd())
ticker = time.NewTicker(updateFreq)
)
var winch chan os.Signal
if sig := getWinchSignal(); sig != nil {
winch = make(chan os.Signal, 1)
signal.Notify(winch, sig)
}
ctxDone := ctx.Done()
for {
select {
case <-ctxDone:
renderProgressBars(false)
outMutex.Lock()
printProgressBars()
outMutex.Unlock()
return
case <-winch:
if stdoutTTY && !errTermGetSize {
// More responsive progress bar resizing on platforms with SIGWINCH (*nix)
tw, _, err := terminal.GetSize(fd)
if tw > 0 && err == nil {
termWidth = tw
}
}
case <-ticker.C:
// Default ticker-based progress bar resizing
if stdoutTTY && !errTermGetSize && winch == nil {
tw, _, err := terminal.GetSize(fd)
if tw > 0 && err == nil {
termWidth = tw
}
}
}
renderProgressBars(true)
outMutex.Lock()
printProgressBars()
outMutex.Unlock()
}
}