-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
benchmark.go
79 lines (66 loc) 路 2.09 KB
/
benchmark.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
package view
import (
"context"
"io/ioutil"
"path/filepath"
"strings"
"github.com/derailed/k9s/internal"
"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/config"
"github.com/derailed/k9s/internal/perf"
"github.com/derailed/k9s/internal/render"
"github.com/derailed/k9s/internal/ui"
"github.com/gdamore/tcell"
)
// Benchmark represents a service benchmark results view.
type Benchmark struct {
ResourceViewer
}
// NewBenchmark returns a new viewer.
func NewBenchmark(gvr client.GVR) ResourceViewer {
b := Benchmark{
ResourceViewer: NewBrowser(gvr),
}
b.GetTable().SetBorderFocusColor(tcell.ColorSeaGreen)
b.GetTable().SetSelectedStyle(tcell.ColorWhite, tcell.ColorSeaGreen, tcell.AttrNone)
b.GetTable().SetColorerFn(render.Benchmark{}.ColorerFunc())
b.GetTable().SetSortCol(ageCol, true)
b.SetContextFn(b.benchContext)
b.GetTable().SetEnterFn(b.viewBench)
return &b
}
func (b *Benchmark) benchContext(ctx context.Context) context.Context {
return context.WithValue(ctx, internal.KeyDir, benchDir(b.App().Config))
}
func (b *Benchmark) viewBench(app *App, model ui.Tabular, gvr, path string) {
data, err := readBenchFile(app.Config, b.benchFile())
if err != nil {
app.Flash().Errf("Unable to load bench file %s", err)
return
}
details := NewDetails(b.App(), "Results", fileToSubject(path), false).Update(data)
if err := app.inject(details); err != nil {
app.Flash().Err(err)
}
}
func (b *Benchmark) benchFile() string {
r := b.GetTable().GetSelectedRowIndex()
return ui.TrimCell(b.GetTable().SelectTable, r, 7)
}
// ----------------------------------------------------------------------------
// Helpers...
func fileToSubject(path string) string {
tokens := strings.Split(path, "/")
ee := strings.Split(tokens[len(tokens)-1], "_")
return ee[0] + "/" + ee[1]
}
func benchDir(cfg *config.Config) string {
return filepath.Join(perf.K9sBenchDir, cfg.K9s.CurrentCluster)
}
func readBenchFile(cfg *config.Config, n string) (string, error) {
data, err := ioutil.ReadFile(filepath.Join(benchDir(cfg), n))
if err != nil {
return "", err
}
return string(data), nil
}