forked from minio/mc
/
speedtest-spinner.go
352 lines (324 loc) · 9.35 KB
/
speedtest-spinner.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
// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// 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 (
"fmt"
"sort"
"strings"
"time"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
humanize "github.com/dustin/go-humanize"
"github.com/olekukonko/tablewriter"
"github.com/trinet2005/oss-admin-go"
)
var whiteStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#ffffff"))
type speedTestUI struct {
spinner spinner.Model
quitting bool
result PerfTestResult
}
// PerfTestType - The type of performance test (net/drive/object)
type PerfTestType byte
// Constants for performance test type
const (
NetPerfTest PerfTestType = 1 << iota
DrivePerfTest
ObjectPerfTest
SiteReplicationPerfTest
ClientPerfTest
)
// Name - returns name of the performance test
func (p PerfTestType) Name() string {
switch p {
case NetPerfTest:
return "NetPerf"
case DrivePerfTest:
return "DrivePerf"
case ObjectPerfTest:
return "ObjectPerf"
case SiteReplicationPerfTest:
return "SiteReplication"
case ClientPerfTest:
return "Client"
}
return "<unknown>"
}
// PerfTestResult - stores the result of a performance test
type PerfTestResult struct {
Type PerfTestType `json:"type"`
ObjectResult *madmin.SpeedTestResult `json:"object,omitempty"`
NetResult *madmin.NetperfResult `json:"network,omitempty"`
SiteReplicationResult *madmin.SiteNetPerfResult `json:"siteReplication,omitempty"`
ClientResult *madmin.ClientPerfResult `json:"client,omitempty"`
DriveResult []madmin.DriveSpeedTestResult `json:"drive,omitempty"`
Err string `json:"err,omitempty"`
Final bool `json:"final,omitempty"`
}
func initSpeedTestUI() *speedTestUI {
s := spinner.New()
s.Spinner = spinner.Points
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
return &speedTestUI{
spinner: s,
}
}
func (m *speedTestUI) Init() tea.Cmd {
return m.spinner.Tick
}
func (m *speedTestUI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "esc", "ctrl+c":
m.quitting = true
return m, tea.Quit
default:
return m, nil
}
case PerfTestResult:
m.result = msg
if msg.Final {
m.quitting = true
return m, tea.Quit
}
return m, nil
default:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
}
func (m *speedTestUI) View() string {
// Quit when there is an error
if m.result.Err != "" {
return fmt.Sprintf("\n%s: %s (Err: %s)\n", m.result.Type.Name(), crossTickCell, m.result.Err)
}
var s strings.Builder
// Set table header
table := tablewriter.NewWriter(&s)
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t") // pad with tabs
table.SetNoWhiteSpace(true)
ores := m.result.ObjectResult
nres := m.result.NetResult
sres := m.result.SiteReplicationResult
dres := m.result.DriveResult
cres := m.result.ClientResult
trailerIfGreaterThan := func(in string, max int) string {
if len(in) < max {
return in
}
return in[:max] + "..."
}
// Print the spinner
if !m.quitting {
s.WriteString(fmt.Sprintf("\n%s: %s\n\n", m.result.Type.Name(), m.spinner.View()))
} else {
s.WriteString(fmt.Sprintf("\n%s: %s\n\n", m.result.Type.Name(), m.spinner.Style.Render(tickCell)))
}
if ores != nil {
table.SetHeader([]string{"", "Throughput", "IOPS"})
data := make([][]string, 2)
if ores.Version == "" {
data[0] = []string{
"PUT",
whiteStyle.Render("-- KiB/sec"),
whiteStyle.Render("-- objs/sec"),
}
data[1] = []string{
"GET",
whiteStyle.Render("-- KiB/sec"),
whiteStyle.Render("-- objs/sec"),
}
} else {
data[0] = []string{
"PUT",
whiteStyle.Render(humanize.IBytes(ores.PUTStats.ThroughputPerSec) + "/s"),
whiteStyle.Render(humanize.Comma(int64(ores.PUTStats.ObjectsPerSec)) + " objs/s"),
}
data[1] = []string{
"GET",
whiteStyle.Render(humanize.IBytes(ores.GETStats.ThroughputPerSec) + "/s"),
whiteStyle.Render(humanize.Comma(int64(ores.GETStats.ObjectsPerSec)) + " objs/s"),
}
}
table.AppendBulk(data)
table.Render()
if m.quitting {
s.WriteString("\n" + objectTestShortResult(ores))
if globalPerfTestVerbose {
s.WriteString("\n\n")
s.WriteString(objectTestVerboseResult(ores))
}
s.WriteString("\n")
}
} else if nres != nil {
table.SetHeader([]string{"Node", "RX", "TX", ""})
data := make([][]string, 0, len(nres.NodeResults))
if len(nres.NodeResults) == 0 {
data = append(data, []string{
"...",
whiteStyle.Render("-- MiB/s"),
whiteStyle.Render("-- MiB/s"),
"",
})
} else {
for _, nodeResult := range nres.NodeResults {
if nodeResult.Error != "" {
data = append(data, []string{
trailerIfGreaterThan(nodeResult.Endpoint, 64),
crossTickCell,
crossTickCell,
"Err: " + nodeResult.Error,
})
} else {
data = append(data, []string{
trailerIfGreaterThan(nodeResult.Endpoint, 64),
whiteStyle.Render(humanize.IBytes(uint64(nodeResult.RX))) + "/s",
whiteStyle.Render(humanize.IBytes(uint64(nodeResult.TX))) + "/s",
"",
})
}
}
}
sort.Slice(data, func(i, j int) bool {
return data[i][0] < data[j][0]
})
table.AppendBulk(data)
table.Render()
} else if sres != nil {
table.SetHeader([]string{"Endpoint", "RX", "TX", ""})
data := make([][]string, 0, len(sres.NodeResults))
if len(sres.NodeResults) == 0 {
data = append(data, []string{
"...",
whiteStyle.Render("-- MiB"),
whiteStyle.Render("-- MiB"),
"",
})
} else {
for _, nodeResult := range sres.NodeResults {
if nodeResult.Error != "" {
data = append(data, []string{
trailerIfGreaterThan(nodeResult.Endpoint, 64),
crossTickCell,
crossTickCell,
"Err: " + nodeResult.Error,
})
} else {
dataItem := []string{}
dataError := ""
// show endpoint
dataItem = append(dataItem, trailerIfGreaterThan(nodeResult.Endpoint, 64))
// show RX
if uint64(nodeResult.RXTotalDuration.Seconds()) == 0 {
dataError += "- RXTotalDuration are zero "
dataItem = append(dataItem, crossTickCell)
} else {
dataItem = append(dataItem, whiteStyle.Render(humanize.IBytes(nodeResult.RX/uint64(nodeResult.RXTotalDuration.Seconds())))+"/s")
}
// show TX
if uint64(nodeResult.TXTotalDuration.Seconds()) == 0 {
dataError += "- TXTotalDuration are zero"
dataItem = append(dataItem, crossTickCell)
} else {
dataItem = append(dataItem, whiteStyle.Render(humanize.IBytes(nodeResult.TX/uint64(nodeResult.TXTotalDuration.Seconds())))+"/s")
}
// show message
dataItem = append(dataItem, dataError)
data = append(data, dataItem)
}
}
}
sort.Slice(data, func(i, j int) bool {
return data[i][0] < data[j][0]
})
table.AppendBulk(data)
table.Render()
} else if dres != nil {
table.SetHeader([]string{"Node", "Path", "Read", "Write", ""})
data := make([][]string, 0, len(dres))
if len(dres) == 0 {
data = append(data, []string{
"...",
"...",
whiteStyle.Render("-- KiB/s"),
whiteStyle.Render("-- KiB/s"),
"",
})
} else {
for _, driveResult := range dres {
for _, result := range driveResult.DrivePerf {
if result.Error != "" {
data = append(data, []string{
trailerIfGreaterThan(driveResult.Endpoint, 64),
result.Path,
crossTickCell,
crossTickCell,
"Err: " + result.Error,
})
} else {
data = append(data, []string{
trailerIfGreaterThan(driveResult.Endpoint, 64),
result.Path,
whiteStyle.Render(humanize.IBytes(result.ReadThroughput)) + "/s",
whiteStyle.Render(humanize.IBytes(result.WriteThroughput)) + "/s",
"",
})
}
}
}
}
table.AppendBulk(data)
table.Render()
} else if cres != nil {
table.SetHeader([]string{"Endpoint", "Tx"})
data := make([][]string, 0, 2)
tx := uint64(0)
if cres.TimeSpent > 0 {
tx = uint64(float64(cres.BytesSend) / time.Duration(cres.TimeSpent).Seconds())
}
if tx == 0 {
data = append(data, []string{
"...",
whiteStyle.Render("-- KiB/s"),
"",
})
} else {
data = append(data, []string{
cres.Endpoint,
whiteStyle.Render(humanize.IBytes(tx)) + "/s",
cres.Error,
})
}
table.AppendBulk(data)
table.Render()
}
return s.String()
}