/
ui.go
378 lines (340 loc) · 10.5 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
371
372
373
374
375
376
377
378
package cmd
import (
"context"
"fmt"
"io"
"os"
"strings"
"sync"
"time"
"unicode/utf8"
"github.com/fatih/color"
"github.com/sirupsen/logrus"
"golang.org/x/term"
"gopkg.in/yaml.v3"
"github.com/gesemaya/k6/cmd/state"
"github.com/gesemaya/k6/lib"
"github.com/gesemaya/k6/lib/consts"
"github.com/gesemaya/k6/metrics/engine"
"github.com/gesemaya/k6/output"
"github.com/gesemaya/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
)
// getColor returns the requested color, or an uncolored object, depending on
// the value of noColor. The explicit EnableColor() and DisableColor() are
// needed because the library checks os.Stdout itself otherwise...
func getColor(noColor bool, attributes ...color.Attribute) *color.Color {
if noColor {
c := color.New()
c.DisableColor()
return c
}
c := color.New(attributes...)
c.EnableColor()
return c
}
func getBanner(noColor bool) string {
c := getColor(noColor, color.FgCyan)
return c.Sprint(consts.Banner())
}
func printBanner(gs *state.GlobalState) {
if gs.Flags.Quiet {
return // do not print banner when --quiet is enabled
}
banner := getBanner(gs.Flags.NoColor || !gs.Stdout.IsTTY)
_, err := fmt.Fprintf(gs.Stdout, "\n%s\n\n", banner)
if err != nil {
gs.Logger.Warnf("could not print k6 banner message to stdout: %s", err.Error())
}
}
func printBar(gs *state.GlobalState, bar *pb.ProgressBar) {
if gs.Flags.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 gs.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
printToStdout(gs, rendered.String()+end)
}
func modifyAndPrintBar(gs *state.GlobalState, bar *pb.ProgressBar, options ...pb.ProgressBarOption) {
bar.Modify(options...)
printBar(gs, bar)
}
// Print execution description for both cloud and local execution.
// TODO: Clean this up as part of #1499 or #1427
func printExecutionDescription(
gs *state.GlobalState, execution, filename, outputOverride string, conf Config,
et *lib.ExecutionTuple, execPlan []lib.ExecutionStep, outputs []output.Output,
) {
noColor := gs.Flags.NoColor || !gs.Stdout.IsTTY
valueColor := getColor(noColor, color.FgCyan)
buf := &strings.Builder{}
fmt.Fprintf(buf, " execution: %s\n", valueColor.Sprint(execution))
fmt.Fprintf(buf, " script: %s\n", valueColor.Sprint(filename))
var outputDescriptions []string
switch {
case outputOverride != "":
outputDescriptions = []string{outputOverride}
default:
for _, out := range outputs {
desc := out.Description()
if desc == engine.IngesterDescription {
if len(outputs) != 1 {
continue
}
desc = "-"
}
outputDescriptions = append(outputDescriptions, desc)
}
}
fmt.Fprintf(buf, " output: %s\n", valueColor.Sprint(strings.Join(outputDescriptions, ", ")))
fmt.Fprintf(buf, "\n")
maxDuration, _ := lib.GetEndOffset(execPlan)
executorConfigs := conf.Scenarios.GetSortedConfigs()
scenarioDesc := "1 scenario"
if len(executorConfigs) > 1 {
scenarioDesc = fmt.Sprintf("%d scenarios", len(executorConfigs))
}
fmt.Fprintf(buf, " scenarios: %s\n", 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 {
fmt.Fprintf(buf, " * %s: %s\n",
ec.GetName(), ec.GetDescription(et))
}
fmt.Fprintf(buf, "\n")
if gs.Flags.Quiet {
gs.Logger.Debug(buf.String())
} else {
printToStdout(gs, buf.String())
}
}
//nolint:funlen
func renderMultipleBars(
nocolor, 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,gocognit
func showProgress(ctx context.Context, gs *state.GlobalState, pbs []*pb.ProgressBar, logger logrus.FieldLogger) {
if gs.Flags.Quiet {
return
}
var errTermGetSize bool
termWidth := defaultTermWidth
if gs.Stdout.IsTTY {
tw, _, err := term.GetSize(int(gs.Stdout.RawOut.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()
_, _ = gs.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(
gs.Flags.NoColor, gs.Stdout.IsTTY, goBack, maxLeft, termWidth, widthDelta, pbs,
)
widthDelta = termWidth - longestLine - termPadding
progressBarsLastRenderLock.Lock()
progressBarsLastRender = []byte(barText)
progressBarsLastRenderLock.Unlock()
}
// Otherwise fallback to fixed compact progress bars
if !gs.Stdout.IsTTY {
widthDelta = -pb.DefaultWidth
renderProgressBars = func(goBack bool) {
barText, _ := renderMultipleBars(gs.Flags.NoColor, gs.Stdout.IsTTY, goBack, maxLeft, termWidth, widthDelta, pbs)
progressBarsLastRenderLock.Lock()
progressBarsLastRender = []byte(barText)
progressBarsLastRenderLock.Unlock()
}
}
// TODO: make configurable?
updateFreq := 1 * time.Second
var stdoutFD int
if gs.Stdout.IsTTY {
stdoutFD = int(gs.Stdout.RawOut.Fd())
updateFreq = 100 * time.Millisecond
gs.OutMutex.Lock()
gs.Stdout.PersistentText = printProgressBars
gs.Stderr.PersistentText = printProgressBars
gs.OutMutex.Unlock()
defer func() {
gs.OutMutex.Lock()
gs.Stdout.PersistentText = nil
gs.Stderr.PersistentText = nil
gs.OutMutex.Unlock()
}()
}
var winch chan os.Signal
if sig := getWinchSignal(); sig != nil {
winch = make(chan os.Signal, 10)
gs.SignalNotify(winch, sig)
defer gs.SignalStop(winch)
}
ticker := time.NewTicker(updateFreq)
ctxDone := ctx.Done()
for {
select {
case <-ctxDone:
renderProgressBars(false)
gs.OutMutex.Lock()
printProgressBars()
gs.OutMutex.Unlock()
return
case <-winch:
if gs.Stdout.IsTTY && !errTermGetSize {
// More responsive progress bar resizing on platforms with SIGWINCH (*nix)
tw, _, err := term.GetSize(stdoutFD)
if tw > 0 && err == nil {
termWidth = tw
}
}
case <-ticker.C:
// Default ticker-based progress bar resizing
if gs.Stdout.IsTTY && !errTermGetSize && winch == nil {
tw, _, err := term.GetSize(stdoutFD)
if tw > 0 && err == nil {
termWidth = tw
}
}
}
renderProgressBars(true)
gs.OutMutex.Lock()
printProgressBars()
gs.OutMutex.Unlock()
}
}
func yamlPrint(w io.Writer, v interface{}) error {
data, err := yaml.Marshal(v)
if err != nil {
return fmt.Errorf("could not marshal YAML: %w", err)
}
_, err = fmt.Fprint(w, string(data))
if err != nil {
return fmt.Errorf("could flush the data to the output: %w", err)
}
return nil
}